forms

I need to show selected value after pressing “Submit”-button. I cannot succeed with the first field. Can someone find the solution? I have tried several approaches. One of them is here, too (line 21).
[php]

Peatused <?php $peatused=array("esimene", "teine", "kolmas", "neljas", "viies", "kuues", "seitsmes", "kaheksas"); //echo "teine element: ".$peatused[6]; ?>

Vali peatus ja mitu peatust selle ümber kuvada

Vali peatus // > esimene teine kolmas neljas viies kuues seitsmes kaheksas
/>
<?php 
if(!empty($_POST)) { // vorm esitati
	$errors=array();
	if(!empty($_POST["peatus"])) {
	//do sth 
	} else {
	$errors[]="Palun vali peatus!";
	}
	if(!empty($_POST["range"])) {
	//do sth 
	} else {
	$errors[]="Palun vali peatuste arv!";
	}		
}
	?>


<?php if (!empty($errors)): ?>
	<?php foreach($errors as $e): ?>
		<p style="color:red"> <?php echo $e; ?> </p>
	<?php endforeach?>
<?php endif;?>


</body>
[/php]

Hello Rene,

On line 21,

//<option <?php if($_POST["peatus"]==$peatused[0]) echo "value=\"{$peatused[0]}\""; ?> > esimene</option>

I think an if statement would do

<?php
   if(isset($_POST['peatus']))
    {
?>
<option <?php if($_POST["peatus"]==$peatused[0]) echo "value=\"{$peatused[0]}\""; ?> > esimene</option>

<?php
     }
?>
[php] > esimene[/php]
What exactly is the point of this anyway? You accomplish the SAME THING doing this, [php] esimene[/php]

So, why complicate it? Unless you are actually trying to do something more like this, and haven’t worked out how?
[php]
for( $i = 0; $i < count( $peatused ); $i++ )
echo “{$peatused[ $i ]}\n”;
[/php]

Thank you!
Here’s my new solution. I still have one problem to solve. In the first field, I ecpect the selected (default) value to be “Vali peatus”. But right now it’s “esimene”. Could someone find the problem?
[php]

Peatused <?php $peatused=array("esimene", "teine", "kolmas", "neljas", "viies", "kuues", "seitsmes", "kaheksas"); ?>

Vali peatus ja mitu peatust selle ümber kuvada

			 <?php
            echo "<option selected value=\"\">Vali peatus</option>";
			 for( $i = 0; $i < count($peatused); $i++ ) { 
                 if($i == array_search($_POST['peatus'], $peatused)) { 
                    $selected = "selected";
                 }else{
                     $selected = "";
                 }
                     echo "<option value=\"$peatused[$i]\" ".$selected.">$peatused[$i]</option>";                                                    
            }
			 ?>				 				 
		</select>
		<br/>
		<input type="number" name="range" <?php if(!empty($_POST["range"]) && is_numeric($_POST["range"])) 
	echo "value=\"".htmlspecialchars($_POST["range"])."\" "; ?> /><br/>
		<input type="submit" value="kuva"/>
	</form>
	
<?php 
if(!empty($_POST)) { // vorm esitati
	$errors=array();
	if(!empty($_POST["peatus"])) {
	//do sth 
	} else {
	$errors[]="Palun vali peatus!";
	}
	if(!empty($_POST["range"])) {
	//do sth 
	} else {
	$errors[]="Palun vali peatuste arv!";
	}		
}
	?>


<?php if (!empty($errors)): ?>
	<?php foreach($errors as $e): ?>
		<p style="color:red"> <?php echo $e; ?> </p>
	<?php endforeach?>
<?php endif;?>


</body>
[/php]

Use code tags please, it is the button that says “php” over the smiley faces.

Where are the values for the dropdown coming from? Here:
[php] $peatused=array(“esimene”, “teine”, “kolmas”, “neljas”, “viies”, “kuues”, “seitsmes”, “kaheksas”);[/php]

“Vali peatus” is not in that list. If you want it to be the default, You need to add it to location 0 in the array.

This:

