Question

I've written a simple entry form that compiles information onto a database, and I'm having trouble with my required fields...

I use a form to process the data via post method if the user neglects to fill in a required field I would like to bring him back to the original form with his/her previous fields entered.

How to I pass info back into an input label? Is there an easy way to do this without any crazy scripts? I started with sessions but I realized I have no clue how to put the stored info from the session back into the input field so the user doesnt have to retype all of their info... Also would a cookie be better for this over session?

Thanks guys,

Arthur

Was it helpful?

OTHER TIPS

When you post a form, all those variables are submitted into a global array named $_POST['input_name'] and available on the page posted to. A lot of times what I like to do if I'm doing it fairly quickly, is just make the value of those input fields equal the same as what would be posting.

For example lets say we have a desired username field but the form didn't validate for some reason and posted back to itself; we don't want them to have to enter it again:

<input type="text" name="username" value="<?php print $_POST['username']; ?>" />

Of course when they first load the page, the value will be empty so there is nothing there, but if for some reason it posts back, that "username" field will already contain entered information.

Even better is java script validation, as the form doesn't have to post back, but this will do the job just fine!

Since the user posts all your data to you, these values are also available in your scripts. So you can use them very easily in the case of text-fields, but a bit more work is required for select options, checkboxes and radio buttons:

<input id="myid" name="myid" type="text" 
   <?php echo !empty($_POST['myid'] ? "value=\"{$_POST['myid']}\"" ?> />

For radio buttons, select options and checkboxes you instead have to check the value to see if it corresponds with the entry you are currently outputting and print selected="selected".

When it comes to validation you can also have a JavaScript validation to give feedback sooner to the user about possible failures. Just remember to have the same validation on the server side in case someone doesn't have JavaScript enabled or submits it using JavaScript, thus bypassing your client side validation.

Not sure if this is the best way, but you could redirect to a "reload" page and use the values from POST or GET to reinput the existing fields. So first validate, the fields that are required and if any are missing, redirect to this page. Then the POST or GET will have all of the values the user filled in (and the missing required fields will already be blank) so you just loop through and load up the supplied info. Additionally, if they supplied incorrect info you could manually clear it and this will also allow you to mark the missing required fields.

Another option is put your validation in JS so you know the data is good before you submit. However, I'm not sure if there are security concerns with that or not.

I check to see if the post value has been set otherwise you can show a default value, then use a bit of jQuery to remove it when the input has focus

<input type="text" name="first_name" id="first_name" value="<?php if(isset($_POST['myid'])) {   echo $_POST['myid'] } else { echo "Your Name" ?>"></input>

Here's the jQuery which will remove the default Your Name when the textbox has focus.

$(document).ready(
function(){
    $.fn.clearDefault = function() {
        return this.focus(function() {
            if( this.value == this.defaultValue ) {
                this.value = "";
            }
        }).blur(function() {
            if( !this.value.length ) {
                this.value = this.defaultValue;
            }
        });
    };
    // clear default textbox entries
    $("#first_name"). clearDefault();
}

);

<input type="text" name="username" value="<?php isset($_POST['username']) ? echo $_POST['username'] : null; ?>" />

will work fine

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top