Jump to content

Form values resetting when required array is not met.


jdhorn

Recommended Posts

The website form that I need help on requires users to fill out certain fields and if those fields are not filled out the form resets with text requesting the user to populate the fields.

 

The problem I am having is that all drop down values are resetting when my form displays the error message. However text fields that users manually fill in data do not reset.

 

I am using <select> and <option> tags for my drop down menus and <input> tags for my fields.

 

How can I keep the drop down values from resetting? Do I need to change the way I display the error message?

 

Click here for the website form in question.

 

Here is the code for the required fields.

 

 

if ($_POST && !$_POST['drvset']) {

//var_dump($_POST);

$required = array ("name","byear","bmonth","bday","propertyaddress","zip","email1","email2","homephone");

foreach ($required as $key => $value) {

if ($_POST[$value] == '') {

$err = 1;

}

if (($value == 'byear' && $_POST['byear'] == '0')||($value == 'bmonth' && $_POST['bmonth'] == '0')||($value == 'bday' && $_POST['bday'] == '0')) {

$err = 1;

}

if ($err == '' && $_POST['email1'] != $_POST['email2']) {

$err = 2;

}

}

 

 

Link to comment
Share on other sites

G'day jdhorn,

 

By the sounds of what you are saying is that as the page refreshes back to itself you are loosing the selection in the options.

 

Perhaps the following will help and the use of "selected" in your option tag.  (this will also work when you use the value item inside the option tag (if you are using it).

 

<?php

$usrStatus=$_POST["usrStatus"];

?>

<select name="usrStatus" class="ezButtonsINPUTBOXplain" id="usrStatus">
                                <option selected><? echo "$usrStatus" ?></option>
                                <option>ACTIVE</option>
                                <option>SUSPENDED</option>
                                <option>CLOSED</option>
                            </select>

 

 

if that doesn't help msg back, I may not understand your question and this was a basic thing (but my brain is a basic thing :( so i may not be able to help)

Link to comment
Share on other sites

I actually ran into another issue while implementing the code. I use a dynamic based upon the number of items the user selects for a certain category.

 

<label>Gender </label>
<select name="gender<?php echo (int)$i+1;?>" id="gender<?php echo (int)$i+1;?>">
<option value="Male">Male</option>
<option value="Female">Female</option>
</select><br />

 

How would I use the code you provided in this scenario?

Link to comment
Share on other sites

I could misunderstand what you are saying, if so post again and rephrase (My brain is broken today, but isn't a full piece any day anyway)

 

<label>Gender </label>
<select name="gender<?php echo (int)$i+1;?>" id="gender<?php echo (int)$i+1;?>">
<option value="gender<?php echo (int)$i+1;?>" selected>gender<?php echo (int)$i+1;?></option>
<option value="Male">Male</option>
<option value="Female">Female</option>
</select><br />

 

 

Although I i'm not fully understanding it appears to me that on your form you will have for example 3 of these each named:

gender1

gender2

gender3

but instead of statically enterig the numbers you are using code. So the above should work ok.

 

If that doesn't work try this:

 

replace:

<option value="gender<?php echo (int)$i+1;?>" selected>gender<?php echo (int)$i+1;?></option>

 

with this:

<option value="gender<?php echo $_REQUEST["gender"(int)$i+1];?>" selected>gender<?php echo $_REQUEST["gender"(int)$i+1];?></option>

 

if that fails let me know, I know I have code around in a few places where I have done that or similar.

 

 

 

...on another hand if you are familiar with them you could load them into an array etc...but if you are not familiar with that concept just ignore this comment

Link to comment
Share on other sites

Thanks for the response. Unfortunately neither of these solutions works.

 

After playing around with the code this seems to work.

					
<?php
$gender=$_POST['gender'.((int)$i+1)]
?>
<label>Gender </label>
<select name="gender<?php echo (int)$i+1;?>" id="gender<?php echo (int)$i+1;?>">
<option selected><? echo "$gender" ?></option>
<option value="Male">Male</option>
<option value="Female">Female</option>
</select>

 

However this sets gender1 = $gender then overwrites this value with gender2 etc. How can I write all these value to their own code and then call each one with echo?

Link to comment
Share on other sites

Refiling selects with the selected item on error is a challenge. I have come up with a couple good ways to do this.

 

Sometimes I use a function that will compare a couple items and if so then it echos selected="selected".

 

e.g.

 

<?php
function selectError($thisValue, $compareValue)
{
  global $errorVar;
  if(isset($errorVar))
  {
	 if($thisValue == $compareValue)
	 {
		echo 'selected="selected"';
	 }
  }
}
?>

<label>Gender </label>
<select name="gender" > 
<option value="Male" <?php selectError('Male', $_POST['gender']); ?> >Male</option>
<option value="Female" <?php selectError('Female', $_POST['gender']); ?>>Female</option>
</select>

 

If you have dynamic items, it is easy too.

 

Here is some code that I helped on yesterday on this board. It shows how to use dynamic values for the select items and refill on error.

 

<?php

$cities = array('city1', 'city2', 'city3');
$customer_city = 'city3';
echo "<select name='cities'>";
foreach($cities as $city) 
{
  // this is the shorthand if/else statement, and it is responsible for comparing what was selected and adding a selected="selected item
  $selected = ($customer_city== $city)? " selected='selected'" : ""; 
  echo '<option value="'.$city.'" '.$selected.'>'.$city.'</option>';
}
echo "</select>";

?>

 

That's the 2 easiest ways to deal with re-filling dropdown values.

 

Hope it helps.

 

Nate

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.