PHP post方法

时间:2014-12-28 13:57:33

标签: php html

我的welcome.php代码如下:

<html>
<body>

Welcome <?php echo $_POST["name"]; ?><br>
Your email address is: <?php echo $_POST["email"]; ?>

</body>
</html>

和表格:

<html>
<body>

     <form action="welcome_get.php" method="get">
     Name: <input type="text" name="name"><br>
      E-mail: <input type="text" name="email"><br>
    <input type="submit">
</form>

</body>
</html>

当我使用我的姓名和emailid填写表单时,它应该输出我的姓名和电子邮件ID。 但它给了我运行php文件的以下错误

Welcome 
( ! ) Notice: Undefined index: name in C:\wamp\www\PHP Learning\welcome.php on line 4
Call Stack
#   Time    Memory  Function    Location
1   0.0000  236440  {main}( )   ..\welcome.php:0

Your email address is: 
( ! ) Notice: Undefined index: email in C:\wamp\www\PHP Learning\welcome.php on line 5
Call Stack
#   Time    Memory  Function    Location
1   0.0000  236440  {main}( )   ..\welcome.php:0

对应于当我运行html文件时应该给我正确的输出,它只显示

在地址栏中我得到了这个:

LH/PHP%20Learning/welcome_get.php?name=shailesh+kumar&email=shailblack%40gmail.com

Not Found///

The requested URL /PHP Learning/welcome_get.php was not found on this server.

Apache/2.4.9 (Win64) PHP/5.5.12 Server at localhost Port 80

请建议我缺少执行post方法的地方。

6 个答案:

答案 0 :(得分:1)

您正在直接访问POST变量,但您的表单是使用GET方法提交的。

顺便说一句,你不应该 以你在你的例子中所做的方式使用全局变量。

答案 1 :(得分:0)

将您的php文件更改为

<html>
<body>

    Welcome <?php echo isset($_POST["name"]) ? $_POST["name"] : ''; ?><br>
    Your email address is: <?php echo isset($_POST["email"]) ? $_POST["email"] : ''; ?>

</body>
</html>

第一次执行脚本时,$_POST变量为空数组。这就是为什么你有错误。

然后正如其他人告诉你的那样 - 改变你的方法发布。

答案 2 :(得分:0)

您使用的方法是get

您必须将其更改为post

有关详细信息,请参阅this文章。

答案 3 :(得分:0)

您使用<form action="welcome_get.php" method="get">执行GET请求,但使用$_POST['name']获取该值。

所以试试下面的例子

的welcome.php

<html>
<body>

Welcome <?php echo $_POST["name"]; ?><br>
Your email address is: <?php echo $_POST["email"]; ?>

</body>
</html>

和表格:

<html>
<body>

     <form action="welcome.php" method="post">
     Name: <input type="text" name="name"><br>
      E-mail: <input type="text" name="email"><br>
    <input type="submit">
</form>

</body>
</html>

答案 4 :(得分:0)

您的脚本正在请求网址welcome_get.php,但您将文件命名为welcome.php - 您需要更改其中一个。{1}}您的代码目前正在搜索不存在的文件。

如果您想发布使用method="POST"而不是method="get"

当您加载页面时,您的脚本会查找$ _POST [&#39; name&#39;]和$ _POST [&#39; email&#39;]。如果您发布任何数据,会发生什么?好吧,因为你已经看到你得到错误。

在呈现变量之前检查变量是否存在,因为您无法保证它们的存在。

if(isset($_POST['email']) {
    echo $_POST['email'];
}

这将检查$ _POST [&#39; email&#39;]是否存在。如果确实如此,它将回显它的价值,如果它没有,那么它将不会显示任何内容。

答案 5 :(得分:0)

更改

<form action="welcome_get.php" method="get">

<form action="welcome_get.php" method="post">

当您发布值时..阅读更多here

再次,你没有一个名为welcome_get.php的文件(我从你得到的错误中理解)。它应该是welcome.php。所以要替换ypur <form>标签,如下所示

<form action="welcome.php" method="post">
相关问题