submit two values from two different dropdown list in php - php

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!!

Related

How to Update Database Table Using Dropdown in Datatable

I've spent more than 48hrs trying to solve the following problem to no avail. I want to use datatable in php to update record. I am able to display record based on the user selected, However, some of the inputs are <Select>.
The following code populates the options but I'm getting
Undefined Index error
<td><select name="class_assigned['<?php echo $id; ?>']" class="form-control
class_assigned"><option value="<?php echo $class_assigned; ?>"><?php echo
$class_assigned; ?>
</option>
<?php
$class_sql = "SELECT Class_Name FROM tbl_classes WHERE Class_Name !=
'$class_assigned'";
$class_result = $conn->query($class_sql);
if ($class_result->num_rows > 0) {
while($row = $class_result->fetch_assoc()) {
$class_name = $row['Class_Name'];
?>
<option value="<?php echo $class_name; ?>"><?php echo $class_name; ?>
</option>
<?php }} ?>
</select>
</td>
While the following code populates the Current Class_Name but I couldn't make other options appear:
<td><?php echo '<select name="class_assigned['.$row["id"].']" class="form-control"><option value="'.$class_assigned.'">'.$class_assigned.'</option></select>'; ?></td>
What I want:
Let's say the table displays SS 1A, and the user wants to update to SS 2A. I want the Options in the dropdown to display all classes available in tbl_classes.
Please have in mind that the table can display multiple rows at a time (Array).
Thank you for your help.

Wordpress retain a selected value in a dropdown after form submit

I am a complete novice but trying to design my first site in Wordpress. I am trying to retain the value of a dropdown that pulls values from a database after a form is submitted. I have searched many questions and tried many different ways of trying to write selected="selected" in my code but none of them seem to work. Please help.
Here is my code that just pulls from the database but isn't trying to retain the selected value:
<select name = "box1" class="searchbox">
<option value = "">All Values</option>
<?php
global $wpdb;
$ddresult = $wpdb->get_results("SELECT Field1 FROM pc_table ORDER BY Field1 ASC");
foreach($ddresult as $ddrow) {
?>
<option value="<?php echo $ddrow->Field1; ?>"><?php echo $ddrow->Field1; ?> </option>
<?php
} ?>
</select>
How can I add the code to keep the value selected after form submit? Any help much appreciated, thank you.
Try this. It checks if box1 was submitted. Then it compares that value to the items in the options loop. A match will set $selected to the correct attribute, otherwise it will stay an empty string by default. (NB: if form is using get method, then change $POST to $_GET)
<select name = "box1" class="searchbox">
<option value = "">All Values</option>
<?php
global $wpdb;
$ddresult = $wpdb->get_results("SELECT Field1 FROM pc_table ORDER BY Field1 ASC");
foreach($ddresult as $ddrow) {
$selected = '';
if(isset($_POST['box1'])){
if($ddrow->Field1==$_POST['box1']){ $selected = 'selected="selected"'; }
}
?>
<option value="<?php echo $ddrow->Field1; ?>" <?php echo $selected; ?>><?php echo $ddrow->Field1; ?> </option>
<?php
} ?>
</select>
This example of course does not include if you want to save the submitted value to the database and then re introduce it in the output at a later time. If that's what you would like to know, then please leave a comment.

Drop down list not populating (PHP/Myadmin)

I am trying to get a dropdown list to populate. I have two tables, a registration table and a studentclasses table. I am looking to populate the dropdown list from the class table, as a field entry in the registration table. My code is as follows:
<p>Class: <select name="classID">
<?php $classlist_sql="SELECT * FROM studentclasses";
$classlist_qry=mysqli_query($dbconnect, $classlist_sql);
$classlist_rs=mysqli_fetch_assoc($classlist_qry);
do { ?>
<option value="<?php echo $classlist_rs['classID']; ?>"
<?php
if($classlist_rs['classID']==$_SESSION['signuptest']['classID']) {
echo "selected=selected";
}
?>
><?php echo $classlist_rs['classcode']; ?></option>
<?php } while ($classlist_rs=mysqli_fetch_assoc($classlist_qry));
?></select>
</p>
My Studentclasses table has the fields classID and classcode, and I am wanting the classcode to appear in the drop down.
Any help would be greatly appreciated.

showing selected option from mysql in dropdown

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 :)

Updating MySql DB with Dropdown values

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'][]

Categories