从 php 中的文本框中获取一个值,调用另一个 php 文件并将此文本框值作为变量传递给它
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16279405/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get a value from a textbox in php, call another php file and pass this text box value as a variable to it
提问by Kumaran Senapathy
I have two php pages. They both return some MySQL tables.
我有两个 php 页面。它们都返回一些 MySQL 表。
I use page1.php to return the a table called "student" from the database. I am now trying to implement a search function that would help people search through the student table for a particular name.
我使用 page1.php 从数据库中返回一个名为“student”的表。我现在正在尝试实现一个搜索功能,该功能将帮助人们在学生表中搜索特定姓名。
I have created a textbox for the search ( This is in page1.php)
我为搜索创建了一个文本框(这是在 page1.php 中)
<INPUT TYPE = "TEXT" VALUE ="search">
<INPUT TYPE = "Submit" Name = "Submit1" VALUE = "Search">
What I am trying to do is : When the user clicks on the button "search", it calls page2.php and passes the value from the textbox to a variable called "sname" in page2.php
我想要做的是:当用户单击“搜索”按钮时,它会调用 page2.php 并将文本框中的值传递给 page2.php 中名为“sname”的变量
In page2.php
在 page2.php 中
I have :
我有 :
$sname = "matt"; /*<-- Need dynamic */
$result = mysql_query("Select * from student where phone like '%$sname%'");
Now I am manually feeding the value of $sname
. How can I get the value from the textbox in page1.php to this variable?
现在我手动输入$sname
. 如何从 page1.php 中的文本框中获取该变量的值?
回答by Corné Guijt
set the code in page1.phplike:
在page1.php 中设置代码,如:
<form action='page2.php' method='post'>
<INPUT TYPE = "TEXT" NAME='search' VALUE ="search">
<INPUT TYPE = "Submit" Name = "Submit1" VALUE = "Search">
</form>
now in page 2 you can use $_POST['search'], like:
现在在第 2 页中,您可以使用 $_POST['search'],例如:
$sname = $_POST['seach'];
回答by fullybaked
Assuming that your inputs are in a form
假设您的输入是一种形式
<form action="page2.php" method="post">
<INPUT TYPE = "TEXT" VALUE ="search" name="search">
<INPUT TYPE = "Submit" Name = "Submit1" VALUE = "Search">
</form>
Then you can access it via
然后你可以通过访问它
$sname = $_POST['search'];
Note that I have added a name
attribute to your text input, this is required for the value to be in the $_POST array
请注意,我已向name
您的文本输入添加了一个属性,这是 $_POST 数组中的值所必需的
回答by Praveen kalal
page1.php
页面1.php
<form action="page2.php" method="post">
<INPUT TYPE = "text" name="sname" VALUE ="search">
<INPUT TYPE = "Submit" Name = "Submit1" VALUE = "Search">
</form>
on page2.php
在 page2.php 上
$sname= $_POST['sname']
回答by MKroeders
Your HTML misses the element FORM
您的 HTML 缺少元素 FORM
<form action="page2.php" method="GET">
<INPUT TYPE = "TEXT" VALUE ="search" name="text">
<INPUT TYPE = "Submit" VALUE = "Search">
</form>
or
或者
<form action="page2.php" method="POST">
<INPUT TYPE = "TEXT" VALUE ="search" name="text">
<INPUT TYPE = "Submit" VALUE = "Search">
</form>
The first method returns the url page2.php?text=test
. And you can access the variable with the global $_GET value
第一个方法返回 url page2.php?text=test
。您可以使用全局 $_GET 值访问变量
The second method returns the url page2.php
, but holds the data in the global POST variable, $_POST.
第二种方法返回 url page2.php
,但将数据保存在全局 POST 变量 $_POST 中。
The first method is mainly done with search things, and the second one with "private" forms. You should use get when you want people to be able to share the url, otherwise the post.
第一种方法主要是用搜索事物来完成,第二种方法是用“私人”形式。当您希望人们能够共享 url 时,您应该使用 get,否则使用 post。
Also I recommend looking at sql injection, SQL injection from php.net
我还建议查看 sql 注入,来自 php.net 的 SQL 注入
回答by Prashanth
Page1.php
页面1.php
<FORM ACTION='page2.php' METHOD='POST' >
<INPUT TYPE = "text" name="sname" VALUE ="search">
<INPUT TYPE = "Submit" Name = "Submit1" VALUE = "Search">
</form>
Now after you navigate to Page2: This code can do the job
现在导航到 Page2 后:此代码可以完成这项工作
Page2.php
页面2.php
//Check if the paramerter is set and it is not equal to NULL
if( isset($_POST['sname']) && $_POST['sname'] != NULL )
{
//now load the variable
$sname = $_POST['sname'];
//Prevent SQL injection..
$sname = addslashes($sname);
$query = "select * from table_name where column_name like '%$sname%'";
//Execute the query and store in $result
$result = mysql_query($query);
// Fetch values from result
while ( $rows = mysql_fetch_array[$result] )
{
//Get the values here..
}
}
I assume that you are verymuch aware of SQL Injection.
我假设您非常了解 SQL 注入。
回答by Brucee
<form action='page2.php' method='post'>
<INPUT TYPE = "TEXT" VALUE ="name">
<INPUT TYPE = "SUBMIT" VALUE ="search">
<INPUT TYPE = "Submit" Name = "Submit1" VALUE = "Search">
In page2.php
在 page2.php 中
$name=$_POST['name']
回答by chandresh_cool
Put this values in a form, give textfield a name let say name="fieldname" and give it a method let say post, then access value like this
把这些值放在一个表单中,给文本字段一个名字让我们说 name="fieldname" 并给它一个方法让我们说 post,然后像这样访问值
$sname = $_POST['fieldname'];