如何在我的 HTML 代码中检索 PHP 变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5849791/
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 do I retrieve a PHP variable in my HTML code?
提问by DG1972
I call a PHP script from my HTML form submit.
我从我的 HTML 表单提交中调用了一个 PHP 脚本。
Then I process the values retrieved from this HTML form in the PHP script and now I want to display these values in another HTML form that I am calling from this PHP script. How do I retrieve this variable?
然后我在 PHP 脚本中处理从这个 HTML 表单中检索到的值,现在我想在我从这个 PHP 脚本调用的另一个 HTML 表单中显示这些值。我如何检索这个变量?
Note: I tried echo
but I think I actually want to display this value in the form (HTML) and not break it again in a PHP tag.
注意:我尝试过,echo
但我认为我实际上想在表单 (HTML) 中显示此值,而不是在 PHP 标记中再次破坏它。
回答by ferguior
Once you got the values in the PHP script, are you calling a new script? If so, you might wanna save the values in $_SESSION["varible_name"]
. If not, you just have to echo it.
获得 PHP 脚本中的值后,您是否正在调用新脚本?如果是这样,您可能希望将值保存在$_SESSION["varible_name"]
. 如果没有,你只需要回应它。
回答by amccormack
It depends on how you are accessing your form data, either through $_POST
or through $_GET
. For simplicity, I'll assume your using $_GET
and modify this examplefor more clarity.
这取决于您访问表单数据的方式,是通过$_POST
还是通过$_GET
。为简单起见,我假设您使用$_GET
并修改此示例以更清晰。
So lets say you have a form hosted on welcome.php:
因此,假设您在welcome.php 上托管了一个表单:
<form action="welcome.php" method="get">
Name: <input type="text" name="fname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
Now the results will be returned back to the same page, so you want to modify the page to say:
现在结果会返回到同一个页面,所以要修改页面说:
<form action="welcome.php" method="get">
Name: <input type="text" name="fname" value="<?php echo $_GET["fname"]; ?>"/>
Age: <input type="text" name="age" value="<?php echo $_GET["age"]; ?>" />
<input type="submit" />
</form>
Though you'll notice that we're using the same page, and we can only have one version of the page, so we want to render if upon the condition that our variable has been set.
尽管您会注意到我们使用的是同一个页面,而且我们只能拥有该页面的一个版本,因此我们希望在设置了变量的条件下进行渲染。
if (isset($_GET["fname"])){
//code to print second form
}
else{
//code to print first form
}
Or, in another way (using the ternary operator):
或者,以另一种方式(使用三元运算符):
<form action="welcome.php" method="get">
Name: <input type="text" name="fname" value="<?php echo ((isset($_GET["fname"]))?$_GET["fname"]:""); ?>"/>
Age: <input type="text" name="age" value="<?php echo ((isset($_GET["age"]))?$_GET["age"]:""); ?>" />
<input type="submit" />
</form>
回答by matzahboy
I'm not sure what you mean by "not breaking it again with a PHP tag". HTML on its own cannot access PHP variables. This is because PHP is a server-side language and HTML is a client side language. But here is the simplest way to print php variables retrieved from a form.
我不确定“不要用 PHP 标签再次破坏它”是什么意思。HTML 本身无法访问 PHP 变量。这是因为 PHP 是一种服务器端语言,而 HTML 是一种客户端语言。但这是打印从表单中检索到的 php 变量的最简单方法。
<form>
<p>Your name: <?php echo $_POST['name'] ?></p>
</form>
This is assuming that there was a form that submitted a field called 'name' using POST.
这是假设有一个表单使用 POST 提交了一个名为“name”的字段。
You can process the name in a php script at the top of the file and then simply echo it when you're printing the html. This way, you won't have too much php code mixed in with the HTML (which makes it look cleaner).
您可以在文件顶部的 php 脚本中处理名称,然后在打印 html 时简单地回显它。这样,您就不会在 HTML 中混入太多 php 代码(这使它看起来更干净)。