简体   繁体   English

将下拉列表中的值从一个 php 传递到另一个

[英]Passing value from a dropdown list from one php to another

I have我有

  1. A dropdown list populated from a MySQL table.从 MySQL 表填充的下拉列表。
  2. A button.一个按钮。
  3. Another php page另一个php页面

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.单击按钮时,将从下拉列表中选择的值作为变量传递给第二个 php 页面。

  2. Pass that variable into a mysql query on the second php page.将该变量传递到第二个 php 页面上的 mysql 查询中。 eg: $result = mysql_query("Select * from table where name like "$drop_down_value");例如:$result = mysql_query("Select * from table where name like "$drop_down_value");

I am new to php and pardon my naivety.我是 php 的新手,请原谅我的天真。

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:这是我的 HTML 部分:

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.假设我在另一个 php 上使用了类似的查询。但是会使用在此页面上选择的值作为该查询中的变量。

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'] .通过使用 POST 方法将下拉列表包装在表单中,您可以将值发送到下一页并通过$_POST['your_field_name']检索。 See the docs .请参阅文档

Your page1 will have a form something like您的 page1 将有一个类似于

<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在 page2.php 中你可以做一些类似的事情

$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!) (但请确保在 page2.php 上使用之前清除用户输入!)

you will need javascript to do this.您将需要 javascript 来执行此操作。 The page already finish loading.该页面已经完成加载。 php script wont work after page finish loading.页面加载完成后,php 脚本将无法工作。 try jquery, ez to use尝试 jquery,ez 使用

The answer to your question is to use the $_POST or $_GET superglobal variables in PHP. You can use either of these to obtain the value of the dropdown list from the first page after the button is clicked.您的问题的答案是使用 PHP 中的 $_POST 或 $_GET 超全局变量。您可以使用其中任何一个在单击按钮后从第一页获取下拉列表的值。 For example, you could use the following code to obtain the value of the dropdown list:例如,您可以使用以下代码获取下拉列表的值:

$drop_down_value = $_POST['dropdown'];

Then, you can pass this variable into your MySQL query on the second page, as you have outlined in your question:然后,您可以将此变量传递到第二页上的 MySQL 查询中,如您在问题中所述:

$result = mysql_query("Select * from table where name like "$drop_down_value");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM