PHP表单无法正常工作

时间:2016-03-15 15:30:30

标签: php

用户在输入姓名和年龄后提交时,代码无效。页面应提交到同一页面,HTML表单也应与下面的结果一起显示。请帮帮我们!!

<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="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['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]";
    }
?>

1 个答案:

答案 0 :(得分:0)

你需要给出名字以获得帖子名称

<input name="yourformname" ...

之后你可以查看帖子名称

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

一定是这样的

 <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]";
    }
?>