How to make selected drop down option stay selected after submit - php

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>

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>";
}

PHP foreach doesn't show the first array value

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>';

Store dropdown choices as set array elements into specific variables

I have the following html:
<form action="" method="get">
<ul>
<li>location
<select name="locationdo">
<option value="tax_cb">Checkbox</option>
<option value="tax_radio">Radio</option>
<option value="tax_dd">Dropdown</option>
</select>
</li>
<li>genre
<select name="genredo">
<option value="tax_cb">Checkbox</option>
<option value="tax_radio">Radio</option>
<option value="tax_dd">Dropdown</option>
</select>
</li>
<li>studio
<select name="studiodo">
<option value="tax_cb">Checkbox</option>
<option value="tax_radio">Radio</option>
<option value="tax_dd">Dropdown</option>
</select>
</li>
</ul>
<p><input type="submit" value="submit" name="submit" /></p>
</form>
What I want is to have array elements from $which_tax_array stored in separate variables based on choices that were made. Hopefully the code will explain better what I want to achive (but it doesn't work as I wanted it to):
if (isset($_GET['submit'])) {
$which_tax_array = array('location', 'genre', 'studio');
$what = array();
foreach ($which_tax_array as $key => $tax_name) {
$what[$tax_name] = $_GET[$tax_name.'do'];
foreach ($what as $tax_term => $display_option) {
if ( in_array($what[$tax_name], $what) ) {
$checkboxes = ','.$tax_term;
} elseif ( in_array($what[$tax_name], $what) ) {
$radios .= ','.$tax_term;;
} elseif ( in_array($what[$tax_name], $what) ) {
$dropdowns .= ','.$tax_term;
}
}
}
}
echo 'cb '.$checkboxes.'<br>';
echo 'radio '.$radios . '<br>';
echo 'dd '.$dropdowns.'<br>';
Not sure what you are trying to do. I see a few possible issues -
(1) you are not closing your foreach ($which_tax_array as $key => $tax_name) until the end, so you are executing your next foreach too early.
(2) you have $checkboxes = ','.$tax_term; instead of $checkboxes .= ','.$tax_term; so you are overwriting your $checkboxes instead of appending.
(3) your if ( in_array($what[$tax_name], $what) ) will aways be true, as you are checking if an array value is in its own array, so all your values will be in $checkboxes. I think you want to check if the value equals tax_cb,tax_radio, or tax_dd.
try something like this-
if (isset($_GET['submit'])) {
$which_tax_array = array('location', 'genre', 'studio');
$what = array();
foreach ($which_tax_array as $key => $tax_name) {
$what[$tax_name] = $_GET[$tax_name.'do'];
}
foreach ($what as $tax_term => $display_option) {
if ($what[$tax_name] == 'tax_cb') {
$checkboxes .= ','.$tax_term;
} elseif ($what[$tax_name] == 'tax_radio') ) {
$radios .= ','.$tax_term;;
} elseif ($what[$tax_name] == 'tax_dd') ) {
$dropdowns .= ','.$tax_term;
}
}
echo 'cb '.$checkboxes.'<br>';
echo 'radio '.$radios . '<br>';
echo 'dd '.$dropdowns.'<br>';
}

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>

Foreach php function inside HTML select options

Im a newbie to this forum and have just started coding in php. Need some help. I have the following code
<?php error_reporting(-1);
require_once('/mysql/sql_connect.php');
$q = "SELECT pty.pty_profile_name AS profile FROM pty, users WHERE users.username = 'testaccount' ";
$r = #mysqli_query ($dbc, $q);
$results_array = array();
while($row = mysqli_fetch_assoc($r)) {
array_push($results_array, $row);
echo "<pre>"; print_r($results_array); echo "</pre>"; }
?>
<p>
<form method="post" action="foreach2.php">
<label for="Property Select" class="title">Select Property</label>
<select name="pty_select" >
<?php foreach($results_array as $key => $value){ ?>
<option value="<?php echo $key; ?>"><?php echo $value['profile']; ?></option>
<?php } ?>
</select>
<input type="submit" name="Submit" />
</form>
<?php
if (isset($_POST['Submit'])) {
echo "<pre>"; echo ($_POST['pty_select']); echo "</pre>"; } ?>
The output I get is correct, but it displays the key, eg, 0 or 1 or 2, based on what I select in the form. I need the value output. Eg 0 = Emerton, it outputs "0" instead of Emerton.
If I echo $value['profile'] instead of pty_select, I get the last result of the query all the time. Which in this example would be 2, which is Ambarvale as I believe it just chooses the last row output of the query.
I hope I've made sense. Thanks in advance.
It will obviously echo the key, as you assigned the value of options as $key
if you need the options in the $_POST['pty_select'] use this:
<select name="pty_select" >
<?php foreach($results_array as $key => $value){ ?>
<option value="<?php echo $value['profile'];?>"><?php echo $value['profile']; ?></option>
<?php } ?>
</select>
You mean you need the value what you have used to display it.
Then,
Change to :
<option value="<?php echo $value['profile']; ?>">
<?php echo $value['profile']; ?>
</option>
And now let's go to ideal world :)
Build data pairs database_id => name for options:
$q = "SELECT pty.id, pty.pty_profile_name AS profile FROM pty, users
WHERE users.username = 'testaccount'";
$r = mysqli_query($dbc, $q);
$values = array();
while($r = mysqli_fetch_row($r)) {
$values[$r[0]] = $r[1];
}
Never use # when working with database, why do you want to suppress errors instead of preventing/handling them?
Now you have real database IDs and respective values (in general, using unique IDs are better... if nothing else they have greater entropy - more efficient search). And sice displaying select box is really common in webs, lets:
function selectbox( $values = array(), $attributes = array(), $selected_value = null)
{
// Header
echo '<select';
foreach( $attributes as $key => $val){
echo ' ' . htmlspecialchars($key) . '="' . htmlspecialchars( $val) . '"';
}
echo '>';
// Values
foreach( $values as $key => $val){
echo '<option value="' . htmlspecialchars( $key) .'"';
if( $key === $selected_value){
echo ' selected="selected"';
}
echo '>' . htmlspecialchars( $val) . '</option>';
}
echo '</select>';
}
And now usage :)
<form method="post" action="foreach2.php">
<label for="Property Select" class="title">Select Property</label>
<?php selectbox( $values, array( 'name' => 'pty_select')); ?>
<input type="submit" name="Submit" />
</form>
And what to do with it then?
$id = (int)(isset( $_POST['pty_select']) ? $_POST['pty_select'] : 0);
$name = null;
if( $id && isset( $values[$id])){
$name = $values[$id];
}
Give
<option value="<?php echo $value['profile']; ?>"><?php echo $value['profile']; ?></option>
instead of
<option value="<?php echo $key; ?>"><?php echo $value['profile']; ?></option>
if (isset($_POST['Submit'])) {
echo "<pre>"; echo ($_POST['pty_select']); echo "</pre>"; } ?>
Change it to something like
if(isset($_POST['Submit'])) {
echo $results_array[$_POST['pty_select']]['profile'];
}
Or alternatively use profile option value.

Categories