表单和PHP在一个文件中
我只是PHP的新手,现在正在学习如何将PHP与HTML表单相结合。我想把它放在一个文件中,所以我尝试了以下方法:表单和PHP在一个文件中,php,html,forms,Php,Html,Forms,我只是PHP的新手,现在正在学习如何将PHP与HTML表单相结合。我想把它放在一个文件中,所以我尝试了以下方法: <?php if(isset($_POST['button'])){ //check if form was submitted $pohlavie = $_POST['gender']; //get input text $plat = $_POST['salary']; //get input text $plat = "Your gender is "
<?php
if(isset($_POST['button'])){ //check if form was submitted
$pohlavie = $_POST['gender']; //get input text
$plat = $_POST['salary']; //get input text
$plat = "Your gender is ".$pohlavie." and your salary is ".$plat;
}
?>
<center><h1>TAXES</h1></center>
<form action="" method="post">
Name: <input type="text" name="name"><br>
<input type="radio" name="gender" value="female"> Female<br>
<input type="radio" name="gender" value="male"> Male<br>
Salary: <input type="number" name="salary"><br>
<button type="submit" name="button" formmethod="post">Calculate DPH</button>
</form>
税
名称:
女性
男性
工资:
计算DPH
不幸的是,它在提交后实际上什么也没做。您能帮我一点忙吗?尝试使用以下行代替当前的表单行:
echo $plat = "Your gender is ".$pohlavie." and your salary is ".$plat;
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">`
以下是:
<?php
if(isset($_POST['button'])){ //check if form was submitted
$pohlavie = $_POST['gender']; //get input text
$plat = $_POST['salary']; //get input text
echo "Your gender is ".$pohlavie." and your salary is ".$plat;
}
?>
<center><h1>TAXES</h1></center>
<form action="" method="post">
Name: <input type="text" name="name"><br>
<input type="radio" name="gender" value="female"> Female<br>
<input type="radio" name="gender" value="male"> Male<br>
Salary: <input type="number" name="salary"><br>
<button type="submit" name="button" formmethod="post">Calculate DPH</button>
</form>
税
名称:
女性
男性
工资:
计算DPH
始终遵循最佳编码实践并使用推荐的功能。下面是经过少量修改的代码:
<?php
if (filter_has_var(INPUT_POST, "button")) { //check if form was submitted
$pohlavie = filter_input(INPUT_POST, 'gender'); //get input text
$salary = filter_input(INPUT_POST, 'salary');
echo $plat = "Your gender is ".$pohlavie." and your salary is ".$salary;
}
?>
<center><h1>TAXES</h1></center>
<form action="" method="post">
Name: <input type="text" name="name"><br>
<input type="radio" name="gender" value="female"> Female<br>
<input type="radio" name="gender" value="male"> Male<br>
Salary: <input type="number" name="salary"><br>
<button type="submit" name="button" formmethod="post">Calculate DPH</button>
</form>
税
名称:
女性
男性
工资:
计算DPH
您还可以根据需要使用不同类型的消毒过滤器。请看这里:
另请参阅本篇文章,了解有关过滤器输入的更多信息:
希望它能帮助您学习并遵循PHP的最佳实践。那么,您想做什么?打印结果?您没有对变量执行任何操作。你需要回音。是的,我忘了回音!对不起,不鼓励只回答代码问题。解释你做了什么。不鼓励只回答代码。解释您所做的。不需要执行PHP\u SELF
操作,因为提交将向当前目标发送HTTP POST请求。这并不能解决OPs的“问题”,因为他们忘记了echo
结果。