Jump to content

Keeping data in fields after submit


trevor4370

Recommended Posts

I have looked and I can not see thread about this, but I am sure there must be something .... if there is please point me in that direction. Also, just learning pHp .....

 

I have a form that is validated, so the fields don't get deleted when the form is submitted and checked. I insert value="<?=$gender;?>"  (for example) in the Gender field. This seems to work with standard textfields, but what about drop down lists etc ..... it doesn't seem to work for them ... where should the value field go in these?

 

Trevor

 

Link to comment
Share on other sites

For drop down menues you have to test if your var is equal to the option value and use SELECTED in your option tag

 

print ("<select name='contact_time' class='FormEntry' id='contact_time'>
<option value='0'"); if ($contact_time=="0"){echo "selected";} print (">(Select a time)</option>
<option value='Anytime'"); if ($contact_time=="Anytime"){echo "selected";} print (">Anytime</option>
<option value='Morning'"); if ($contact_time=="Morning"){echo "selected";} print (">Morning</option>
<option value='Afternoon'"); if ($contact_time=="Afternoon"){echo "selected";} print (">Afternoon</option>
<option value='Evening'"); if ($contact_time=="Evening"){echo "selected";} print (">Evening</option>
</select>");

Link to comment
Share on other sites

Thank you for you quick reply. I tried this but it does not seem to work .... it displays OK and everything, but once the Submit button is clicked and the form valilates, the entry reverts back to (Select a time), doesn't matter what is selected.

 

Am I doing something wrong ???? I am using the same code you gave me.

 

<?php

print ("<select name='contact_time' class='FormEntry' id='contact_time' >

<option value='0'"); if ($contact_time=="0"){echo "selected";} print (">(Select a time)</option>

<option value='Anytime'"); if ($contact_time=="Anytime"){echo "selected";} print (">Anytime</option>

<option value='Morning'"); if ($contact_time=="Morning"){echo "selected";} print (">Morning</option>

<option value='Afternoon'"); if ($contact_time=="Afternoon"){echo "selected";} print (">Afternoon</option>

<option value='Evening'"); if ($contact_time=="Evening"){echo "selected";} print (">Evening</option>

</select>");

 

  ?>

Link to comment
Share on other sites

I prefer to to use an array. (The same technique works if you are loading the options from a database instead of an array)

 

<?php 
$options = array('Anytime', 'Morning', 'Afternoon', 'Evening');

echo "<select name='contact_time'>";
echo "<option value='0'>(select time)</option>";

foreach ($options as $opt)
{
    $sel = $opt==$_POST['contact_time'] ? 'selected' : '';
    echo "<option value='$opt' $sel>$opt</option>"; 
}
echo '</select>';
?>

Link to comment
Share on other sites

Lol okay haha I dunno what I am doing. But i use if (isset ($_POST['submit'] {  for my form action.

 

then i can make it return me back to my form.

 

and then once it returns me the form data will be filled out like it was left.

 

print 'Username: <input type='text' name='username' size='20' value="' .$_POST[username']. '" />':

 

edit:

 

Okay I quit. I am wayy off topic

Link to comment
Share on other sites

OK .. yes I did forget - $contact_time=$_POST[contact_time];

 

Thanks, and thanks Barand for your code .... I will try this as well. Just one more thing (and there always is) ... checkboxes ... how are these accomplished ... value="<?=$xxxxx;?>"  doesn't work here either.

 

I have learnt more on pHp today, thanks heaps .... more for the snippet box.

 

 

Link to comment
Share on other sites

OK .... I get it now.

 

Now if I want an error message ... the following should work ... but doesn't.

 

<?php

$options = array('Male','Female');

 

echo "<select name='gendar'>";

echo "<option value=''></option>";

 

foreach ($options as $opt)

{

    $sel = $opt==$_POST['gendar'] ? 'selected' : '';

    echo "<option value='$opt' $sel>$opt</option>";

}

echo '</select>';

 

 

if (trim($gendar)==''){

  print " Please enter your Gendar ";

    }else{

  print " ";

    }

?>

 

What am I doing wrong, it should work .... I have used this for textfields ???

 

Thanks

Link to comment
Share on other sites

No .... actually is is a large form ...

 

$firstname  = $_POST['firstname'];

$surname = $_POST['surname'];

$gender= $_POST['gender'];

$gender2= $_POST['gender'];

$age_on_12_05_07= $_POST['age_on_12_05_07'];

$day= $_POST['day'];

$month= $_POST['month'];

$year= $_POST['year'];

$address= $_POST['address'];

$town= $_POST['town'];

$postcode= $_POST['postcode'];

$state= $_POST['state'];

$phone= $_POST['phone'];

$Emergency_contact_name= $_POST['Emergency_contact_name'];

$Emergency_contact_phone= $_POST['Emergency_contact_phone'];

$phone_home= $_POST['phone_home'];

$phone_work= $_POST['phone_work'];

$phone_mobile= $_POST['phone_mobile'];

$email= $_POST['email'];

$email2= $_POST['email2'];

$medical= $_POST['medical'];

$finish_time_mar= $_POST['finish_time_mar'];

$finish_time_5km= $_POST['finish_time_5km'];

$finish_time_1500m= $_POST['finish_time_1500m'];

$race1= $_POST['race1'];

$race2= $_POST['race2'];

$race3= $_POST['race3'];

$race4= $_POST['race4'];

$race5= $_POST['race5'];

$race2_15under= $_POST['race2_15under'];

$race3_15under= $_POST['race3_15under'];

$race5_15under= $_POST['race5_15under'];

$Pay= $_POST['Pay'];

$Terms= $_post['Terms'];

$caps= $_POST['caps'];

$hats_sm= $_POST['hats_sm'];

$hats_med= $_POST['hats_med'];

$hats_lg= $_POST['hats_lg'];

$dinner_adults= $_POST['dinner_adults'];

$dinner_children= $_POST['dinner_children'];

$Bus_from_Yangan= $_POST['Bus_from_Yangan'];

$Bus_start_Killarney= $_POST['Bus_start_Killarney'];

$Bus_from_Falls= $_POST['Bus_from_Falls'];

$race1_name= "Half Marathon";

$race2_name= "4.6km Cross-Country";

$race3_name= "5000m";

$race4_name= "10km Ascent ";

$race5_name= "1500m";

$race2_15under_name= "4.6km X-Country Under 15";

$race3_15under_name= "5000m under 15";

$race5_15under_name= "1500m Under 15";

 

/*Formula for total costs  */

 

$CapsCost= $caps*10;

$hats_sm_cost= $hats_sm*10;

$hats_med_cost= $hats_med*10;

$hats_lg_cost= $hats_lg*10;

$dinner_adults_cost=$dinner_adults*15;

$dinner_children_cost=$dinner_children*7;

$TotalCost= $race1+$race2+$race3+$race4+$race5+$CapsCost+$hats_sm_cost+$hats_med_cost+$hats_lg_cost+$dinner_adults_cost+$dinner_children_cost+$race2_15under+$race3_15under+$race5_15under;

 

Just makes it easier for the user to find out what they have not filled in .... I have found not all users know what they are doing on the Internet .... even when completing forms .... I have tried to make them simple, but someone alway has a problem. Just trying to be end user friendly.

 

Thanks

 

Link to comment
Share on other sites

Okay well my keyboard is acting up aagin so I am not sure if this script works. infact I am 90% sure it wont missing some quotes and stuff. But I think you may try somethign like:

<?php
//filename is test.php in this case and we are doing the full script on one page
echo "<html>
<head></head>
<body>
<form action='test.php' method='post'>
<select name='gender'>
	<option value=' ' selected='$_POST['gender']'> $_POST['gender']</option>
	<option value='1'>Female</option>
	<option value='2'>Male</option>
</select>
<input type='submit' value='Submit'> <input type='reset' value='Reset'>
</form>";

if (isset($_POST['submit'])) {
$female = 1;
$male = 2;
if ($_POST['gender'] != $female || $_POST['gender'] != $male) {
¨	echo "please enter a gender";
} else {
	echo "echo or whatever you wanna do here";
}
}
?>
</body>
</html> 

Link to comment
Share on other sites

if ($_POST['gender'] != $female || $_POST['gender'] != $male) {

¨ echo "please enter a gender";

} else {

echo "echo or whatever you wanna do here";

}

 

Suppose 1 (male) is entered.

 

in that case, gender is not equal to female, so it asks for a gender.

 

If 2 (female) is entered, gender is not equal to male, so it asks for a gender.

 

 

If you use && as suggested it will ask for gender if it is not 1 AND it is not 2.

 

You need to get a handle on Boolean logic

 

NOT (A || B) == NOT A && NOT B

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.