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().
Related
Is there a way to find out what is the value of the <option> in a <select> tag?
I have a form that contains a tag, with several options. Then, I want to insert the selected value on the database.
Is this doable with PHP? Or do i need JS/Jquery?
EDIT: this is what i have tried:
This creates the variable and stores the value of the option.
$query=mysql_query("Select cargo from utilizadores where nome<>'admin'");
echo '<select name="icargo[]">';
while ($row = mysql_fetch_array($query, MYSQL_ASSOC))
{
foreach ($row as $cargo){
echo '<option value="$cargo" name="icargo[]">'.$cargo.'</option>';
}
}
echo '</select>';
I send the variable this way, and then i try to receive it with a foreach:
$countcargo = 0;
foreach ($_POST["icargo"] as $cargoo){
$cargoarray[$countcargo] = $cargo;
$countcargo++;
}
I have a drop down menu for 'Countries'. Onchange of the dropdown ,i want to change the values of two other drop down menus.
While i do it for a single dropdown,it works just fine.But i seem to get confused when it comes to changing the data for both the dropdown menus.
This is what i am doing.
$.post(
base_url+'hostel_admin/functions/functions.php',
{country_code:country_code}
).done(function(data){
$("#continent").html(data.continents);
$("#continent").html(data.cities);
});
In functions.php
if($_SERVER['REQUEST_METHOD']=='POST' && (isset($_POST['country_code'])))
{
change_continents($continent_id);
change_cities($country_code);
}
function change_continents($code)
{
$sql = "select id as Code,name as Name from hostel_continent";
$result = mysql_query($sql);
$output['continents'] = "<option value='0'>Select One</option>";
while($row = mysql_fetch_assoc($result))
{
$data = $row['Name'];
$val = $row['Code'];
if($val==$code)
{
$output .= "<option value='$val' selected='selected'>$data</option>";
}
else{
$output .= "<option value='$val'>$data</option>";
}
}
echo $output;
}
function change_cities($country_code)
{
$sql = "select ID,Name,District from hostel_city where CountryCode='$country_code'";
$result = mysql_query($sql);
$output['cities'] = "<option value='0'>Select One</option>";
while($row = mysql_fetch_assoc($result))
{
$data = $row['Name'];
$val = $row['District'];
$city_id = $row['ID'];
$output .= "<option value='$city_id' selected='selected'>$data.' / '.$val</option>";
}
echo $output;
}
I am able to do it for a single dropdown,can someone tell where exactly and what is going wrong?.
Thanks in advance.
First thing is first, separate your data. You are using PHP to echo html, that is the normal use case, but here I think json might help more.
So first to something like this on the php side:
$ajaxResponse = [];
Then for each of the data you can do this:
#in the change_continents function
$ajaxResponse["continents"][] = $row;
........
#in the change_cities
$ajaxResponse["cities"][] = $row;
Then do the output after each call
echo json_encode($ajaxResponse);
On the javascript side, you will now need to actually create the the html you are going to push to the objects.
First do not do $('').html() twice on the same selector, the second one will overwrite the first one. You will need tow selectors.
Then you will need to do something like this:
$.post(
base_url+'hostel_admin/functions/functions.php',
{country_code:country_code}
).done(function(data){
var continents = '<select {select options}>';
if(typeof(data)=='string')data=JSON.decode(data);
for( key in data.continents)
continents+='"<option value="'+data.continents.Code+'" selected='selected'>"'+data.continents.Name+'" </option>"; '
continents+="</select>"
$("#continent").html(continents);
var countries = '<select {select options}>';
for( key in data.countries)
continents+='"<option value="'+data.countries[key].Code+'" selected='selected'>"'+data.countries[key].Name+'" </option>"; '
continents+="</select>
$("#countries").html(data.cities);
});
The code above might not be fully functional, but should lead you in the right direction.
The best way would be to make the functions on the php side return the data you need, and then format it in another function, so if you need json you can output json, if you need html you can output html.
Second, on the javascript side, you could use "underscore" and it's templateing engine to do the hard work for you.
Hope this helps.
i have a dropdown list the value of each item generate dynamic
<?php
include('../db_inc.php');
$sql="select * from genre";
$result = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_object($result)){
$option =$row->genre_name;
$value =$row->genre_id;
echo '<option value='.$value.'>'.$option.'</option>';
}
?>
i want pass selected item value from dropdown to server and save that to database,
i can do that with ajax,but i want to know php can do that without ajax function?
PHP runs on server side. Dropdown list on client side. You need somehow send the value from client to server (post, get, ajax, websocket, etc).
The answer is NO.
you can send it with a <form> tag and submit form with on dropdown onchange attribute. then get it with php and save that to database.
<form name='form1' method='post' action=''>
<?php
include('../db_inc.php');
$sql="select * from genre";
$result = mysql_query($sql) or die(mysql_error());
echo '<select onchange="form1.submit();">';
while($row = mysql_fetch_object($result)){
$option =$row->genre_name;
$value =$row->genre_id;
echo '<option value='.$value.'>'.$option.'</option>';
}
echo '</select>';
?>
</form>
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>";
}
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