2017-04-11 31 views
2

我只是tottaly與PHP的新手,我現在正在學習如何將PHP與HTML表單結合起來。我想它在一個文件中,所以我想這:表單和PHP在一個文件中

<?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> 

不幸的是,它從字面上無所事事提交後。你能幫我一點嗎?

+0

那麼,你想要做什麼?打印結果? – manian

+0

你沒有對你的變量做任何事情。你需要回應他們。 – Hunter

+0

是的,我忘了回聲!對不起。 –

回答

0

嘗試使用,以替代目前的形式行的下面一行:

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">` 
+2

由於提交將向當前目標發送HTTP POST請求,因此不需要對「PHP_SELF」的操作。這不能解決OPs「問題」,因爲他們忘記「迴應」結果。 –

1
echo $plat = "Your gender is ".$pohlavie." and your salary is ".$plat; 
+0

不提供代碼解答。 *解釋*你做了什麼。 –

1

這裏是:

<?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> 
+0

不提供代碼解答。 *解釋*你做了什麼。 –

0

始終遵循最佳的編碼實踐,並推薦使用的功能。這裏的代碼很少修改:

<?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> 

您也可以根據您的需要使用不同類型的消毒過濾器。在這裏看到:http://php.net/manual/en/filter.filters.sanitize.php

也看到這個帖子得到關於filter_input更多的知識: When to use filter_input()

希望它會幫助你學習,並按照PHP的最佳實踐。

相關問題