POST BACK in PHP or JAVASCRIPT? - Stack Overflow

how can I post back the data that are already in the text field?example: if I miss one of the required

how can I post back the data that are already in the text field?

example: if I miss one of the required field an error will prompt when i click the submit button.

How can I make an post back data in that form using php or javascript and make the cursor of the mouse directly located to the field that caused an error?

how can I post back the data that are already in the text field?

example: if I miss one of the required field an error will prompt when i click the submit button.

How can I make an post back data in that form using php or javascript and make the cursor of the mouse directly located to the field that caused an error?

Share Improve this question asked Mar 5, 2009 at 15:19 phpaddictphpaddict
Add a ment  | 

6 Answers 6

Reset to default 2

There is no automated ways in PHP to write back the informations of the fields so you just have to echo it back.

Let's say you've got a "username" field ( <input type="text" name="username" /> ) you just need to add this:

value="<?php echo isset($_POST['username']) ? $_POST['username'] : ''; ?>"

or if you like more:

value="<?php if(isset($_POST['username'])) echo $_POST['username']; ?>"

changed "" to ''

This sounds like basic form validation. I would remend reading some of these tutorials or looking for some pre-built PHP form validation mechanisms.

  1. Form validation using PHP
  2. PHP/CSS Form validation
  3. PHP Form Validation

Some frameworks such as CodeIgniter will do this for you if you use their own libraries. It's worth checking out such a framework as they provide a lot of other benefits. Of course it's not always possible to transfer an existing application but it's still useful to bear in mind for the future.

If I understand this correctly you want to keep whatever data the user has already entered, tell him what he did wrong and preferably focus on the bad field. If so then here's a very basic example using a form with two fields where both need to be filled in to proceed.

<?php

$field1=$_POST['field1'];
$field2=$_POST['field2'];

$badField="";
if($_POST['form_action']=="submitted") {
    //Check ining data
    if(empty($field1)) {
        $badField="field1";
        echo 'field1 is empty<br>';
    }
    elseif(empty($field2)) {
        $badField="field2";
        echo 'field2 is empty<br>';
    }
    else {  //Everything ok - move to next page
        header('Location: <next page>');
    }
}

echo '<form name="mybo" action="' . $_SERVER['PHP_SELF'] . '" method="POST">
        <input type="text" name="field1" value="' . $field1 . '"><br>
        <input type="text" name="field2" value="' . $field2 . '"><br>
        <input type="submit" name="Submit" value="    Enter    ">
        <input type="hidden" name="form_action" value="submitted">
    </form>';

//Focus on empty field
if(!empty($badField)) {
    echo '<SCRIPT language="JavaScript">
        document.mybo.' . $badField . '.focus(); </SCRIPT>';
}
?>

I think the Moav's answer is "philosophically" correct however if you want do that you can:

1) pass via GET or POST the text control id; 2) on the server check that error condition; 3) fill an hidden input field with that value on the page returns 4) if error that with JS you can do: window.onload = init; // init stuff here function init() { checkForError(); }

function checkForError() { var h = document.getElementById("error_field"); var v = h.value; if(v) document.getElementById(v).focus(); }

However, if you will do that for every error field there will be a post and this is by a user perspective very boring...so it is better to adopt other approaches...

I would take a different approach:

Validation should be in JS, and as such you never loose data, as you don't submit. Any wrong data that was submitted and caught on the server is due to someone trying to pass over your JS validation, which means he has criminal thoughts, usually.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742285520a4415249.html

相关推荐

  • POST BACK in PHP or JAVASCRIPT? - Stack Overflow

    how can I post back the data that are already in the text field?example: if I miss one of the required

    1天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信