recovery value in a while - php

I create this code to display a list of countries, I would retrieve the selected country but it does not work
<?php
include 'includes/combo.php';
?>
<?php
echo "<label for='pays'>Pays*</label>
<select id='pays' name='pays '>";
while ($row = mysql_fetch_array($combo)) {
echo "<option>$row[0]</option>";
}
echo "</select> ";
echo "<option>$row[0]</option>";
?>
and recover with a
if(isset($_POST['pays']))
$pays=$_POST['pays'];
else
$pays="";
echo $pays;
but the pays value is not recouped

echo"<option value=" . $row[0] . ">$row[0]</option>";
To retrieve the value of your option

Try this
<?php
include 'includes/combo.php';
?>
<?php
echo '<label for="pays">Pays*</label>
<select id="pays" name="pays">';
while ($row = mysql_fetch_array($combo)) {
echo '<option value="' . $row[0] . '">' . $row[0] . '</option>';
}
echo '</select>';
?>
You have one space after name in select
And your option hasn't value attribut

Related

How to show a selected value in a dynamic drop down in PHP

I am trying to retain the value selected in a drop down menu. Everything is working, but I don't know how to show and retain the selected value. How can I do this?
I've got this working using another way:
<?php if($_POST['selClass'] == $row1['class']) echo 'selected="selected"' ?>
but this leads to other problems, i.e. a blank option in my drop down menu.
<form action="" method="POST" name="form1" id="form1">
<select name="selClass" size="1" id="selClass" onchange="form1.submit()">
<option value="">Select a class</option>
<?php
echo "<option value='". "All records". "' . >" . "all records". "</option>";
while ($row1 = mysqli_fetch_array($rs5)) {
echo "<option value='".$row1["class"] ."'>" . $row1["class"]. "</option>";
}
?>
</select>
</form>
You can approach this as
<?php
$selectedOption = '';
if($_POST){
$selectedOption = $_POST['selClass'];
}
?>
<form action="" method="POST" name="form1" id="form1">
<select name="selClass" size="1" id="selClass" onchange="form1.submit()">
<option value="">Select a class</option>
<?php
echo "<option value='". "All records". "' . >" . "all records". "</option>";
while ($row1 = mysqli_fetch_array($rs5)) {
if($row1["class"] == $selectedOption)
echo "<option value='".$row1["class"] ."' selected='selected'>" . $row1["class"]. "</option>";
else
echo "<option value='".$row1["class"] ."'>" . $row1["class"]. "</option>";
}
?>
</select>
</form>
There is two option you can choose whichever you feel ease.
<?php
echo "<option value='". "All records". "' . >" . "all records". "</option>";
while ($row1 = mysqli_fetch_array($rs5)) {
if($_POST['selClass'] == $row1['class']){
echo "<option value='".$row1["class"] ."' selected='selected'>" . $row1["class"]. "</option>";
}else{
echo "<option value='".$row1["class"] ."'>" . $row1["class"]. "</option>";
}
}
?>
OR
<?php
$selectedClass = $_POST['selClass'];
while ($row1 = mysqli_fetch_array($rs5)) { ?>
<option value="<?php echo $row1['çlass']?>" <?php if(selectedClass == $row1['class']) { echo "selected='selected'"; }?> ><?php echo $row1['class']?></option>
<?php } ?>

Make select options from database

I want to make select options list with data from my database
I did something like this
<?php
if ($resultt->num_rows > 0) {
// output data of each row
while($row = $resultt->fetch_assoc()) {
?>
<select name="name" class="custom-select">
<?php
echo "<option value='1'" . $row['name'] . "'>" . $row['name'] . "</option>";
?>
</select>
<?php
}
} else {
echo "0 results";
}
?>
I see my output but every data is in seperated "box". I just want list to choose for example options1 or 2.
What I did wrong?
You need to take the <select> element outside the loop.
<?php
if ($resultt->num_rows > 0) {
echo '<select name="name" class="custom-select">';
// output data of each row
while($row = $resultt->fetch_assoc()) {
echo "<option value='$row[id]'>$row[name]</option>";
}
echo '</select>';
} else {
echo "0 results";
}
its happen because you put <select> inside loop so it create every time new <select>
just put your select outside loop like as follow
<select name="name" class="custom-select">
<?php
while($row = $resultt->fetch_assoc()) {
echo "<option value='1'" . $row['name'] . "'>" . $row['name'] . "</option>";
}
?>
</select>
You are nesting select elements inside each loop
<select name="name" class="custom-select">
<?php
while($row = $resultt->fetch_assoc()) {
echo "<option value='".$row['name']."'>" .$row['name']."</option>";
}
?>
</select>
And watch out for unnecessary single quote that will break your html:
"'>"

