Populating dynamically generated select fields after submit using PHP value - php

I am trying to keep the answer that the user selected and display it once the form has been submitted and there are errors somewhere. This is so the form is not tedious and the user does not have to keep inserting the same values into the form once it has been submitted.
I have been successful in completing this for options that are not dynamically generated from database values:
<option
<?php if($_POST['condition'] == 'acceptable') {
echo 'selected="selected"';
}?> value="acceptable">Acceptable
</option>
That works fine! Now the problem is how do I do this for this form option?
<option value="select">- Select school -</option>
<?php
$result = mysqli_query($con,"SELECT `name` FROM school");
while($row = mysqli_fetch_array($result)) {
echo '<option value="'.$row['name'].'">'.$row['name'].' School</option>';}?>
</select>
I have tried this so far but I cannot get it to work:
$result = mysqli_query($con,"SELECT `name` FROM school");
while($row = mysqli_fetch_array($result)) {
if($_POST['school'] == '$row["name"]') {
$selected = 'selected="selected"';
}
echo '<option $selected value="'.$row['name'].'">'.$row['name'].' School</option>';}

You don't need to store that "selected" attribute in a variable,
just print it.
Dividing the echo to 2 allows you to insert a condition between them.
Another note: you did you wrapped the variable with '? You don't need to.
echo '<option value="'.$row['name'].'"';
if($_POST['school'] == $row["name"]) {
echo ' selected="selected"';
echo '>'.$row['name'].' School</option>';}?>

Change. In single qoutes echo doesnt interprets $selected as variable :
$result = mysqli_query($con,"SELECT `name` FROM school");
while($row = mysqli_fetch_array($result)) {
if($_POST['school'] == '$row["name"]') {
$selected = 'selected="selected"';
}
echo '<option '.$selected.' value="'.$row['name'].'">'.$row['name'].' School</option>';
}

Related

How to add an additional option to an option form which is filled with a db query

How can I add an extra <option> with a value of all to:
<?php
mysql_connect("localhost", "root","") or die(mysql_error());
mysql_select_db("tnews2") or die(mysql_error());
$query = "SELECT name,id,path FROM categories ORDER BY ID DESC LIMIT 0,6";
$result = mysql_query($query) or die(mysql_error()."[".$query."]");
?>
<select name="categories">
<?php
while ($row = mysql_fetch_array($result))
{
echo "<option value='".$row['path']."'>'".$row['name']."'</option>";
}
?>
</select>
I would be grateful for any suggestions
Edit
echo "<th>Tierart";
$query = $pdo->query("select sp_term from species");
//Abfrage der Tabelle Tierart
echo '<select name="sp_term">';
while ($sql_sp_term = $query->fetch(PDO::FETCH_ASSOC)) {
echo '<option value="all">all</option>';
echo '<option value="'.$sql_sp_term['sp_term'].'">'.$sql_sp_term['sp_term'].'</option>';
}
echo '</select>';
Since you're populating the values of option in a select tag section with a database query doing something as follows would do the trick.
echo '<select name="sp_term">';
while ($sql_sp_term = $query->fetch(PDO::FETCH_ASSOC)) {
echo '<option value="'.$sql_sp_term['sp_term'].'">'.$sql_sp_term['sp_term'].'</option>';
}
echo '</select>';
However, when you want to add an additional item like All as an option, place an explicit option value before the while/after the while depending on your requirement and you'll have the select > option with the new All option added to the options from the database.
echo '<select name="sp_term">';
echo '<option value="all">all</option>';
while ($sql_sp_term = $query->fetch(PDO::FETCH_ASSOC)) {
echo '<option value="'.$sql_sp_term['sp_term'].'">'.$sql_sp_term['sp_term'].'</option>';
}
echo '</select>';

PHP MySQL Populated Select Options making the right one "Selected"

I'm currently outputting a select menu using the following code;
while($row = mysqli_fetch_array($result) ) {
echo '<option value="'.$row['id'].'">'.$row['title'].'</option>';
}
I'm wondering how I can make the one that is selected in the database appear as selected, seeings as the options are dynamically drawn and added?
Edit: I still want the other options that aren't selected to appear in the list so it can be changed.
You try this way:
while($row = mysqli_fetch_array($result) ) {
$selected=$row['id']==$current_id? "selected": ""; //$current_id is which you want to selected..
echo '<option value="'.$row['id'].'" '. $selected.' >'.$row['title'].'</option>';
}
Make an if condition and then use selected Attribute
while($row = mysqli_fetch_array($result) ) {
if (your condition) {
echo '<option value="'.$row['id'].'" selected>'.$row['title'].'</option>';
} else {
echo '<option value="'.$row['id'].'">'.$row['title'].'</option>';
}
}

How to get selected value from dropdown list after submitting form in php