[php]if($i == array_search($_POST['peatus'], $peatused)) { [/php]

Will likely cause a warning. If the form hasn’t been submitted, that value is not set.

I personally like using foreach statements instead of for loops for they look neater in my opinion.

[php]foreach ($peatused as $key => $value) {
if (in_array($_POST[‘peatus’], $peatused) {
$selected = ‘selected’;
} else {
$selected = “”;
}
echo “<option value=”$peatused[$key]" “.$selected.”>$peatused[$key]";
} [/php]

I can’t guarantee that this work for I haven’t tested it. Just a different way of doing it.

Won’t work, or actually it will work too well. It loops through the array, so every value will be in the array, resulting in every item having a selected attribute.

This should work the same though:

[php]foreach ( $peatused as $key => $value ) {
if ($key == array_search($_POST[‘peatus’], $peatused) )
{
$selected = ‘selected’;
} else
{
$selected = “”;
}
echo “<option value=”$key" " . $selected . “>$value”;
} [/php]

Thank you, astonecipher and Strider64! Your comments were helpful!

What is the purpose of range? I am seeing an out of index issue.

Thank you for the comment! I’ll try to deal with that!

I’m afraid I cannot fix this out-of-index-problem…
Could anyone provide me with a hint that could help me with that matter?

[ In the form there are two fields

  1. in the first field there are bus stops - first, second,…, eight.
  2. in the second field the user must enter a number (“range”). How many nearby bus stops would she or he like to see before and after the selected bus-stop. ]
    The “key”-code starts from the line 17-th.

[php]

Peatused <?php $peatused=array("esimene", "teine", "kolmas", "neljas", "viies", "kuues", "seitsmes", "kaheksas"); if (!empty ($_POST["peatus"]) && !empty($_POST["range"]) ) { $key = array_search($_POST["peatus"], $peatused); for ($i=$key-($_POST["range"]); $i<=$key+($_POST["range"]); $i++) { echo $peatused[$i]."
"; } } else { //do nothing } ?>

Vali peatus ja mitu peatust selle ümber kuvada

<?php echo " "; if(!empty($_POST['peatus'])){ echo "Vali peatus"; for( $i = 0; $i < count($peatused); $i++ ) { if($i == array_search($_POST['peatus'], $peatused)) { $selected = "selected"; }else{ $selected = ""; } echo "$peatused[$i]"; }
        } else {
             echo "<option value=\"\" selected>Vali peatus</option>";
             for ($i = 0; $i < count($peatused); $i++)  echo "<option value=\"$peatused[$i]\">$peatused[$i]</option>";
        }
                 
    echo "</select>";

?>

		</select>
		<br/>
		<input type="number" name="range" <?php if(!empty($_POST["range"]) && is_numeric($_POST["range"])) 
	echo "value=\"".htmlspecialchars($_POST["range"])."\" "; ?> /><br/>
		<input type="submit" value="kuva"/>
	</form>
	
<?php 
if(!empty($_POST)) { // vorm esitati
	$errors=array();
	if(!empty($_POST["peatus"])) {
	//do sth 
	} else {
	$errors[]="Palun vali peatus!";
	}
	if(!empty($_POST["range"])) {
	//do sth 
	} else {
	$errors[]="Palun vali peatuste arv!";
	}		
}
	?>


<?php if (!empty($errors)): ?>
	<?php foreach($errors as $e): ?>
		<p style="color:red"> <?php echo $e; ?> </p>
	<?php endforeach?>
<?php endif;?>



</body>

[/php]

Here you are getting into some possibly complicated logic.

You don’t have anything linking where the bus stops are. If they are listed in order… You could do something like,

[php]
$_POST [‘peatus’] = “teine”;
$range = 3;

foreach ( $peatused as $key => $value ) {
if ($key == array_search ( $_POST [‘peatus’], $peatused )) {
echo “Key : $key, $value
”;
// $range = $range - $key;
echo “Range : $range
”;
if ($range - $key >= 0) {
echo “Range is greater than 0
”;
while ( $range != 0 ) {
echo "{$peatused [$range]}
";
$range --;
}
} else {
echo “Range is equal or less than 0
”;
$index = 0;
while ( $range <= $key ) {
echo "{$peatused [$range]}
";
$range ++;
}
}
}[/php]

Which outputs like this:

Key : 1, teine Range : 3 Range is greater than 0 neljas kolmas teine

Yes, that would be the case. Thank yo very much, astonicipher!

Sponsor our Newsletter | Privacy Policy | Terms of Service