PHP foreach doesn't show the first array value - php

echo 'SSH Timeout: <select name="ssh_timeout"';
$time = array('1', '5', '15', '30', '60');
foreach ($time as $value) {
if (15 == $value) {
echo "<option value='$value' selected>$value seconds</option>";
}
else {
echo "<option value='$value'>$value seconds</option>";
}
}
echo '</select>';
That code doesn't show the first value which is 1 but instead it starts from 5 to 60. How do I fix it ?

You should close the opening <select tag, otherwise the <option value='1' gets inside the <select and the browser will not render it.
This is the fix:
echo 'SSH Timeout: <select name="ssh_timeout">';
$time = array('1','5','15','30','60');
foreach ($time as $value) {
if (15 == $value) {
echo "<option value='$value' selected>$value seconds</option>";
}
else{
echo "<option value='$value'>$value seconds</option>";
}
}
echo '</select>';

Related

Dropdown value stays selected

I have a dropdown and I want the input selected after the page refresh or post. So every post has the same $hoogte_array.
<table><form action="index.php" method="post">
<tr><th>Hoogte: <select name="hoogte">
<?
$hoogte_array[1]= 63;
$hoogte_array[2]= 103;
$hoogte_array[3]= 123;
$hoogte_array[4]= 153;
$hoogte_array[5]= 173;
$hoogte_array[6]= 203;
$kleur_array[1] = "groen";
$kleur_array[2] = "blauw";
foreach ($hoogte_array as $key => $valuehoogte)
{
echo "<option value='".$key."''>".$valuehoogte."</option>";
}
?>
</select></th>
<th>Kleur: <select name="kleur">
<?
foreach ($kleur_array as $key => $valuekleur)
{
echo "<option value='".$key."''>".$valuekleur." </option>";
}
?>
foreach ($hoogte_array as $key => $valuehoogte)
{
$hoogte = (isset($_POST['hoogte']))?$_POST['hoogte']:"1";
$sel = ($hoogte == $key)? 'selected="selected"' : '';
echo "<option value='".$key."'' ".$sel.">".$valuehoogte."</option>";
}

How to make selected drop down option stay selected after submit

I am trying to make the select box sticky that is located in the first foreach loop:
I believe there should be an if statement located inside the <option> tag like below:
if($selectedMake==$key){
echo selected='selected';
}
echo "<option im not sure how to properly enter it in here?>$key</option>"
// Start of code below:
$selectedMake = $_POST['make'];
$cars = array(
'Toyota'=>array(
'Corolla'=>array(
'image'=>'corolla.png',
'colour'=>'blue',
'transmission'=>'manual',
'doors'=>'2'
),
'Highlander'=>array(
'image'=>'highlander.png',
'colour'=>'silver',
'transmission'=>'auto',
'doors'=>'4'
),
),
'Mazda'=>array(
'RX7'=>array(
'colour'=>'blue',
'transmission'=>'manual',
'doors'=>'2'
),
'MX-5'=>array(
'colour'=>'red',
'transmission'=>'manual',
'doors'=>'2'
)
)
);
echo '<form method="post" action="cars.php">';
echo '<select name="make">';
foreach ($cars as $key => $value) {
echo "<option>$key</option>"; // This option tag needs to be made sticky
}
echo '</select>
<input type="submit" name="submit">
</form>
';
if (isset($_POST['submit'])) {
$selectedMake = $_POST['make'];
echo "<h1>$selectedMake</h1>";
foreach ($cars as $key => $value) {
if ($selectedMake == $key) {
foreach ($value as $key => $value) {
echo "<b>$key</b> <br>";
foreach ($value as $key => $value) {
if ($key == 'image') {
echo '<img src="imgs/'.$value.'" width="150px">';
} else {
echo "<li>$key: $value</li>";
}
}
echo "<br>";
}
}
}
} else {
echo "Not clicked";
}
echo '<pre>',print_r($cars),'</pre>';
?>
If by sticky, you mean selected if it matches the value you have then:
echo "<option".($selectedMake==$key ? " selected" : "").">$key</option>"
Ah, I think I know what you mean. You want something like this...
<select name="make">
<?php
foreach (array_keys($cars) as $key) :
$selected = $key === $selectedMake ? ' selected' : '';
?>
<option<?= $selected ?>><?= htmlspecialchars($key) ?></option>
<?php endforeach ?>
</select>

Echo selected value first