I would like to remain my drop down value which I select for submitting after posting the form. My form posts to the same page.
$query = "SELECT countryName,countryCode FROM tcf_countries";
$result = mysql_query ($query);
echo "Country: <select name='country' value=''>";
while($r = mysql_fetch_array($result)) {
$id = $r['countryCode'];
$cname = $r['countryName'];
echo "<option value=".$id.">".$cname."</option>";
}
echo "</select>"; ?>
Remove your current echo inside the loop and replace it with the following:
if($_POST["country"]==$id)
echo "<option value='".$id."' selected='selected'>".$cname."</option>";
else
echo "<option value='".$id."' >".$cname."</option>";
This will check if the current option being displayed is the one that was submitted and it will select it in that case.
If I understand what you are looking for correctly you need to use the $_POST value of your select to set the selected item...
$query = "SELECT countryName,countryCode FROM tcf_countries";
$result = mysql_query ($query);
$country = '';
echo "Country: <select name='country'>";
while($r = mysqli_fetch_array($result)) {
$id = $r['countryCode'];
$cname = $r['countryName'];
echo "<option value=".$id;
echo ($_POST["country"]==$id) ? ' selected="SELECTED"' : '';
echo ">".$cname."</option>";
}
echo "</select>"; ?>
Setting selected="SELECTED" for the $id that matches $_POST['country'] will make it the selected item in your dropdown.
And, get rid of mysql* functions and use mysqli* functions instead...

How do I show the selected value from the dropdown [duplicate]

This question already has answers here:
Showing the selected value in Drop down select using PHP
(4 answers)
Closed 8 years ago.
I have a form and here is the code for the dropdown menu. Can you help me with the code to show the selected value after submitting the form? im using php
<?php
$result = mysql_query("SELECT * FROM professional") or die(mysql_error());
if (mysql_num_rows($result)!=0)
{
echo '<select name="professional">
<option value=" " selected="selected">Choose one</option>';
while($row = mysql_fetch_array( $result ))
{
echo '<option value="'.$row['prcno'].'">'.$row['prcno']."\t"."|\t".$row['name']."\t"."|\t".$row['profession'].'</option>';
}
echo '</select>';
}
?>
You will have two Superglobal variables available to you: $_GET and $_POST.
These are arrays, and the key for each is the name of any submitted form element, whether it was POSTED or sent with GET parameters.
See: http://www.php.net/manual/en/reserved.variables.post.php and http://www.php.net/manual/en/reserved.variables.get.php
To display a value would be as simple as:
<?php echo $_POST['form-element-name']; ?>
Suppose you are receiving the selected value in $_POST['professional'] then in your code, you should write as -
<?php
$result = mysql_query("SELECT * FROM professional") or die(mysql_error());
if (mysql_num_rows($result)!=0)
{
echo '<select name="professional"> ';
if(isset($_POST['professional']))
if($_POST['professional']=="")
echo '<option value="" selected>Choose one</option>';
while($row = mysql_fetch_array( $result ))
{
if(isset($_POST['professional']))
if($_POST['professional'] == $row['prcno'])
echo '<option value="'.$row['prcno'].'" selected>'.$row['prcno']."\t"."|\t".$row['name']."\t"."|\t".$row['profession'].'</option>';
else
echo '<option value="'.$row['prcno'].'">'.$row['prcno']."\t"."|\t".$row['name']."\t"."|\t".$row['profession'].'</option>';
}
echo '</select>';
}
?>

whitespace in select dropdown, pulled from mysql

I'm using this to populate a dropdown:
$result = mysql_query("SELECT user from `users` order by user asc") or die(mysql_error());
echo '<select name="user" class="user">';
while ($row = mysql_fetch_array($result)) {
echo "<option value=".$row['user'].">".$row['user']."</option>";
}
echo '</select>';
But the source is this:
<select name="user" class="user"><option value=joe bloggs>joe bloggs</option>
So when i do this:
var user = $('.user').val();
It only sees "joe" not "joe bloggs"?? Any ideas
$result = mysql_query("SELECT user from `users` order by user asc") or die(mysql_error());
echo '<select name="user" class="user">';
while ($row = mysql_fetch_assoc($result)) {
echo '<option value="'.htmlspecialchars($row['user']).'">'.htmlspecialchars($row['user']).'</option>';
}
echo '</select>';
Corrected for you :)
Wrap the value in quotes:
echo "<option value=\"".$row['user']."\">".$row['user']."</option>";
Also, make sure to htmlspecialchars() in order to escape possible quotes in the name. E.g.,
$user = htmlspecialchars($row['user']);
printf('<option value="%s">%s</option>', $user, $user);
inside the while loop try:
echo "<option value=\"".$row['user']."\">".$row['user']."</option>";
basically adding a quote to the value inside the option
Use quotes " around attribute values..
your html should look like
<option value="joe bloggs">joe bloggs</option>

Categories