PHP dropdown only selecting the last item in list - php

I created a drop down list in PHP and it's selecting the last item of the list and I can't figure out why it's doing this.
When I echo the event_id (the value that is returned), it's the last item every time. The list is populated correctly.
$forms = mysql_query("select events.event_title, events.event_id, saved_forms.id from events
INNER JOIN saved_forms on saved_forms.id = events.event_id
where saved_forms.form_type = 'e' and events.event_by = '$my_username'");
while($form = mysql_fetch_array($forms)){
$form_id = $form['event_id'];
$selection.="<OPTION VALUE=\"$form_id\">".$form['event_title']."</OPTION>";
}
?>
<div id="saved_forms">
<tr><td><select name ="saved_form" value ="<? echo $form_id; ?>" onchange="showUser(<? echo $form_id; ?>)">
<Option value="$form_id"><? echo $selection; ?></Select></td><td>Select Existing Form</td></tr>
</div>

Your problem is here
<select name="saved_form" value="<? echo $form_id; ?>" onchange="showUser(<? echo $form_id; ?>)">
By giving the select tag a value of $form_id you have a high chance of overriding the value set in the options. And you shouldn't hardcode the value into the onchange event. Try removing the value from select and let the option tags do the work.
<select name="saved_form" onchange="showUser(this.value)">

Related

Data Not Showing Up From A For Loop

I am making a product recommender in my shopping cart, something similar to when you go to the grocery store and pickup candy as an impulse.
It looks like below. When I click on the dropdown menu, it does not dropdown with the data I am looking for.
I am using the following code that I would think would input this data:
<?php foreach($Fluoros['Fluoro'] as $Fluoro) {
echo $Fluoro['Fluoro'], '<br>';
}
?>
<select title="Add Fluoro To Your Cart" id="<?php echo $Fluoro['Description']; ?>" class="selectpicker white-drop crimp-part" data-width="auto">
<select title="Select Fluoro" id="<?php echo $Fluoros['Product']; ?>" class="selectpicker white-drop crimp-part" data-width="auto">
<?php foreach($Fluoros['Product'] as $Fluoro){
echo '<option ' . 'value = ' . $Fluoro['Fluoro'] . ' </option>';
}
?>
</select> ```
[enter image description here][1]
[1]: https://i.stack.imgur.com/AXygI.png
The data is being pulled in my "Cart " class. The Select statement below works and brings the correct results
```php
public function fluoroCart() {
$getFluoroProducts = $this->_db->query("SELECT i.Part_Number, i.Description
FROM Inventory i
INNER JOIN Product_Details pd on pd.Part_Number = i.Part_Number
WHERE pd.Category_ID = 15");
$fluoroProducts = $this->_db->results();
foreach($fluoroProducts as $Fluoro){
$Flouros [] = array( 'Fluoro'=>$Fluoro->Part_Number, 'Description'=>$Fluoro->Description);
}
}```
In HTML, select option values need to be surrounded by quotes. Also, in addition to indicating a value, you need to put some text between the opening and closing option tags for it to actually show up in the menu.
<select>
<option value="some value">Some Value Text</option>
</select>
You need to modify your PHP like this
echo '<option value = "' . $Fluoro['Fluoro'] . '">'.$Fluoro['Fluoro'].' </option>';

submit two values from two different dropdown list in 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!!

Dynamic Dropdown List PHP MYSQL

I am new to PHP and trying to make dynamic list box using SQL
<label> Select Sport</label>
<select name = "Sport">
<option value = "">Select Sport</option>
<?php
$all = "SELECT * FROM EventTable";
$result = $pdo->query($all);
foreach($result as $Sport){
?>
<option value ="<?php echo $Sport['Sport']; ?>"></option>
<?php
}
?>
</select>
But its printing BLANK space
Try this:
<select name="">
foreach($result as $Sport){
?>
<option value ="<?php echo $Sport['Sport']; ?>"><?php echo $Sport['Sport']; ?></option> // The value between <option> value </option> is the value which is to be shown in the dropdown, without this it is showing blank
<?php
}
</select>
You probably shouldn't use SELECT *, but since you're learning, I digress. Also, I personally have a hard time reading a lot of opening and closing php tags scattered throughout plus they often give weird results than what you're expecting, so this is how I'd write it.
<?php
$all = "SELECT * FROM EventTable";
$result = $pdo->query($all);
foreach($result as $sport)
{
echo "<option value =" . $sport['Sport'] . ">" . $sport['Sport'] . "</option>";
}
?>
The blank spaces indicates that you are actually hitting the loop and iterating, but the values for that array item are null. Are you sure the field name isn't 'sport' instead of 'Sport'?

How can I pre-select a dropdown option using previously submitted data from database, MySQLI

I have two tables: orders and companies. Note that both tables has a company_id row.
Orders Table:
order_id | order_name | company_id | .....
Companies Table:
company_id | company_name | .....
I have a page with a form that INSERTS data for an order into the orders table in my database. Part of the order is selecting a company from the companies table to go with it, so I use a dropdown for this.
I also have another page with a form that UPDATES that data (an edit page). When I click on the button to bring up the edit page, I'd like to have the previously submitted data in the input fields. With text fields, I just use value="<?php echo $company['whatever']; ?>". However, with dropdown menu's I can't figure out how to pre-select the correct option.
Here is my URL structure:
http://website.com/orders.php?page=edit&order_id=1
Here is what I have to create my dropdown menu:
$getOrder = mysqli_query($db, "SELECT * FROM orders WHERE order_id = ".$_GET['order_id']);
$getCompany = mysqli_query($db, "SELECT * FROM companies ORDER BY company_name ASC");
....
<select class="form-control" name="company_id" id="company_id">
<?php
if($getCompany) {
while($company = mysqli_fetch_assoc($getCompany)) { ?>
<option value="<?php echo $company['company_id']; ?>">
<?php echo $company['company_name']; ?>
</option>
<?php }
}
?>
How can I get selected on the option that's been previously selected?
If $getOrder has the order details (including company_id), and $getCompany has the company details (including company_id), then you can compare the two.
If they are equal, echo out selected as an attribute in the option, like so:
<select class="form-control" name="company_id" id="company_id">
<?php
if($getCompany) {
//Get company ID from Order
$orderID = $getOrder["company_id"];
while($company = mysqli_fetch_assoc($getCompany)) { ?>
<option value="<?php echo $company['company_id']; ?>"
<?php
//Compare and echo `selected` if they are equal
if($orderId==$company["company_id"]) echo "selected";
?>>
<?php echo $company['company_name']; ?>
</option>
<?php }
}
?>
That code can be cleaned up, however:
<select class="form-control" name="company_id" id="company_id">
<?php
if($getCompany) {
$orderID = $getOrder["company_id"];
while($company = mysqli_fetch_assoc($getCompany)) {
echo "<option value='{$company['company_id']}".($getOrder["company_id"]==$getCompany["company_id"] ? " selected" : null).">{$company['company_name']}</option>";
}
}
?>

How to change value in input (textbox) by changing select

I'm trying to change the value in an input when a different option gets selected from a select:
View image here.
The idea is that for each ad package you can have a different number of images (free gets 0 images, $20 for 4 images etc.). With this number I then want to display the correct amount of upload fields for images.
I'm already retrieving the values from the database for the number of images for each package as you can see in the code below:
<select name="ad_pack_id" class="dropdownlist required">
<?php foreach ( $results as $result ) { ?>
<option value="<?php esc_attr_e($result->pack_id); ?>" class="<?php esc_attr_e($result->pack_images); ?>"><?php esc_attr_e($result->pack_name); ?></option>
<?php } ?>
</select>
<input type="hidden" value="" name="packimages" id="packimages" />
I've tried getting the value from the select directly by doing:
mainform.ad_pack_id.options[selectedIndex].class.innerHTML
but this isn't getting the number of images.
How can I get the number of images for the selected ad package without submitting the form first?
you could also try jquery:
$("#ad_pack_id option:selected").val()
this should get you the value. to show/hide upload fields you can use for example the CSS display attribute:
$("#uploadfield_1").css("display", "none")
$("#uploadfield_1").css("display", "block")
try this:
document.mainform.ad_pack_id.options[Selected].className;
This code will set the input's value to the selected option's value.
I just added "id" attribute to select menu
<select id="ad_pack_id" name="ad_pack_id" class="dropdownlist required">
<?php foreach ( $results as $result ) { ?>
<option value="<?php esc_attr_e($result->pack_id); ?>" class="<?php esc_attr_e($result->pack_images); ?>"><?php esc_attr_e($result->pack_name); ?></option>
<?php } ?>
</select>
<input type="hidden" value="" name="packimages" id="packimages" />
<script type="text/javascript">
var selectmenu = document.getElementById("ad_pack_id");
selectmenu.onchange = function()
{ //run some code when "onchange" event fires
var chosenoption = this.options[this.selectedIndex] //this refers to "selectmenu"
if (chosenoption.value!="nothing")
{
document.getElementById("packimages").value = chosenoption.value ;
}
}
</script>

Categories