Hello,

I have a form which I am using simple php validation on.

At the end you can find the error array code.

Some of the options are in a drop-down list/menu

Although the form checks string length as required on _post, when the page is refreshed the input value (on dropdowns) will disappear, which will become quite frustrating having to enter the info each time, despite the error message not telling you it is missing.

Is there a way to maintain the previous input? Or perhaps there is a better way to check for errors?

if(isset($_POST['Submitted'])) {

  $Errors = Array();
  if(strlen(trim($_POST['name'])) <3 ) $Errors[] = "Please check your name is correct";
  if(!preg_match("/[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9]{2,4}/",$_POST['email'])) $Errors[] = "please check your email address";
  if(strlen($_POST['group_size']) >4) $Errors[] = "How many people?";
  if(strlen($_POST['rooms']) >4) $Errors[] = "How many rooms?";
  if(strlen($_POST['nights']) >4) $Errors[] = "How many nights?";
  if(!trim($_POST['comment'])) $Errors[] = "Please add some more comments";
  if(strtolower(trim($_POST['antispam'])) != "xxxxxx") $Errors[] = "Spam check should be xxxxxx";
  if(!count($Errors)) {

You can look at the html at http://yabbox.com/form-php.php

    echo '<input name="elementName" value="'.isset($_POST['elementName']) ? $_POST['elementName'] : '' . '"/>';

      Thanks johanafm,

      But can you be more specific? Where does this line go? I'm thinking it may be instead of the echo htmlentities, yes? Or maybe combined with the if array at the top of the page?

      Sorry, this isn't my subject.

        right, so I've put it here in the is set error code and it prints the answer, but in the wrong place.

        <?php
        		if(isset($Errors) && is_array($Errors) && count($Errors)) {
          		echo '<p>'."\n";
        		foreach($Errors as $Error) echo $Error.' *<br />'."\n";
        		echo '<input name="group_size" value="'.isset($_POST['group_size']) ? $_POST['group_size'] : '' . '"/>'; 
          		echo '</p>'."\n";
        		}
        		if(isset($MailSuccess)) {
          		if(!$MailSuccess) {
            	echo '<p class="required">There was an unexpected error. Please try later</p>'."\n";
          }
        }
        ?>
        

        So, I added it to the form list/menu and it seems to be ignored.

        <select name="group_size" value="<?php 
        		if(isset($_POST['group_size'])) echo htmlentities($_POST['group_size']);
        		echo '<input name="group_size" value="'.isset($_POST['group_size']) ? $_POST['group_size'] : '' . '"/>';?>" tabindex="18" size="1" id="group_size">        
        <option value="blank">People</option>

        Sorry, if I'm missing the obvious.
        Andy

          in your form, set all the default values for each of the items to be what they are set to if they ARE set... johanafm gave you example/generic code to accomplish this... edit based on the form/field name

            Example for listbox:

            <?php
            
            $atransport = array(0 => "Select",
                1 => "Walk",
                2 => "Bus or coach",
                3 => "Train",
                4 => "Car",
                5 => "Boat",
                6 => "Cycle",
                7 => "Unsure"); // initialize form variables
            
            $cleantransport = 0; // single listbox 
            
            
            if  ($_SERVER['REQUEST_METHOD']=="POST") { 
            
            	$aErrors = array(); 
            	// check transport
            	if (isset($_POST['transport']) && 0 != (int)$_POST['transport']) {
            	    $cleantransport = (int)$_POST['transport'];
            	    if (!array_key_exists($cleantransport, $atransport)) {
            	        $aErrors['transport'] = 'You must choose a valid transport';
            	    } 
            	} else {
            	    $aErrors['category'] = 'You must choose at least one from the list "transport"';
            	} 
            
            	if (0 == count($aErrors)) {
            		// send mail here    
            
            	}
            	else
            	echo implode("<br />" , $aErrors);
            }
            // form comes here
            ?>
            
            <select id="transport" name="transport" border="0">
            <?php 
            // add options from transport array
            foreach ($atransport as $key => $value) {
                $selected = ($key == $cleantransport) ? 'selected = "selected"': '';
            
            ?>
              <option <?php echo $selected; ?> value="<?php echo $key; ?>"><?php echo $value; ?></option>
            <?php
            } // option ends
            
            ?>
              </select><br/>
              </form>

              And validate your html code here

              You end up with something like this

              <select>
              	2 <!-- echo htmlentities($_POST['group_size']) -->
              	<input>
                        <option value="blank">People</option>
              

                Thanks for everybody who has replied.

                scrupul0us - I thought I had set the default value in the group_size example above. But as it wasn't incorporating the 'selection' I can see why the dropdown was not set.

                djjjozsi - a great help, and I went through and added this code to all my listboxes.

                What do I need to change so the text is printed to email and not the integer?

                // form variables start
                $acountry = array(0 => "Select",
                    	1 => "England",
                    	64 => "Other...");
                

                This code works great, but it is far removed from my existing way of checking for Errors and I found myself in deep water when trying to combine the two. The previous code was split into sections: error array, error printing, mail sending, etc. I tried splitting your code up, so the form variables inside the form, while the error checks where grouped together, but I couldn't mix this type of code with the existing error code, so the existing send_request would override the second lot of php.

                johanafm - when I asked for validation, I meant form checking, not html validation. The result you quoted came about because I added your code to the form, hoping it would select the existing choice - but didn't really know where to place it.

                I have tried to search hk2.php.net resource, but it doesn't really give tutorials, just complicated raw code. It's hard to know where to start when you don't know the correct terms!

                Thanks for your help.

                  The select element sends only the value only of the selected option on form submit. The value, if not explitly set, defaults to the option text. Either remove the value attribute from your options, or retrieve the text based on the integer in some way. You did somehow associate the two when showing the form in the first place, so you should be able to repeat that process once more.

                    $stringToThEmail.='Transport: ' . $atransport[$cleantransport];

                    Because your form sends the index value only, based on the array index.

                      Write a Reply...