Passing value from a dropdown list from one php to another

自闭症网瘾萝莉.ら 提交于 2020-01-06 20:23:46

问题


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.


回答1:


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.




回答2:


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!)




回答3:


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



来源:https://stackoverflow.com/questions/16220721/passing-value-from-a-dropdown-list-from-one-php-to-another

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!