I have a dropdown showing options from mysql, no problem there. The problem is on the update page when i want to show the option already selected previously.
The dropdown selects options from the margins table and puts the value into a field in the products table.
This is the query that selects the product record :
<?php
$recordID = $_GET["recordID"];
$product_result = mysqli_query($con,"SELECT * FROM products WHERE product_code='$recordID'") or die(mysqli_error($con));
$product = mysqli_fetch_array($product_result);
$checked_special = $product['product_special'];
$checked_publish = $product['product_publish'];
$checked_frontpage = $product['product_display_frontpage'];
$checked_facebook = $product['display_facebook'];
{
?>
And this is the part of the form that gets the options from the margins table and displays them on page.
<tr>
<td>Display Facebook</td>
<td><input type="checkbox" name="display_facebook" id="display_facebook" value="y" <?php if ($checked_facebook == 'y') echo 'checked'; ?> /></td>
<td><strong>Margin Group :</strong></td>
<td>
<select name="margin_group" id="margin_group"><?php
$resul2 = mysqli_query($con,"SELECT * FROM margins");
while($row2 = mysqli_fetch_array($resul2))
{
?> <option value="<?php echo $row2['margin_group']; ?>"> <?php echo $row2['margin_group']; ?></option>
<?php } ?> </select></td>
</tr>
How can i get the $product['margin_group'] value from the products table show as selected option in the dropdown, so that the user doesn't have to reselect every time they update the page.
Thanks :)
MsKazza
The idea is to add the word selected in the desired option tag like this :
<option value="x" selected>x</option>
This way it will be selected in the form Check this
in order to do that we will make a conditional statement for every option value in the while loop. If the value meets the condition, we will echo the word selected
<?php while($row2 = mysql_fetch_array($resul2): ?>
<option value="<?= $row2['margin_group']; ?>"
<?php if($row2['margin_group']) == $products_table_variable) : ?>
selected
<?php endif; ?>
><?= $row2['margin_group']; ?></option>
<?php endwhile ?>
<select name="margin_group" id="margin_group">
<?php
$datasource = mysqli_query($con,"SELECT * FROM margins");
while($getdata= mysql_fetch_array($$datasource)){
?>
<option value="<?=$row2['margin_group']?>" <?php if($getdata['colume_name']==$row2['margin_group']) echo "selected";?>> <?=$row2['margin_group']?></option>
<?php } ?>
</select>
Hope it will help you :)
Related
I have a function having two dropdown list and one submit button, the user must choose a value from dropdown list A and a value from dropdown list B so he can submit them.
I knew that I need to have an attribute in the value section as follows
<select name="students" id="students">
<?php
while($data = mysqli_fetch_array($selectstudent1)) {
$name=$data['name'];
$idname =$data['user_id_student'];
?>
<option name="studentdd" value="<?php echo $idname; ?>"><?php
echo $name; ?></option>
<?php } ?>
</select>
and the second dropdown list here
<select name="groups" id="groups">
<?php
while($data1 = mysqli_fetch_array($selectgroup1)) {
$groupschoose = $data1['group_number']; ?>
<option name="nogroup" value="<?php echo $groupschoose; ?>">
<?php echo $groupschoose; ?></option>
<?php }//while ?>
</select>
so, i need to get the "php echo $idname" (student id) from A dropdown list
and "php echo $groupschoose; " (group id) form drop downlist B
so I tried this PHP code as follows:
<?php
if(isset($_POST['addmember'])){
//addmember is id for dropdownlists form
$groupnum= $_POST['nogroup'];
$stuid=$_POST['studentdd'];
echo $groupnum;
echo $stuid ;
$sq="SELECT user_id_student FROM student WHERE
user_id_student=$stuid ";
$sq1 = mysqli_query($con,$sq);
while ($sq2 = mysqli_fetch_array($sq1)) {
$tes = $sq2['user_id_student'];
if( $tes == $stuid){
$sqlmem="UPDATE student
set group_id= $groupnum
WHERE user_id_student=$stuid";
$resultmem = mysqli_query($con,$sqlmem);}
}
}
?>
But it won't work, and I'm not sure where is the missing section
I hope my Q is clear and I'm sorry if it's little bit long, but i'm looking for your help, thank you!!
I couldn't keep the selected value after loading the page, that's why I couldn't take the selected value from the select option in Database after done some function..
Here is my code
<select name='courseID' class="mySelect" id='courseID'>
<?php while ($row1 =mysqli_fetch_array($result1)):;?>
<option value="<?php echo $row1['CourseID'];?>"><?php echo $row1['CourseID'];?></option><?php endwhile; ?></select>
<script type="text/javascript">document.getElementById('courseID').value="<?php echo $_GET['courseID'];?>";</script>
Did JS can add in a HTML in this way??
You have add condition to check if the course id is in the result.
<?php while ($row1 =mysqli_fetch_array($result1)):;
$selected = ($_GET['courseID'] == $row1['CourseID']) ? 'selected' : '';
?>
<option value="<?php echo $row1['CourseID'];?>" <?php echo $selected;?>><?php echo $row1['CourseID'];?></option>
<?php endwhile; ?>
I have edit form where I get info from database
<select name="table">
<?php
//fetch all tables from database
$user = $con->query("SELECT * FROM table") or die(mysql_error());
while($row = $table->fetch_object()) { ?>
<option value="<?php echo $row->tablename;?>">
<?php echo $row->tablename; ?>
</option> <?php }?> </select>
<label for="time">Time :</label>
<select name="time">
<option value="twotothree">2PM-3PM</option>
<option value="threetofour">3PM-4PM</option>
<option value="fourtofive">4PM-5PM</option>
<option value="fivetosix">5PM-6PM</option>
</select>
The names of the tables from drop-down menu are different. Everytime I select a table and a time slot and save the data, the selection goes back to the first row of the menu. Eg I select table 3 and 4PM-5PM after saving it goes back to table 1 and 2PM-3PM. I need to be fixed on the last selection as I might use 4PM-5PM for table 4 also. Any idea? Thanks
You can add selected attribute when rendering your select list, depending on $_POST variable, when it's available. For example for you table select element:
<select name="table">
<?php
$user = $con->query("SELECT * FROM table") or die(mysql_error());
while($row = $table->fetch_object()) { ?>
<option value="<?php echo $row->tablename;?>" <?php if (isset($_POST['table']) && $_POST['table'] == $row->tablename) echo 'selected'; ?> >
<?php echo $row->tablename; ?>
</option>
<?php }?>
</select>
In similar way you can do for time select element.
My update statement isn't working correctly, I am attempting to pull the data from the database, populate a dropdown with either "Y" or "N" inside it, on submit the values are entered into the database and the page refreshes.
So far I have my list of items, each with correctly populated dropdown, it is now my submit that is failing to work.
<?php
$updatedFeatProd = $_POST['featuredProduct'];
var_dump($updatedFeatProd);
if ($_POST) {
foreach ($_POST['featuredProduct'] as $key => $val) {
$query = 'UPDATE tblProducts SET featuredProduct = ' . $updatedFeatProd . '
WHERE fldID = ' . $val;
$sql = dbQuery($query);
}
}
$sql = dbQuery('SELECT fldId, fldName, featuredProduct FROM tblProducts');
?>
<form method="post" action="#" name="featuredProd">
<table>
<tr><td><p>Product Name</p></td><td><p>Is a featured product?</p></td></tr>
<?php
$products = dbFetchAll($sql);
foreach ($products as $product) {
//var_dump($product['fldName']);
?>
<tr>
<td>
<p><?php echo $product['fldName']; ?></p>
</td>
<td>
<select name="featuredDropdown">;
<?php
if ($product['featuredProduct'] == 'Y') {
?>
<option value="<?php $product['fldId'] ?>"><?php echo $product['featuredProduct'] ?></option>
<option value="<?php $product['fldId'] ?>">N</option>
<?php
} else {
?>
<option value="<?php $product['fldId'] ?>"><?php echo $product['featuredProduct'] ?></option>
<option value="<?php $product['fldId'] ?>">Y</option>
<?php
}
?>
</select>
</td>
</tr>
<?php
}
?>
The presentaton here does not make much sence. You have a dropdown with the ProductName in one slot and a 'N' in another.
Once the user has selected 'N' for a product they have no idea what they have said NO to, as they can no longer see the product name that they have selected NO for.
It would make more sence to provide a <label> containing the product name and a YES/NO dropdown beside it for them to select from.
However the reason your update code is not working is that you have called the dropdown featuredDropdown
<Select name="featuredDropdown">
and you are trying to process a field called featuredProduct in the update code
foreach ($_POST['featuredProduct'] as $key => $val) {
Your next problem will probably be that you are oututting more than one <Select name="featuredDropdown"> so you need to make that into an array as well like this:
<Select name="featuredDropdown[]">
Then you will have an array of featuredDropdown in the $_POST array. $_POST['featuredDropdown'][]
I've googled a lot and found alternative solutions but in my case things are a bit different.
<select name='database'>
<?php foreach($databases as $row): ?>
<option value="<?php echo $row; ?>"><?php echo $row; ?></option>
<?php endforeach; ?>
</select>
I need to display the selected option after a POST request in between the option tags but since I already have a value for that I cannot find a way to do so. The idea is that I have one form with a couple of select menus. From the first one I select a database. The second one is for selecting a table from the already chosen database and another select menu for the columns. The problem is that I'm sending a new request for both the database and table and the chosen database cannot be remembered (it just 'resets' the select menu and starts from the first value).
Here's the whole code
Right now I need to reselect the database which I've previously chosen in order to display the columns from a table.
<select name='database'>
<?php foreach($databases as $row): ?>
<option value="<?= $row; ?>"
<?php if ($row == $_POST['database']){echo " selected";}?>>
<?= $row; ?>
</option>
<?php endforeach; ?>
</select>
Wouldn't this work?
$dbms=$_POST['database'];
<select name='database'>
<?php foreach($databases as $row): ?>
<option value="<?php echo $row; ?>"
<?php if ($row == $dbms) echo " selected"; ?>
> <?php echo $row; ?></option>
<?php endforeach; ?>
</select>