Passing value from a dropdown list from one php to

2019-08-28 11:18发布

I have

  1. A dropdown list populated from a MySQL table.
  2. A button.
  3. Another php page

What I need to do:

  1. On clicking the button, pass the value selected from the dropdown list as a variable to second php page.

  2. Pass that variable into a mysql query on the second php page. eg: $result = mysql_query("Select * from table where name like "$drop_down_value");

I am new to php and pardon my naivety.

This is my code to get values for the dropdown list:

function dropdown_query()
{
mysql_connect("localhost","root","root") or die("Failed to connect with database!!!!");
mysql_select_db("student_test") or die("cannot select DB");
$result = mysql_query("Select * from marks");
if($result=== FALSE){
die(mysql_error());
}
while($r=mysql_fetch_array($result))
{
echo '<option value="' .$r['marks'] . '">' . $r['marks'] . '</option>';

}

and this is my HTML part:

select name="dropdown" onchange="somefunc()">
<option value="">Select...</option>
<?php dropdown_query()?>
</select>

Lets say I use a similar query on another php. But would use the value selected on this page as a variable in that query.

3条回答
Melony?
2楼-- · 2019-08-28 11:55

you will need javascript to do this. The page already finish loading. php script wont work after page finish loading. try jquery, ez to use

查看更多
Emotional °昔
3楼-- · 2019-08-28 11:57

Your page1 will have a form something like

<form action="page2.php" method="post">
 <p>Name: <input type="text" name="name" /></p>
 <p><input type="submit" /></p>
</form>

And in page2.php you can do something along the lines of

$name = $_POST['name'];
$sql  = "SELECT * FROM table WHERE name LIKE '$name'";
...

(But make sure to scrub the user input before using it on page2.php!)

查看更多
Ridiculous、
4楼-- · 2019-08-28 12:06

By wrapping the drop down in a form with POST method, you can send the value to the next page and retrieve via $_POST['your_field_name']. See the docs.

查看更多
登录 后发表回答