I need to echo the selected value first in a form when user wants to make an update. I tried several variants:
1)
<?php
$opt= array('1' => 'opt1', '2' => 'opt2', '3' => 'opt3') ;
echo '<select name="up_opt" >' ;
foreach ($opt as $i => $value) {
echo "<option value=\"$i\"";
if ($_REQUEST['up_opt'] == $i)
{
echo "selected" ;
}
echo ">$opt[$i]</option>" ;
}
echo '</select>' ;
?>
2)
<?php $opt= array('1' => 'opt1', '2' => 'opt2', '3' => 'opt3') ;
$edu = $_REQUEST['edu'];
<select name="up_opt">
<?php foreach ( $opt as $i=>$opt ) : ?>
<option value="<?php echo $i?>" <?php echo $i == $edu ? 'selected' : ''?>><?php echo $opt ?></option>
<?php endforeach; ?>
</select>
3)
<select name="up_opt">
<option value="1" <?php if ($_GET['1'] == 'option1') { echo 'selected'; } ?>>Opt1</option>
<option value="2" <?php if ($_GET['2'] == 'option2') { echo 'selected'; } ?>>Opt2</option>
<option value="3" <?php if ($_GET['3'] == 'option3') { echo 'selected'; } ?>>Opt3</option>
</select>
None of these variants echoes the checked value first. Can someone help me, tell me what is wrong or give me another variant?
Variant 3 is ok (but I'd rather use a loop instead of hard-coded options). Your mistake is that you compare 'option1', 'option2' and so one when your real values are '1', '2', '3'. Also as #ElefantPhace said, don't forget about spaces before selected, or you'll get invalid html instead. So it would be this:
<select name="up_opt">
<option value="1" <?php if ($_GET['up_opt'] == 1) { echo ' selected="selected"'; } ?>>Opt1</option>
<option value="2" <?php if ($_GET['up_opt'] == 2) { echo ' selected="selected"'; } ?>>Opt2</option>
<option value="3" <?php if ($_GET['up_opt'] == 3) { echo ' selected="selected"'; } ?>>Opt3</option>
</select>
With loop:
<?php
$options = array(
1 => 'Opt1',
2 => 'Opt2',
3 => 'Opt3',
);
?>
<select name="up_opt">
<?php foreach ($options as $value => $label): ?>
<option value="<?php echo $value; ?>" <?php if ($_GET['up_opt'] == 1) { echo ' selected="selected"'; } ?>><?php echo $label; ?></option>
<?php endforeach ?>
</select>
Here is all three of your variants, tested and working as expected. They are all basically the same, you were just using the wrong variable names, and different ones from example to example
1)
<?php
$opt= array('1' => 'opt1', '2' => 'opt2', '3' => 'opt3') ;
echo '<select name="up_opt">';
foreach ($opt as $i => $value) {
echo "<option value=\"$i\"";
if ($_POST['up_opt'] == $i){
echo " selected";
}
echo ">$value</option>";
}
echo '</select>';
?>
2) uses same array as above
$edu = $_POST['up_opt'];
echo '<select name="up_opt">';
foreach ( $opt as $i => $value ){
echo "<option value=\"$i\"", ($i == $edu) ? ' selected' : '';
echo ">$value</option>";
}
echo "</select>";
3)
echo '<select name="up_opt">';
echo '<option value="1"', ($_POST['up_opt'] == '1') ? 'selected':'' ,'>Opt1</option>';
echo '<option value="2"', ($_POST['up_opt'] == '2') ? 'selected':'' ,'>Opt2</option>';
echo '<option value="3"', ($_POST['up_opt'] == '3') ? 'selected':'' ,'>Opt3</option>';
echo '</select>';

How to embed if statement inside echo [duplicate]

