phpaddict
phpaddict

Reputation:

POST BACK in PHP or JAVASCRIPT?

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?

Upvotes: 1

Views: 10192

Answers (6)

Erick
Erick

Reputation: 6089

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

Upvotes: 2

xdevel2000
xdevel2000

Reputation: 21374

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

Upvotes: 0

Manos Dilaverakis
Manos Dilaverakis

Reputation: 5869

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>';
}
?>

Upvotes: 1

user7094
user7094

Reputation:

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.

Upvotes: 1

Quintin Robinson
Quintin Robinson

Reputation: 82355

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

Upvotes: 1

Itay Moav -Malimovka
Itay Moav -Malimovka

Reputation: 53607

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.

Upvotes: -1

Related Questions