PHP 在回显中提交按钮
PHP submit button in echo
我正在尝试构建一个零食机,您可以在其中选择您的零食,获取价格,然后点击按钮支付。
喜欢:
价格是 0,60 欧元
0.05(按钮)0.10(按钮)....
如果您按下 0.05 按钮,价格将降至 0,55€
为什么我在单击按钮后没有收到 "test" 回显?
这是我的代码
<?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. In other words, you need to wrap your form elements in a <form></form>
Tag.
Secondly, the nested if
: if(isset($_GET['fcent']))
is unreachable because when you press the fcent
button; 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. The Snippet below takes this 2 Points into account and you can fine-tune it even further to meet your needs...
NOTE: You have to be sure that your URL reads something similar to this: 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";
}
我正在尝试构建一个零食机,您可以在其中选择您的零食,获取价格,然后点击按钮支付。
喜欢: 价格是 0,60 欧元 0.05(按钮)0.10(按钮).... 如果您按下 0.05 按钮,价格将降至 0,55€
为什么我在单击按钮后没有收到 "test" 回显?
这是我的代码
<?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. In other words, you need to wrap your form elements in a
<form></form>
Tag.
Secondly, the nestedif
:if(isset($_GET['fcent']))
is unreachable because when you press thefcent
button; 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. The Snippet below takes this 2 Points into account and you can fine-tune it even further to meet your needs...
NOTE: You have to be sure that your URL reads something similar to this: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";
}