Dropdown Box from MySQL to remember selection after submit - php

Can anyone help me with the following code? I have a dropdown box that is populated from a MySQL DB. All works fine but I would like it to remember the selection after the form has been submitted.
Code below:
$sql="select category_code, category_desc from members_categories";
$result=mysql_query($sql);
$dropdown = "<select name='category_desc' size='1'><option value='%'>All</option>";
while($rows=mysql_fetch_array($result)){
$dropdown .= "\r\n<option value='{$rows['category_code']}'>{$rows['category_desc']}</option>";
}
$dropdown .= "\r\n</select>";
echo $dropdown;
Many thanks,
John

you basically need to check in a loop if the submitted selection matches any selections being added to the html and if they do then insert the html selected="selected" to that element.
Here is an example of how it could work using your code:
$sql="select category_code, category_desc from members_categories";
$result=mysql_query($sql);
$dropdown = "<select name='category_desc' size='1'><option value='%'>All</option>";
while ($rows=mysql_fetch_array($result)) {
$checked = ($rows['category_code'] == $_POST['category_code'])? "selected='selected'":"";
$dropdown .= "\r\n<option value='{$rows['category_code']}' $checked>{$rows['category_desc']}</option>";
}
$dropdown .= "\r\n</select>";
echo $dropdown;
Hope this helps.
EDIT That is assuming you will be showing the form again after
If not then you need to just save the posted selection to the $_SESSION array and you will be able to access it from the $_SESSION array when ever you need it while the session is active at least.
have a read of this basic php sessions examples

Related

Save in the database values of dropdown

I'm making a dropdown, and I want when the user select one values, save to the database.
This is my code:
$query = mysql_query("SELECT name FROM store_locator_bundes");
echo '<select name="bundesland-dropdown">';
while ($row = mysql_fetch_array($query)) {
echo '<option value="'.$row['name'].'">'.$row['name'].'</option>';
}
echo '</select>';
And the conditional
if($_POST[$criteria['submit']['name']]) {
// here I am lost, I have to insert the values of the dropdown to store_locator and the field bundes_id
}
Thanks for you help ;)
You want
$_POST['bundesland-dropdown']; // You want the <select> not the <options>'s name
Then for the insert:
if (isset($_POST['bundesland-dropdown'])){
//mysqli/PDO/whatever database method of insertion you want here
}

2nd dropdown list based on the value of first drop down list [duplicate]

This question already has an answer here:
How to populate second dropdown based on selection of first dropdown using jQuery/AJAX and PHP/MySQL?
(1 answer)
Closed 9 years ago.
I have the following code to generate a dropdown list.
$query = "SELECT * FROM firm";
// Execute it, or return the error message if there's a problem.
$result = mysql_query($query) or die(mysql_error());
$dropdown = "<select name='name'>";
while($row = mysql_fetch_assoc($result))
$dropdown .= "\r\n<option value='{$row['name']}'>{$row['name']}</option>";
$dropdown .= "\r\n</select>";
How can i modify this code to make a 2nd dropdown based on the value selected in the above drop down.
What i am trying to achieve is that you can firstly select a firm as above, then secondly i want to be able to choose between the different areas for that specific firm. All this information is in one table.
Is it possible to modify this code.
You can bind a change event on the first select element
$("#firm").change(function(){...});
When that happens you need to make an AJAX request to get those extra options for the second select element and then use that result set to populate the new select element.
Here you can see the Javascript implementation of getting that new result set and populating select element: Populate Select box options on click with Javascript/Jquery with Json data
P.S. There is a second option where you don't use AJAX but get all select options first with their relationships to each other and then based on selected item hide/show connected areas in the second select element.
create a function in javascript to generate query string :
<script type="text/javascript">
function show(val){
window.location="pagename.php?drp="+val;
}
</script>
call this function on onchange event of Dropdown:
$dropdown = "<select name='name' onChange='show(this.value)'>";
now a query string generate. fetch query string value in any variable
Bind second Dropdown:
if(isset($_REQUEST["drp"]))
{
$val=$_REQUEST["drp"];
$query = "SELECT * FROM tablename where parametername='$val'";
// Execute it, or return the error message if there's a problem.
$result = mysql_query($query) or die(mysql_error());
$dropdown = "<select name='drp2'>";
while($row = mysql_fetch_assoc($result)) {
$dropdown .= "\r\n<option value='{$row['name']}'>{$row['name']}</option>";
}
$dropdown .= "\r\n</select>";
}

Listbox - initialize with saved value in PHP

How can I initialize a listbox using a value from a table in the database?
When I'm posting page without doing changes in listbox, application save first value of listbox to the table.
<?php
$cpquery = "Select distinct(operater) from operater where dept='$dept' order by operater";
$cpresult = mysql_query($cpquery) or die(mysql_error());
?>
<select name="operater" value="operater"> <!-- Drop down -->
<?php
if($cpresult) {
while($row = mysql_fetch_array($cpresult)) {
echo '<option value="' .$row['operater']. '">'. $row['operater']. '</option>' ;
}
}
echo "<option value='operater' ></option>";
echo "</select>"; <!-- end of block --> –
Maybe you could use a hidden form input with the "fallback" value, so in PHP you could check if your listbox was changed. If it wasn't, you could use this hidden value.
But I'm not sure I understood your question.

select list in edit form

I have this code to collect categories from the field "category" from Mysql, it is a part editing form, i want the category that was selected before to be selected after the list is filled, the code is :
<td align="right">Category</td>
<td>
<?php
// Write out our query.
$query = "SELECT id,cat_name,parent FROM categories WHERE parent='0'";
// Execute it, or return the error message if there's a problem.
$result = mysql_query($query) or die(mysql_error());
$dropdown = "<select name='category'>";
while($row = mysql_fetch_assoc($result)) {
$dropdown .= "\r\n<option value='{$row['id']}'>{$row['cat_name']}</option>";
}
$dropdown .= "\r\n</select>";
echo $dropdown;
?>
</td>
I don't know how to do it? i know that i should type "selected" on the option i want to select but how to choose it after i filled the list?
You could use an indexed tab $tab["key"] this way you can easily find the line that you want and edit it.
Edit: Your message isn't very clear, but I figured that's want you want to do. Otherwise, clarify please.
In your code you're not checking what the user has selected previously. You can do that with something like this:
$selected = '';
if($row['id']==$userSelectedOption) $selected = ' selected="selected"';
$dropdown .= "\r\n<option value='{$row['id']}'{$selected}>{$row['cat_name']}</option>";
In the above example, we have a variable named $userSelectedOption which represents the value a user has already selected. We're also assuming that it is the ID of a category. Within your while loop you would use the above 3 line instead of your one line. When this renders out the list will have the selected item be whatever matches $userSelectedOption

PHP dropdown populated by database to be use javascript

I have this php code that get items from the database and i have to use this drop-down in my JavaScript code to duplicate the same drop-down when a button is clicked
function dup(){
}
<?php
include('view/connect.php');
$sql="SELECT ss_code FROM ss WHERE sn_id = 1";
$result =mysql_query($sql);
$dropdown = "<select name='users'>";
while($row = mysql_fetch_assoc($result)) {
$b = $row['ss_code'];
$dropdown .= "\r\n<option value='{$b}'>{$b}</option>";
}
$dropdown .= "\r\n</select>";
echo $dropdown;
?>
Put all your items in an array and then set a JavaScript variable to the value returned by encoding it as JSON.
var iarray = <?php echo json_encode($items); ?>;
Then iterate over the resultant array and create the pulldown from that.
What do you mean same drop down?If you want same dropdown without any change on a button click you can use jQuery .clone().

Categories