SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Wizard billy_111's Avatar
    Join Date
    Jul 2009
    Posts
    1,683
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to keep the values in a text box after submitting form with validation errors

    Hey,

    I have a registration form, which is validated.

    When the user doesnt enter a required field there are given an error message and the form is wiped out and they start again, now i have everything going on the one page i didnt want to go to another page.

    I have a simple if statement as so:-

    PHP Code:
                    if ($_POST['txt_name'] == "" || $_POST['txt_c_name'] == "" || $_POST['txt_phone'] == "" || $_POST['txt_email'] == "" || $_POST['txt_password'] == "")
                    {    
                        echo 
    "<p style=margin-left:22px;width:660px;font-size:12px;border:1px solid blue><img src=images/warning_48.png width=20px> You did not complete all of the required fields. <a href='register.php'> Return</a> to amend.</p>";
                    }
                    elseif(!
    $_POST['txt_email'] == "" && (!strstr($_POST['txt_email'],"@") || !strstr($_POST['txt_email'],"."))) 
                    {
                    echo 
    "<p style=margin-left:22px;width:660px;font-size:12px;border:1px solid blue><img src=images/warning_48.png width=20px> Please enter a valid e-mail address. <a href='register.php'> Return</a> to amend.</p>";
                    }
                    else if (
    $_POST['txt_password'] != $_POST['txt_password2']) {
                    echo 
    "<p style=margin-left:22px;width:660px;font-size:12px;border:1px solid blue>Your passwords did not match. <a href='register.php'>Return</a> to amend.</p>";
                    }
                    else 
                    {
    //Do the insert here

    If this does not happen then the registration form is shown. But how can i stop the fields being wiped out each time a user makes a blunder..?

    You can view my page here:-

    http://www.offers4manchester.com/register.php

    Regards

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    PHP Code:
    $c_name "" ;
    $msg "" ;
    if( isset( 
    $_POST['c_name'] ) && $_POST['c_name'] == "" ) {
    $msg . = "First name, " ;
    $c_name $_POST['c_name'] ;

    }

    if( 
    $msg != "" )
    echo 
    "Error you missed:" rtrim($msg", " );


    echo 
    "Enter your first name <input type='text' value='$c_name' />" 
    Thats a simple way of doing it if your form posts back to itself.
    Many people would use Sessions to restore data.
    The above would become massively unwieldy and brittle if you had more than a couple of vars, so you'd likely create a function which took care of all that duplication of var names, and also you'd likely create and array of elements.

    There are some old posts around going into this a bit deeper, I'll try and find one ...


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •