PHP:获取 TEXTBOX 的值,然后将其传递给 VARIABLE
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17010975/
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
PHP: get the value of TEXTBOX then pass it to a VARIABLE
提问by Philistyne Brigid Bellisima
My Problem is:
我的问题是:
I want to get the value of textbox1 then transfer it to another page where the value of textbox1 will be appeared in the textbox2.
我想获取 textbox1 的值,然后将其传输到另一个页面,其中 textbox1 的值将出现在 textbox2 中。
Below is my codes for PHP:
以下是我的 PHP 代码:
<html>
<body>
<form name='form' method='post' action="testing2.php">
Name: <input type="text" name="name" id="name" ><br/>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
I also add the code below and the error is "Notice: Undefined index: name"
我还添加了下面的代码,错误是“注意:未定义索引:名称”
<?php
$name = $_GET['name'];
echo $name;
?>
or
或者
<?php
$name = $_POST['name'];
echo $name;
?>
回答by Expedito
In testing2.php use the following code to get the name:
在 testing2.php 中使用以下代码获取名称:
if ( ! empty($_POST['name'])){
$name = $_POST['name']);
}
When you create the next page, use the value of $name
to prefill the form field:
创建下一页时,使用 的值$name
预填充表单字段:
Name: <input type="text" name="name" id="name" value="<?php echo $name; ?>"><br/>
However, before doing that, be sure to use regular expressions to verify that the $name only contains valid characters, such as:
但是,在此之前,请务必使用正则表达式来验证 $name 是否仅包含有效字符,例如:
$pattern = '/^[0-9A-Za-zá-úá-úààüü]+$/';//integers & letters
if (preg_match($pattern, $name) == 1){
//continue
} else {
//reload form with error message
}
回答by Fabio
I think you should need to check for isset and not empty value, like form was submitted without input data so isset will be true This will prevent you to have any error or notice.
我认为您应该检查 isset 而不是空值,例如提交表单时没有输入数据,因此 isset 将为 true 这将防止您有任何错误或通知。
if((isset($_POST['name'])) && !empty($_POST['name']))
{
$name = $_POST['name']; //note i used $_POST since you have a post form **method='post'**
echo $name;
}
回答by Andy G
You are posting the data, so it should be $_POST. But 'name' is not the best name to use.
您正在发布数据,因此它应该是 $_POST。但是“名称”并不是最好的名称。
name = "name"
will only cause confusion IMO.
只会引起 IMO 的混乱。