This question already has answers here:
if block inside echo statement?
(4 answers)
Closed 11 months ago.
I'm gettin' a headache on that. I need to put if statement inside an echo (this echo is in a function, it's for a form submit actually)
Here is an example on a partial of my code. In this situation, how can I put theses if statement inside my echo??
<?php echo '<td><select id="depuis" name="depuis">
<option value=\'4\' <?php if(isset($_POST[\'depuis\']) && $_POST[\'depuis\'] == \'4\'){ echo \'selected\'; } else { echo ''; } ?> ></option>
<option value=\'1\' <?php if(isset($_POST[\'depuis\']) && $_POST[\'depuis\'] == \'1\'){ echo \'selected\'; } else { echo ''; } ?> >2 ans et moins</option>
<option value=\'2\' <?php if(isset($_POST[\'depuis\']) && $_POST[\'depuis\'] == \'2\'){ echo \'selected\'; } else { echo ''; } ?> >2 à 5 ans</option>
<option value=\'3\' <?php if(isset($_POST[\'depuis\']) && $_POST[\'depuis\'] == \'3\'){ echo \'selected\'; } else { echo ''; } ?> >5 ans et plus</option>
</select>
</td>'
; ?>
Everything is php so need to use more than the first <?php Finish each echo before checking with if. Like this:
<?php
echo '<td><select id="depuis" name="depuis">
<option value="4"';
if(isset($_POST['depuis']) && $_POST['depuis'] == '4') {
echo ' selected';
}
echo ' >Something here maybe?</option>...etc
Use an inline if statement:
echo 'Based on your score, you are a ',($score > 10 ? 'genius' : 'nobody');
This would work - although I'm sure it could be streamlined:
<?php
$out = '
<td>
<select id="depuis" name="depuis">
<option value="4"
';
if(isset($_POST['depuis']) && $_POST['depuis'] == '4'){
$out .= 'selected';
}
$out .= '
></option>
<option value='1'
';
if(isset($_POST['depuis']) && $_POST['depuis'] == '1'){
$out .= 'selected';
}
$out .= '
>2 ans et moins</option>
<option value=\'2\'
';
if(isset($_POST['depuis']) && $_POST['depuis'] == '2'){
$out .= 'selected';
}
$out .= '
>2 à 5 ans</option>
<option value='3'
';
if(isset($_POST['depuis']) && $_POST['depuis'] == '3'){
$out .= 'selected';
}
$out .= '
>5 ans et plus</option>
</select>
</td>
';
echo $out;
?>
Meilleurs voeux...
You will want to use the a ternary operator which acts as a shortened IF/Else statement:
echo '<option value="'.$value.'" '.(($value=='United States')?'selected="selected"':"").'>'.$value.'</option>';
You shouldn't escape the array keys in the string
if(isset($_POST[\'depuis\']) ...
Should be
if(isset($_POST['depuis']) && ...
Also you could clean up the the generation of the options with a array and a loop
$options = array(
'label1' => 1,
'label2' => 2,
'label3' => 3,
'label4' => 4
);
$value = (isset($_POST['depuis'])) ? $_POST['depuis'] : 0;
foreach ($options as $label => $optionValue) {
$selected = ($optionValue == $value) ? ' selected' : '';
printf('<option value="%s"%s>%s</option>', $optionValue, $selected, $label);
}
Not sure you can do that like you're asking. You'd just need to use regular if/else if block and output the outcome separately.
<?php
echo("<td><select id=\"depuis\" name=\"depuis\">");
echo("<option value='4'");
if(isset($_POST['depuis']) && $_POST['depuis'] == '4'){ echo("selected"); }
echo("></option>");
[ ...etc... ]
?>

How do I display result on the same page if I meet condition in php?

I want to display a checkbox on the same page if the second option is chosen, could you help me with the code please.
Here is my current code:
<form action="form1.php" method="post">
<?php
$company = array (1 => 'One', 'Two', 'Three');
echo '<fieldset>
<select name="companys">';
foreach ($company as $key => $value) {
echo "<option value=\"$key\">$value</option>\n";
}
echo '</select>';
if ($company == 2) {
echo'<p><input type="checkbox" name="tandc" value="terms" />I accept the terms and conditions
</p>';
} else {
echo 'OK';
};
echo '</fieldset>';
?>
</form>
Assuming you post the form somewhere, and the current page is form1.php.
<form action="form1.php" method="post">
<?php
$company = array(1 => 'One', 2 => 'Two', 3 => 'Three');
echo '<fieldset>
<select name="companys">';
foreach ($company as $key => $value) {
echo "<option value=\"$key\">$value</option>\n";
}
echo '</select>';
if (isset($_POST['companys']) && $_POST['companys'] == 2) {
echo'<p><input type="checkbox" name="tandc" value="terms" />I accept the terms and conditions</p>';
} else {
echo 'OK';
}
echo '</fieldset>';
?>
</form>
***if ($company == 2) {***
$company is defined as array but you have mentioned as string. please check.
Try this:
<form action="#" method="post">
<?php
$company = array (1 => 'One', 'Two', 'Three');
echo '<fieldset>
<select name="companys">';
foreach ($company as $key => $value) {
echo "<option value=\"$key\" onclick='this.form.submit()'>$value</option>\n";
}
echo '</select>';
if ($_POST['companys'] == 2) {
echo'<p><input type="checkbox" name="tandc" value="terms" onclick="this.form.submit()"/>I accept the terms and conditions
</p>';
} else {
echo 'OK';
};
echo '</fieldset>';
?>
</form>
You do not need to submit form to just show an element if dropdown value matches condition you can simply use javascript function to make it possible see example code below
<form action="form1.php" method="post">
<?php
$company = array(1 => 'One', 2 => 'Two', 3 => 'Three');
echo '<fieldset>
<select name="companys" onchange="check_option(this.value);">';
foreach ($company as $key => $value) {
echo "<option value=\"$key\">$value</option>\n";
}
echo '</select>';
echo'<p><input type="checkbox" id="terms" name="tandc" value="terms" style="display:none;"/>I accept the terms and conditions</p>';
echo '</fieldset>';
?>
</form>
<script>
function check_option(val)
{
if(val=='Two')
{
document.getElementById('terms').style.display='block';
}
}
</script>

Categories