tags:

views:

2109

answers:

6

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?

A: 

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.

Itay Moav
What about people who have JavaScript turned off? And I believe Web Accessibility requirements mean a site has to work with no JS.
Phill Sacre
JS is today an integral part of a web application. What you say is like "I will play this game, but without the physical engine..."
Itay Moav
No... validation simply cannot be done entirely on the client side because of security issues. It must be done on the server side and it's a + to implement it on the client side
m_oLogin
+2  A: 

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']; ?>"
Erick
+1  A: 

This sounds like basic form validation. I would recommend 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
Quintin Robinson
+1  A: 

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.

Phill Sacre
+1  A: 

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 incoming 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>';
}
?>
Manos Dilaverakis
Instead of using hidden field to check if form has been submitted, you can do: if (isset($_POST['Submit']))
z-boss
A: 

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...

xdevel2000