Store dropdown choices as set array elements into specific variables - php

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

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 remove previously selected options in PHP form with multiple select dropdowns

I am creating a game launch form with four dropdown selection boxes. Each box will be used to select a player (players are pulled from the user's friend's list in my mysql database). I am using PHP to dynamically populate the list of options (potential players).
Each of the items have a unique name and id.
Is there a way to remove options that have already been selected from the other dropdowns? I can find examples in javascript on here, but I haven't found one in PHP.
I've inserted the form code I'm using. $option is just an array of usernames. (Note: my if statement was a complete guess. I'm not a seasoned coder by any means.)
<form method="POST" action="game.php">
<select id="1stPlayer" name="1stPlayer" required>
<option value="" disabled="disabled" selected="selected">Please select Player 1 (Team 1)</option>
<?php
$first = $_GET["1stPlayer"];
$second = $_GET["2ndPlayer"];
$third = $_GET["3rdPlayer"];
$fourth = $_GET["4thPlayer"];
echo implode(",",array_unique($option));
foreach ($option as $row) {
$value = $row;
if ($value != $second && $value != $third && $value != $fourth ) {
echo '<option value="'. $value .'">'. $value .'</option>';
};
};
?>
</select>
<select id="2ndPlayer" name="2ndPlayer" required>
<option value="" disabled="disabled" selected="selected">Please select Player 1 (Team 1)</option>
<?php
$first = $_GET["1stPlayer"];
$second = $_GET["2ndPlayer"];
$third = $_GET["3rdPlayer"];
$fourth = $_GET["4thPlayer"];
echo implode(",",array_unique($option));
foreach ($option as $row) {
$value = $row;
if ($value != $first && $value != $third && $value != $fourth ) {
echo '<option value="'. $value .'">'. $value .'</option>';
};
};
?>
</select>
<select id="$third" name="$third" required>
<option value="" disabled="disabled" selected="selected">Please select Player 1 (Team 1)</option>
<?php
$first = $_GET["1stPlayer"];
$second = $_GET["2ndPlayer"];
$third = $_GET["3rdPlayer"];
$fourth = $_GET["4thPlayer"];
echo implode(",",array_unique($option));
foreach ($option as $row) {
$value = $row;
if ($value != $second && $value != $first && $value != $fourth ) {
echo '<option value="'. $value .'">'. $value .'</option>';
};
};
?>
</select>
<select id="4thPlayer" name="4thPlayer" required>
<option value="" disabled="disabled" selected="selected">Please select Player 1 (Team 1)</option>
<?php
$first = $_GET["1stPlayer"];
$second = $_GET["2ndPlayer"];
$third = $_GET["3rdPlayer"];
$fourth = $_GET["4thPlayer"];
echo implode(",",array_unique($option));
foreach ($option as $row) {
$value = $row;
if ($value != $second && $value != $third && $value != $first ) {
echo '<option value="'. $value .'">'. $value .'</option>';
};
};
?>
</select>
<input type="submit" name="startGame" id="single_game_button" value="Start Game">
</form>

Getting Select Options From Array

My code contains a select box for selecting multiple options.
<select name="menu-item-visibility[<?php echo $item_id; ?>][]" id="edit-menu-item-visibility-<?php echo $item_id; ?>" class="chzn-select" multiple="true"
<option value="AF" <?php $val = get_post_meta( $item_id, 'locations', true ); if ( is_array( $val ) ) { if ( in_array( "AF", $val ) ) { echo "selected='selected'"; } else { echo ""; } } ?> >Afghanistan</option>
<option value="AL" <?php $val = get_post_meta( $item_id, 'locations', true ); if ( is_array( $val ) ) { if ( in_array( "AL", $val ) ) { echo "selected='selected'"; } else { echo ""; } } ?> >Albania</option>
...
</select>
The problem is I'm writing the same code over and over and the select box alone makes up more than 50% of the lines of code. Seems kind of wasteful. I'm wondering if I can apply some abstraction by looping through an array of the countries stored in a separate file.
$countries = array (
'AF' => 'Afghanistan',
'AL' => 'Albania',
...
)
Any insight into how this might be accomplished in this case?
You can try -
<select name="menu-item-visibility[<?php echo $item_id; ?>][]" id="edit-menu-item-visibility-<?php echo $item_id; ?>" class="chzn-select" multiple="true"
<?php
$vals = get_post_meta( $item_id, 'locations', true );
foreach($countries as $key => $value) { // Loop through countries
?>
<option value="<?php echo $key;?>"
<?php echo (is_array( $vals ) && in_array( $key, $vals )) ? 'selected' : ''; ?> >
<?php echo $value;?>
</option>
<?php
}
?>
</select>
Also you can do like this, Its different from your code
<?php
$countries = ['de' => 'Germany','pl' => 'Poland','fr' => 'France'];
echo "<select>";
echo "<option value=''>Select Country</option>";
$country = "pl";
foreach ($countries as $code => $name) {
echo '<option value="' . $code . '" ' . ($country == $code ? 'selected="selected"' : null) . '>' . $name . '</option>';
}
echo "</select>";
?>

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>

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