Jump to content

Sticky form, but already in php tags. Help!


Scorptique

Recommended Posts

This is a drop down menu of day / month / year. I dont know how to make it sticky since it's already in a php tag. Does anyone know how to make it sticky? Help please!

 

 

Sticky means that when user refreshes the page, the value in the form will still remain the same.

 

<?php
    	echo "<select name='day'>";
for ($x=1; $x<=31; $x++) {
	echo "<option value=$x>$x</option>";
}
echo "</select> ";


echo "<select name='month'>";
for ($x=1; $x<=12; $x++) {
	echo "<option value=$x>$x</option>";
}
echo "</select> ";


echo "<select name='year'>";
for ($x=2008; $x>=1930; $x--) {
	echo "<option value=$x>  $x  </option>";}	
echo "</select> ";
    ?>

Assuming you're talking about what I think you're talking about (retaining form values upon page-reload), use:

 

<?php
echo "<select name='year' value='".$_POST['year']."'>";
//I just assumed your form method was POST, use $_GET otherwise (obviously)
for ($x=2008; $x>=1930; $x--) {
	echo "<option value=$x>  $x  </option>";}	
echo "</select> ";
?>

Firefox has a little proprietary piece of annoyance called autocomplete that remembers select box values, usually I am fighting to turn if off:

 

<form autocomplete="off"> </form>

 

Try this:

 

<?php
   echo '<select name="day">';
   for ($x=1; $x<=31; $x++) {
      echo '<option value="'.$x.'" '.($_POST['day'] == $x) ? 'selected="selected"' ? ''.'>'.$x.'</option>';
   }
   echo '</select>';
?>

 

 

Flyhoney, I dont understand why are the question marks in it. What does it mean? I copied this sentence to my code and there was some ';' error =/

 

echo '<option value="'.$x.'" '.($_POST['day'] == $x) ? 'selected="selected"' ? ''.'>'.$x.'</option>';

change the 2nd ? to a :

 

It's called a ternary operator.  Basically it's a shorthand version of an if..else.

 

if(condition) {

  // condition is true

} else {

  // condition is false

}

 

vs.

 

(condition) ? 'condition is true' : 'condition is false'

 

example:

 

$x = 5;

echo ($x < 10)? 'x is less than 10' : 'x is greater than 10';

 

that would echo 'x is less than 10'

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.