简体   繁体   中英

Input values lost when form submitted with errors

My form is working fine with the validations being done by PHP. I have three fields: Name, EMail and Message. Form and PHP code is within the same pgae, same page is called for validations when user submits the form.

When a user submits the form, same page is called and it checks whether the form is submitted or not. If the form is submitted it then does the validations for blank entries and throws error message below the fields to inform user that field is left blank. It also shows error icon next to field.

Till this, it is working fine.

However, the problem, is if the user has filled any field, for example name filed and left the other two fields(EMail and Message) blank, then on submittion, it throws error messages for blank fields which is ok, but for name field which was filled by user it empty the content and shows blank name field and does not show error(as earlier user had filled it).

My only concern is that when it relods the form after submission, it should also reload the earlier values in the respective fields which user input before submitting.

Below is the PHP validation code.

<?php
error_reporting(E_ALL & ~E_NOTICE);

    if(isset($_POST['nameField_Name']) AND isset($_POST['nameField_EMail']) AND isset($_POST['nameField_Message']) AND isset($_POST['nameSubmit'])){  
        // Form Submited  
        if ($_POST['nameField_Name']) {
    $phpVarNameField = mysql_escape_string($_POST['nameField_Name']);
} else {
    $errormsgNameField = "Name field is required, Please enter your Name.";

}

if ($_POST['nameField_EMail']) {
    $phpVarEMailField = mysql_escape_string($_POST['nameField_EMail']);
} else {
    $errormsgEMailField = "E-Mail field is required, Please enter your E-Mail ID.";
}

if ($_POST['nameField_Message']) {
    $phpVarMessageField = mysql_escape_string($_POST['nameField_Message']);
} else {
    $errormsgMessageField = "Message field is required, Please enter your Message.";
}
    }  
?>

Below is the form code.

                <form name="myform" action="contactus.php" method="post"">
                <div id="r1">
                    <div id="r1c1">
                        <input type="text" name="nameField_Name" id="idField_Name" placeholder="Enter your name here"/>
                    </div>
                    <div id="r1c2">
                 <?php  
                      if(isset($errormsgNameField)){  // Check if $msg is not empty  
                  echo '<img src="error.png" width="45" height="45" style="margin: 5px 0px" alt="">';
                      }  
                    ?> 
                    </div>
                </div>
                <div id="afterr1">
               <?php  
                      if(isset($errormsgNameField)){  // Check if $msg is not empty  
                  echo '<div class="statusmsg" id="idErrorMsgNameField">'.$errormsgNameField.'</div>'; // Display our message and wrap it with a div with the class "statusmsg".  
                      }  
                    ?>  

                </div>
                <div id="r2">
                    <div id="r2c1">
                        <input name="nameField_EMail" type="text" id="idField_EMail" placeholder="Enter your E-Mail address here" />
                    </div>
                    <div id="r2c2">
                <?php  
                      if(isset($errormsgEMailField)){  // Check if $msg is not empty  
                  echo '<img src="error.png" width="45" height="45" style="margin: 5px 0px" alt="">';
                      }  
                    ?> 
                    </div>
                </div>
                <div id="afterr2">
                <?php  
                      if(isset($errormsgEMailField)){  // Check if $msg is not empty  
                  echo '<div class="statusmsg" id="idErrorMsgEMailField">'.$errormsgEMailField.'</div>'; // Display our message and wrap it with a div with the class "statusmsg".  
                      }  
                    ?>  
                </div>
                <div id="r3">
                    <div id="r3c1">
                        <textarea name="nameField_Message" id="idField_Message" placeholder="Enter your message for us here"></textarea>
                    </div>
                    <div id="r3c2">
                <?php  
                      if(isset($errormsgMessageField)){  // Check if $msg is not empty  
                  echo '<img src="error.png" width="45" height="45" style="margin: 115px 0px" alt="">';
                      }  
                    ?> 

                    </div>
                </div>
                <div id="afterr3">
                <?php  
                      if(isset($errormsgMessageField)){  // Check if $msg is not empty  
                  echo '<div class="statusmsg" id="idErrorMsgMessageField">'.$errormsgMessageField.'</div>'; // Display our message and wrap it with a div with the class "statusmsg".  
                      }  
                    ?>  
                </div>

                <div id="r4">
                    <div id="r4c">
                        <input type="Submit" name="nameSubmit" id="idButton_Submit" value="Submit"  alt="Submit Button"/>
                    </div>
                </div>
                </form>

Any help will be great on this.

Thank You.

You will need to add a value attribute on your <input> elements:

<input type="text"
       name="whatever"
       value="<?php echo htmlspecialchars($_POST['whatever']); ?>"
>

It may be easier to read if PHP outputs the field:

<?php
printf('<input type="text" name="%s" value="%s">',
       'whatever',
       htmlspecialchars($_POST['whatever']));
?>

This can even be wrapped in a function so you don't need to retype it for every single form field.

Note the call to htmlspecialchars . It is needed so that < and > and quotes don't destroy your HTML document.

Well, You could do validation with jQuery validation plugin - easy and good. jQuery plugin

Or with PHP store POST data in array, check for errors and fields that are not empty set as value to input text.

if (isset($_POST)) {
    $data = $_POST;
}
foreach ($data as $row) {
    if ($row == "")
        $error = true; // do what ever you want
}

and then in form
<input type="text" name="name" value="<?php ($data['name'] != "")? $data['name'] : '' ?>" />

something like this.

Try changing your tag like :

<input type="text" 
  name="nameField_Name" 
  id="idField_Name" 
  placeholder="Enter your name here" 
  value ="<?php 
             if (isset($phpVarNameField)) 
              echo $phpVarNameField; 
         ?>"
/>
.......
<input 
   name="nameField_EMail" 
   type="text" 
   id="idField_EMail" 
   placeholder="Enter your E-Mail address here" 
   value ="<?php if (isset($phpVarEMailField)) echo $phpVarEMailField; ?>" 
 />
.......
<textarea name="nameField_Message" id="idField_Message" placeholder="Enter your message for us     
here" value ="<?php if (isset($phpVarMessageField)) echo $phpVarMessageField; ?>" ></textarea>

Good Luck !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM