[英]PHP submit button in echo
I'm try to build a snack machine where you can choose your snack, get the price and then click the button to pay.我正在尝试构建一个零食机,您可以在其中选择零食,获取价格,然后单击按钮付款。
Like: The price is 0,60 € 0.05(button) 0.10(button) .... if you press 0.05-button the price will reduce to 0,55€喜欢:价格是 0,60 € 0.05(button) 0.10(button) .... 如果你按下 0.05-button 价格会降到 0,55€
Why don't I get a "test" echo after I click the button?为什么我点击按钮后没有得到“测试”回声?
Here is my code这是我的代码
<?php
if(isset($_GET['mars']))
{
$mars = "0,60";
echo "Bitte Zahlen Sie noch <input type=\"button\" value=\"$mars\"> Euro<br>";
echo "<input type=\"submit\" value=\"0,05\" name=\"fcent\">";
if(isset($_GET['fcent']))
{
echo "test";
}
}
?>
First, there appears to be no form-tag in your code.
首先,您的代码中似乎没有表单标签。 Without a form tag, it would be a miracle that pressing that button actually submits it via PHP.
如果没有表单标签,按下那个按钮实际上是通过 PHP 提交它会是一个奇迹。 In other words, you need to wrap your form elements in a
<form></form>
Tag.换句话说,您需要将表单元素包装在
<form></form>
标签中。
Secondly, the nestedif
:if(isset($_GET['fcent']))
is unreachable because when you press thefcent
button;其次,嵌套的
if
:if(isset($_GET['fcent']))
无法访问,因为当您按下fcent
按钮时; the$_GET['mars']
is no more in scope and since your code explicitly demands to be run when$_GET['mars']
is SET, nothing would happen.$_GET['mars']
不在范围内,并且由于您的代码明确要求在$_GET['mars']
设置时运行,所以不会发生任何事情。 The Snippet below takes this 2 Points into account and you can fine-tune it even further to meet your needs...下面的代码段考虑了这 2 点,您可以进一步微调它以满足您的需求......
NOTE: You have to be sure that your URL reads something similar to this:http://localhost/index.php?mars=some-value
注意:您必须确保您的 URL 读取类似于以下内容:
http://localhost/index.php?mars=some-value
<?php
$mars = "0,60";
$payForm = "<form name='whatever' method='get' action=''><br>";
$payForm .= "Bitte Zahlen Sie noch <input type=\"button\" value=\"$mars\"> Euro<br>";
$payForm .= "<input type=\"submit\" value=\"0,05\" name=\"fcent\">";
$payForm .="</form>";
if(isset($_GET['mars'])){
echo $payForm;
}
if(isset($_GET['fcent'])){
echo $payForm;
echo "test";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.