I want to get the options selected, i tried many ways and when i select an option from the list and after pressing submit the selectmenu points to the first element, which makes the option that i selected not actually selected. And i can't send what the user have chosen.
<?php
$operatorsAndPackages = $_SESSION['outputOperatorsAndPackages'];
$json_a = json_decode($operatorsAndPackages,true);
?>
<label>Paquet : Opérateur : Montant</label><br/>
<select id="recharge_operator" name="recharge_operator">
<?php foreach ($json_a as $value): ?>
<?php echo $value; ?>
<option value="<?php echo $value[paquet_id] . ":" . $value[code_operateur] . ":" . $value[montant_recharge]; ?>"><?php echo $value[paquet_id] . ":" . $value[code_operateur] . ":" . $value[montant_recharge]; ?></option>
<?php endforeach ?>
</select>
You need to look at the submitted value (I'll assume this is a POST operation in my code sample) and compare it to each assembled value in the loop to determine if the current is the same as what was submitted. If it is, you set the selected attribute of your option element.
Lines 13 and 16 of the following code sample are the most pertinent changes.
<?php
$operatorsAndPackages = $_SESSION['outputOperatorsAndPackages'];
$json_a = json_decode($operatorsAndPackages,true);
?>
<label>Paquet : Opérateur : Montant</label><br/>
<select id="recharge_operator" name="recharge_operator">
<?php
foreach ($json_a as $value)
{
$value = $value['paquet_id'] . ":" . $value['code_operateur'] . ":" . $value['montant_recharge'];
$selected = ($value === $_GET['recharge_operator']);
$html_safe_value = htmlentities($value);
?>
<option value="<?php echo $html_safe_value; ?>"<?php if ($selected) {?> selected="selected"<?php } ?> >
<?php echo $html_safe_value; ?>
</option>
<?php
}
?>
</select>
I have a quick demo with source code available at: http://jaaulde.com/test_bed/SO_Lotus91/
Related
I am currently working with XML files in PHP and would like to be able to load in a specific XML file based on a selection from a dropdown list.
This is code I have been trying so far with help from some other stackoverflow posts about this method:
<html>
<?php
$submittedValue = "";
$value0 = "Route 7602";
$value1 = "Route 7603";
if (isset($_POST["busroutes"])) {
$submittedValue = $_POST["busroutes"];
}
?>
<form action="" name="busroutes" method="post">
<select name="busroutes" onchange="this.form.submit()">
<option value = "<?php echo $value0; ?>"<?php echo ($value0 == $submittedValue)?" SELECTED":""?>><?php echo $value0; ?></option>
<option value = "<?php echo $value1; ?>"<?php echo ($value1 == $submittedValue)?" SELECTED":""?>><?php echo $value1; ?></option>
</select>
<noscript><input type="submit" value="Submit"></noscript>
</form>
<?php
if($submittedValue = $value0){
$urlbus = ("https://data.dublinked.ie/cgi-bin/rtpi/realtimebusinformation?stopid=7602&format=xml");
}
elseif($submittedValue = $value1){
$urlbus = ("https://data.dublinked.ie/cgi-bin/rtpi/realtimebusinformation?stopid=7603&format=xml");
}
else{
echo "No XML file loaded";
}
$dublinbus_array = simplexml_load_file($urlbus);
echo '<pre>';
print_r($dublinbus_array);
echo '</pre>';
?>
</html>
The first XML file with the stopid of 7602 is displaying correctly but the XML file with stopid of 7603 isn't. I have a feeling im close to something but just can't get over the line.
Any help would be greatly appreciated.
Use "===" for strict comparison not just "=" .
Learn more details here http://www.programmerinterview.com/index.php/php-questions/difference-between-and-in-php/
I want to have dropdown selected option selected after submit/refresh page. I search for other examples but no one works on my code.
How to retain selected value after submit/refresh with this code?
<form name="test" action="test.php?id=<?php echo $row["id"]; ?>" method="post">
<select id="test_email" name="test_email">
<option value="">...select</option>
<?php
$sql2 = "SELECT test_id, test_email FROM test WHERE status='Act'";
$res = $db->query($sql2);
if ($res->num_rows > 0) {
while($row1 = mysqli_fetch_array($res)) {
?>
<option value="<?php echo $row1['test_email'];?>-<?php echo $row1['test_id'];?>"><?php echo $row1['test_id'];?></option>
<?php
}
}
?>
</select>
Solved this way:
Because I could not pass $_POST['test_email'] with <?php echo $row1['test_email'];?>-<?php echo $row1['test_id'];?>
Because I use explode on $_POST['test_email']
I make one more post (insert in db) $test_temp = trim(mysqli_real_escape_string($db, $_POST['test_email'])); to have my dropdown value (whole string) in db.
I added hidden input in my form <input id="test_temp" type="hidden" name="test_temp" value="<?php echo $row["test_temp"]; ?>">
And changed select option line to <option value="<?php echo $row1['test_email'] . '-' . $row1['test_id']; ?>"<?php if($row1['test_email'] . '-' . $row1['test_id'] == $row['test_temp']) echo ' selected="selected"' ; ?>><?php echo $row1['test_id'];?></option>.
Perhaps this could be simpler but it works like a charm.
#roberto06: Thank you for your guidance.
You need to add the selected="selected" attribute on the option matching your $_POST value.
You might also want to concatenate <?php echo $row1['test_email'];?>-<?php echo $row1['test_id'];?> into <?php echo $row1['test_email'] . '-' . $row1['test_id']; ?>, but that's up to you.
Here's a solution (I assumed that $POST['test_email'] has to be equal to $row1['test_email'] . '-' . $row1['test_id'] in order for the condition to be matched). I'm using a shorthand if...else here, but you could also use a classic if :
<form name="test" action="test.php?id=<?php echo $row["id"]; ?>" method="post">
<select id="test_email" name="test_email">
<option value="">...select</option>
<?php
$sql2 = "SELECT test_id, test_email FROM test WHERE status='Act'";
$res = $db->query($sql2);
if ($res->num_rows > 0) {
while($row1 = mysqli_fetch_array($res)) {
?>
<option value="<?php echo $row1['test_email'] . '-' . $row1['test_id']; ?>"<?php echo (isset($_POST['test_email']) && $row1['test_email'] . '-' . $row1['test_id'] == $_POST['test_email']) ? ' selected="selected"' : ''; ?>><?php echo $row1['test_id'];?></option>
<?php
}
}
?>
</select>
</form>
my problem is to show previously selected in a while loop. I found a way to do it in not-loop setup, but a loop is a problem for me.
The following code give me a drop down of countries (and country code):
<?php
foreach($countries as $key => $value) { ?>
<option value="<?= $value . ' ' . $key ?>" title="<?= htmlspecialchars($value) ?>"><?= htmlspecialchars($value) ?></option>
<?php } ?>
And it works good. But imagine someone edit their country, thus I would love to show previously selected country. Of course I have a variable with the specific previously selected country... Thanks.
<?php
$preselected = 'whatever_previous_selected';
foreach($countries as $key => $value) { ?>
<option value="<?php $value; ?>" title="<?= htmlspecialchars($value) ?>" <?php if($preselected == $value) {echo "selected='selected'"; }?>>
<?= htmlspecialchars($value) ?>
</option>
<?php } ?>
try this:
<?php
foreach($countries as $key => $value) { ?>
<?php if ($previousCountry == $value) ?>
<option selected value="<?= $value . ' ' . $key ?>" title="<?= htmlspecialchars($value) ?>"><?= htmlspecialchars($value) ?></option>
<?php else ?>
<option value="<?= $value . ' ' . $key ?>" title="<?= htmlspecialchars($value) ?>"><?= htmlspecialchars($value) ?></option>>
<?php } ?>
Not sure with the syntax but you should detect if the value is equal to the previous country then you put selected attribute on it.
Use selected='selected' when you get id in edit.
<?php
$id = 1; // In edit you get some value here
foreach($countries as $key => $value) {
$selected = (isset($id) && $id == $key) ? "selected='selected'" : "";
?>
<option value="<?= $value . ' ' . $key ?>" title="<?= htmlspecialchars($value) ?>" <?php echo $selected ?>><?= htmlspecialchars($value) ?></option>
<?php } ?>
try this:
<?php
$prevSelectedCountry;
foreach($countries as $key => $value) { ?>
<option <?php echo ($prevSelectedCountry == $value . ' ' . $key)?"selected='selected'":"" ?> value="<?= $value . ' ' . $key ?>" title="<?= htmlspecialchars($value) ?>"><?= htmlspecialchars($value) ?></option>
<?php } ?>
Here goes my first post.
I am currently pulling two fields out from my MySQL database with a fetch all and am trying to get the data in those fields to become options in a listbox.
This is my code:
<fieldset class="contact">
<legend>Select a Band</legend>
<!-- Drop List -->
<select id="lst1" name="lst1" tabindex="281" size="1">
<?php
foreach ($bands as $band) {
$name = $band["fldBand"];
$id = $band["pkID"];
$options .= '<option value="' . $id . '">' . $name . '</option>';
}
echo $options;
?>
</select>
and here is the result of echoing out $options:
<option value="1">Rise Against</option><option value="2">Alter Bridge</option><option value="3">Falling In Reverse</option><option value="4">Saosin</option><option value="5">Pennywise</option><option value="6">The Killers</option><option value="7">Thrice</option><option value="8">Four Year Strong</option>
I want to have the foreach loop print out the code that will be the options for my listbox lst1 but it is currently not working. Any ideas as to why?
<?php
foreach ($bands as $band) {
$name = $band["fldBand"];
$id = $band["pkID"]; ?>
<option value = "<?php echo $id; ?>"><?php echo $name; ?></option>
<?php } ?>
Try this code. In your code '<' and '>' makes html entity format. So html does't accept it as a tag.
<select id="lst1" name="lst1" tabindex="281" size="1">
<?php
foreach ($bands as $band) {
$name = $band["fldBand"];
$id = $band["pkID"];
$options .= '<option value="' . $id . '>' . $name . '</option>';
}
echo $options;
?>
</select>
i am selecting an entry from the database to be edited on php/html. i can call the values of a table from the database and put it in a multiple tag.
$job_exp_tags = $row[16];//value from another table
$job_tags = explode(',',$job_exp_tags);//array to be put "selected" on the multiple select tag
$sql_2 = "SELECT _id, job_name from job_tags";
$sel_2 = mysql_query($sql_2);
$array = array();
while($row_new = mysql_fetch_assoc($sel_2)){
$array[] = $row_new;
}
<tr>
<td>Job Experience:<br>
(Hold ctrl to select multiple)</td>
<td><select name = 'job_tags[]' multiple>
<?php
foreach($array as $value){ ?>
<option value ='<?php echo $value['_id']; ?>'> <?php echo $value['job_name']; ?> </option>
<?php
}
?>
</select> </td>
</tr>
my problem is how can i put selected values to it from the explode statement
edit:
i've solved the problem, thanks anyway guys!
<option value ="<?php echo $value['_id']; ?>" <?php echo in_array($value['_id'], $job_tags) ? 'selected="true"' : null; ?>><?php echo $value['job_name']; ?></option>
Just check if its in your array, if so, set it selected:
foreach($array as $value){ ?>
$selected = in_array($value, $job_tags) ? ' selected ' : '';
/* Or [selected="selected"] if you dont use html5 yet (which you should) */
<option value ='<?php echo $value['_id']; ?>' <?php echo $selected; ?>> <?php echo $value['job_name']; ?> </option>
<?php
}
Your code can be simplified though:
foreach($array as $value){
$selected = in_array($value, $job_tags) ? ' selected="selected" ' : '';
?>
<option value="<?=$value['_id']?>" <?=$selected?> > <?=$value['job_name']?> </option>
<?php
}
I changed the quotes arround the value to doubles, not really a rule, but it is a good practice to do so. The other change is the short echo. Small demo, both do the same:
<php $var = 'foorbar'; ?> <!-- A bit weird, but this is demo-purpose -->
<span><?php echo $var; ?></span>
<span><?=$var?></span>
You could look for the value in the string $job_exp_tags and if found set the selected property.
foreach($array as $value){
$selected = strpos($job_exp_tags, $value) ? 'selected' : '';
echo '<option value="'.$value['_id'].'" '.$selected.'>'.$value['job_name'].'</option>';
}