2016-03-15 53 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