Pre-selected drop down list value from MySQL database

Though I have researched, I couldn't find any solution for this. I need to get the database value ("Default") as the pre-selected value of the drop down list.
<select name="listCustomer" id="listCustomer">
<?php
$sql = mysqli_query($connection,"SELECT customer_name FROM customers");
while ($row = mysqli_fetch_array($sql,MYSQLI_ASSOC)){
echo "<option value=\"" . $row['customer_name'] . "\">" . $row['customer_name'] . "</option>";}
?>
</select>
Can you please help me on this?
Just create a variable before the echo, something like:
$selected = ((strtolower($row['customer_name']) == 'default') ? 'selected' : '');
then change the echo to this:
echo '<option '.$selected.' value="'.$row['customer_name'].'">'.$row['customer_name'].'</option>';
This can be accomplished using an if statement on the customer_name
$sql = mysqli_query($connection,"SELECT customer_name FROM customers");
while ($row = mysqli_fetch_array($sql,MYSQLI_ASSOC)){
if($row["customer_name"] === "Default"){
echo "<option value=\"" . $row['customer_name'] . "\" selected>" . $row['customer_name'] . "</option>";
} else {
echo "<option value=\"" . $row['customer_name'] . "\">" . $row['customer_name'] . "</option>";
}
}
?>
Note the selected tag on the first echo.
$query="SELECT customer_name FROM customers";
$result = #mysql_query ($query);
echo "<select name=customer_name value=' '>";
while($drop=#mysql_fetch_array($result)){
echo "<option value=$drop[customer_name]>$drop[customer_name]</option>";
}
echo "</select>";

In the edit mode i need to show selected value from the dropdown in PHP

I have a dropdown in php the value is coming from database. In the edit mode i need to show selected value in to drop down. My php is below:
<?php
echo "<td width='5%'>";
$sql_currency = "SELECT * FROM currency1";
$result_currency = mysql_query($sql_currency);
echo "<select id='currency_change$i' >";
while ($row_currency = mysql_fetch_array($result_currency)) {
echo "<option value=" . $row_currency['currency'] . " data-price=" . $row_currency['rate'] .">" . $row_currency['currency'] ."</option>";
}
echo "</select>";
echo "</td>";
?>
Try this code...
<?php
echo "<td width='5%'>";
$sql_currency = "SELECT * FROM currency1";
$result_currency = mysql_query($sql_currency);
echo "<select id='currency_change$i' >";
/*
* selected value
*/
$selectedValue = ""; // assign that value to this variable
while ($row_currency = mysql_fetch_array($result_currency)) {
$selected = "";
if($row_currency['currency'] == $selectedValue){
$selected = ' selected="selected" ';
}
echo "<option ".$selected." value=" . $row_currency['currency'] . " data-price=" . $row_currency['rate'] . ">" . $row_currency['currency'] . "</option>";
}
echo "</select>";
echo "</td>";
?>

how to do echo of selected item using php

In a form after submitting the data, if i want to edit form the data in the form should be from database i have to echo the selected value from the database if data is present else it has to show the options to select the values from another table where these options are present
here is the code
please correct it
<select name="ccname" class="form-control form-color">
<?php
if(isset($storyData)) {
echo '<option value="' . $storyData['ccname'] . '">' . $storyData['ccname'] . '</option>';
}
else{
echo "<option value="">CCname</option>";
foreach(getNames() as $ccname)
echo '<option value="'.$ccname.'">'.$ccname.'</option>';
}
?>
</select>
after working with concatenation operator finally debugged the code
here is the code
that i corrected
<select name="ccname" class="form-control form-color">
``<?php if(isset($storyData))
{
echo "<option value = '" . $storyData['ccname'] . "'>".$storyData['ccname']."</option>";
}
else{
echo "<option value='1'>CCname</option>";
foreach(getNames() as $ccname)
echo '<option value="'.$ccname.'">'.$ccname.'
</option>';
}
?>
</select>

Categories