php 如何在一页内传递变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1833736/
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
how to pass variables inside one page
提问by SUN Jiangong
I want to know how to pass variables from a form to a php page.
我想知道如何将变量从表单传递到 php 页面。
Thanks.
谢谢。
Edit:
编辑:
I have some input and a submit button in a form. I want my php page to search the database and display a relevant table after I click the submit button. And all the actions here are in one page.
我在表单中有一些输入和一个提交按钮。我希望我的 php 页面在单击提交按钮后搜索数据库并显示相关表。这里的所有操作都在一页中。
So I want to know how to pass variables from a form to a php script on the same page. I'm sorry for leaving so little detail.
所以我想知道如何将变量从表单传递到同一页面上的 php 脚本。我很抱歉留下这么少的细节。
Can you understand my question now? Thanks again.
你现在能明白我的问题了吗?再次感谢。
回答by Robert Greiner
EDIT: This is how you pass a user entered value from a form to a PHP page.
编辑:这是将用户输入的值从表单传递到 PHP 页面的方式。
Here is a generic form:
这是一个通用的形式:
<form method="post" action="<?php echo $_SERVER['$PHP_SELF'];?>">
<input type="text" size="12" maxlength="12" name="name">
...
...
</form>
Now for the PHP code:
现在为 PHP 代码:
<?php
$name = $_POST["name"];
?>
Note: you can change the post type between POST and GET and change the action option to send the form input to a different PHP page.
注意:您可以在 POST 和 GET 之间更改帖子类型,并更改操作选项以将表单输入发送到不同的 PHP 页面。
Are you talking about passing a variable to a function?
您是在谈论将变量传递给函数吗?
$name = 'Robert';
printName($name);
function printName($n) {
echo $n;
}
回答by dnagirl
All form variables end up in the $_POST or $_GET superglobal array (depending on the form method). If your script both displays the form and processes it, a standard method goes something like this:
所有表单变量最终都在 $_POST 或 $_GET 超全局数组中(取决于表单方法)。如果您的脚本既显示表单又处理它,则标准方法如下:
if(isset($_POST['submit'])){
//validate $_POST variables from form
//if validation works do action
//else output errors and output form again
} else {
//output form
}
回答by Sajad Bahmani
GET it from the URL
从 URL 中获取
The quickest (but most limited) way to transfer variables is by a method called GET. With GET, you append the variables onto the URL of the page you want the variables to be transferred to:
传输变量的最快(但最受限制)的方法是通过一种称为 GET 的方法。使用 GET,您可以将变量附加到您希望将变量传输到的页面的 URL 上:
http://www.matthom.com/contact.php?id=301&name=Matthom
http://www.matthom.com/contact.php?id=301&name=Matthom
The example above would give the contact.php page two variables to utilize: id, and name, whose values are 301, and Matthom, respectively.
上面的示例将为 contact.php 页面提供两个要使用的变量:id 和 name,其值分别为 301 和 Matthom。
You can add as many variables to the URL as you'd like.
您可以根据需要向 URL 添加任意数量的变量。
Beware – sometimes you don't want your variables to be shown "out in the open." Also, you are limited to 255 characters in the URL, so the variables can't contain too much information.
当心 - 有时您不希望您的变量“公开显示”。此外,您在 URL 中被限制为 255 个字符,因此变量不能包含太多信息。
From contact.php, you can GET these two variables via PHP:
从contact.php,你可以通过PHP GET这两个变量:
GRAB THE VARIABLES FROM THE URL
从 URL 中获取变量
$id = $_GET['id'];
$name =$_GET['name'];
POST it from a FORM
从表单发布
Another way to transfer variables, and by far the more robust way, is to grab them from a form.
另一种传输变量的方法,也是目前为止更可靠的方法,是从表单中获取它们。
Let's say this is your form field code:
假设这是您的表单域代码:
<form action="process.php" method="post">
<input type="text" size="25" name="searchtype" />
<input type="text" size="25" name="searchterm" />
</form>
These two input boxes allow users to enter information. At process.php, you can grab the variables in the same way:
这两个输入框允许用户输入信息。在 process.php,你可以用同样的方式获取变量:
GRAB THE VARIABLES FROM THE FORM
从表单中获取变量
$searchtype = $_POST['searchtype'];
$searchterm = $_POST['searchterm'];
Notice the use of $_POST[]over $_GET[]. This is an important distinction.
注意$_POST[]over的使用$_GET[]。这是一个重要的区别。
回答by Langdon
That's easy, just comment them out:
这很简单,只需将它们注释掉即可:
$a = 'variable 1';
//$b = 'variable 2';
$c = 'variable 2';
You see? I assigned $a, then I passed right over $b, and finally assigned $c. All inside one PHP page!!
你看?我分配了 $a,然后我直接通过了 $b,最后分配了 $c。全部在一个 PHP 页面中!!
回答by Julius F
<?php
$value = 'bla';
function getValue($string)
{
return ($_GET[$string]);
}
$value = getValue('bla');
?>
<html>
//...
<body>
<form>
<input type="text" value="<?php echo $value; ?>" />
</form>
<tr>
<td>
<tr><?php echo $value; ?></tr>
<td>
</body>
</html>
It is totally unimportant which order you use. You can put the html part into a PHP String with PHP variables inside, and echo it, php will automatically assign the vars inside. Or you seperate HTML and PHP as seen above.
您使用哪种顺序完全不重要。你可以把html部分放到一个PHP String里面,里面有PHP变量,回显一下,php会自动给里面的var赋值。或者您将 HTML 和 PHP 分开,如上所示。
Try to work with template engines, e.g. SMARTY (smarty.com)
尝试使用模板引擎,例如 SMARTY (smarty.com)

