2016-03-15 51 views
-1

用戶在輸入姓名和年齡後提交時,代碼不起作用。頁面應提交到同一頁面,並且HTML表單也應與下面的結果一起顯示。請幫助我的傢伙!PHP表單不工作

<html> 
 
<head> 
 
\t <title>My first PHP page</title> 
 
</head> 
 
<body> 
 
\t \t <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <!-- $_SERVER['PHP_SELF'] array --> 
 
\t \t \t Name: <input type="text" name="name"/> 
 
\t \t \t Age: <input type="text" name="age"/> 
 
\t \t \t <input type="submit"/> 
 
\t \t </form> 
 

 

 
</body> 
 
</html>

<?php 
/* because both the HTML form data and the PHP processing code are in the same script, you will need a conditional check to 
    see if the form has been submitted */ 

    if (isset($_POST['submit'])) { //was the form submitted? 
     print "Raveen"; 
     //echo "Welcome ". $_POST["name"] . "<br>"; 
     //echo "You are $_POST["age"] years old<br>"; 
     //echo "The path to this file is: $_SERVER[PHP_SELF]"; 
    } 
?> 

回答

0

你需要給名之後,要獲得職位名稱

<input name="yourformname" ... 

可以檢查崗位名稱

like: if (isset($_POST['yourformname'])) { 

它萬畝t是這樣的

<input name="submit" type="submit" /> 

也有另一種方式來了解如何獲得該職位。 forexmaple在表單中創建一個隱藏的輸入,如:

<html> 
    <head> 
     <title>My first PHP page</title> 
    </head> 
    <body> 
      <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <!-- $_SERVER['PHP_SELF'] array --> 
       Name: <input type="text" name="name"/> 
       Age: <input type="text" name="age"/> 
<input type="hidden" name="gotit"/> 
       <input type="submit"/> 
      </form> 


    </body> 
    </html> 

之後,你可以檢查

<?php 
/* because both the HTML form data and the PHP processing code are in the same script, you will need a conditional check to 
    see if the form has been submitted */ 

    if (isset($_POST['gotit'])) { //was the form submitted? 
     print "Raveen"; 
     //echo "Welcome ". $_POST["name"] . "<br>"; 
     //echo "You are $_POST["age"] years old<br>"; 
     //echo "The path to this file is: $_SERVER[PHP_SELF]"; 
    } 
?> 
+0

謝謝你,先生!我在幾分鐘內解決了我的問題+瞭解了額外的點(隱藏的輸入)。 – Ishani