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>
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++;
}
hi i want to get the value the user selected from the dropdown menu without submitting a form and save it in a php variable any ideas ??
<select name="car" value="Select" size="1">
<?php
$sql = "SELECT fullname FROM users";
$result = mysql_query($sql) or die (mysql_error());
while ($row = mysql_fetch_array($result))
{
$name=$row["fullname"];
$options .= '<option value="'.$name.'">'.$name.'</option>';
}
?>
<?php echo $options; ?>
</option>
</select>
I don't think you fully understand how PHP works server side, but to get the value of a dropdown menu without submitting it you'll need Javascript (jQuery makes everything easier). From there you just send an AJAX request using JSON as data format and retrieve it from PHP with json_decode.
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.
My problem is that I try to load a drop down based on selection from previous drop down. I'm loading the values from database. but when I use $_get['emirate'] I'm not getting the value. It says the value is not set.
$data = mysql_query("SELECT * FROM emirate")
or die(mysql_error());
Print "<table border cellpadding=3>";
print"<tr><th><form method=get action='index.php'><select id=EMIRATE size=1><OPTION value=all>all</option>";
while($info = mysql_fetch_array( $data ))
{
Print "<option value=". $info['em_name'] .">".$info['em_name']."</option>";
}
print"</select></form></th>";
if(isset($_GET['EMIRATE'])){
$name=$_GET['EMIRATE'];
echo $name;
$data = mysql_query("SELECT a_name FROM areas where em_id=(select em_id from emirate where em_name=\"".$name."\")")
or die(mysql_error());
Print "<th><select name=AREAS size=1>";
while($info = mysql_fetch_array( $data ))
{
Print "<option >".$info['a_name']."</option>";
}
print"</select></th>";
}
I found out the problem.
I haven't put a submit button and hence my form wasn't getting
submitted. And hence I wasn't getting any value from $_GET.
http://php.net/manual/en/reserved.variables.get.php
Now I get the results smoothly.
which method are you using on your form on the previous page, post or get?
you can replace $_GET with $_REQUEST which retrieves values from both post and get.
<select id=EMIRATE size=1><OPTION value=all>all</option>
Please fix the HTML.... you miss to set the name attribute for your select...
<select id="EMIRATE" size="1" name="EMIRATE">
<option value="all">all</option>
....
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().