PHP中POST方法的问题 [英] Issues with POST method in PHP

查看:97
本文介绍了PHP中POST方法的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:

表单提交后的未定义POST变量。



完成研究和疑难排解:


  • 在这里的大量问题中,几乎所有的问题都与表单字段上没有名称标签有关。
  • 配置我的PHP.ini使$ HTTP_RAW_POST_DATA设置为-1

  • 遵循PHP中的教程.net和W3SChools



此时我迷路了。数据只是拒绝发布,它都返回undefined。下面是显示问题的HTML,PHP和两个截图。



我在Windows上使用PHPStorm的内置服务器。



signup.html

 < div class =text-center col-md -4 col-md-offset-4> 
< form id =user_signupclass =form-horizo​​ntal signInFieldsaction =../ php / register.phpmethod =POST>
< input type =textid =first_namename =first_nameplaceholder =First Name>
< input type =textid =last_namename =last_nameplaceholder =Last Name>
< input type =emailid =user_emailname =user_emailplaceholder =Email>
< input type =textid =user_idname =user_idplaceholder =User ID>
< input type =passwordid =user_passwordname =user_passwordplaceholder =Password>
< input type =passwordid =confirm_passwordname =confirm_passwordplaceholder =确认密码>
< button id =btn_signuptype =submitname =signup_button> Sign Me Up!< / button>
< / form>

register.php

  //注册表单中的变量POST动作
$ first_name = $ _POST [first_name];
$ last_name = $ _POST [last_name];
$ user_email = $ _POST [user_email];
$ user_id = $ _POST [user_id];
$ user_password = $ _POST [user_password];
$ confirm_password = $ _POST [confirm_password];

//向数据库添加一个新用户
$ conn = new mysqli($ servername,$ username,$ password);
$ testQuery = mysql_insert($ first_name,$ last_name,$ user_email,$ user_id,$ user_password);
$ b $ if($ conn-> query($ testQuery)=== TRUE){
echoNew Record Created Successfully!;
} else {
echoError:。 $ testQuery。 <峰; br> 中。 $ conn->误差;
}

$ conn-> close();

填充字段的表单



提交后输出:



此时我很困惑。据我可以告诉W3Schools,PHP.net和各种问题,我已经正确设置了它。但是有些事情显然是关闭的任何和所有的帮助将不胜感激。

解决方案

编辑

您正在使用 PHPStorm 的。

  • 检查 echo file_get_contents(php:// input); 以格式显示数据: first_name = John& last_name = Doe .... 。请参阅 php.net

  • 检查 var_dump($ _ POST)是否显示数组中的表单数据。


    希望它能帮助你。欢迎评论。


    The Issue:

    Undefined POST variables after form submission.

    Research and Troubleshooting Done:

    • Read over a multitude of questions here, almost all had to do with not having a name tag on the form field. All of my fields have a tag and ID present.
    • Configured my PHP.ini to have $HTTP_RAW_POST_DATA set to -1
    • Followed tutorials across PHP.net and W3SChools

    At this point I'm lost. The data simply refuses to post, it all comes back undefined. Below is the HTML, PHP, and two screenshots showing the issue.

    I am using PHPStorm's built in server on Windows.

    signup.html

    <div class="text-center col-md-4 col-md-offset-4">
            <form id="user_signup" class="form-horizontal signInFields" action="../php/register.php" method="POST">
                <input type="text" id="first_name" name="first_name" placeholder="First Name">
                <input type="text" id="last_name" name="last_name" placeholder="Last Name">
                <input type="email" id="user_email" name="user_email" placeholder="Email">
                <input type="text" id="user_id" name="user_id" placeholder="User ID">
                <input type="password" id="user_password" name="user_password" placeholder="Password">
                <input type="password" id="confirm_password" name="confirm_password" placeholder="Confirm Password">
                <button id="btn_signup" type="submit" name="signup_button">Sign Me Up!</button>
            </form>
    

    register.php

    // Variables from the sign-up form POST action
    $first_name = $_POST["first_name"];
    $last_name = $_POST["last_name"];
    $user_email = $_POST["user_email"];
    $user_id = $_POST["user_id"];
    $user_password = $_POST["user_password"];
    $confirm_password = $_POST["confirm_password"];
    
    // Add a new user to the database
    $conn = new mysqli($servername, $username, $password);
    $testQuery = mysql_insert($first_name,$last_name,$user_email,$user_id,$user_password);
    
    if($conn->query($testQuery) === TRUE){
        echo "New Record Created Successfully!";
    } else {
        echo "Error: " . $testQuery . "<br>" . $conn->error;
    }
    
    $conn->close();
    

    Form With Fields Filled In

    Output After Submission:

    At this point I'm baffled. As far as I can tell from W3Schools, PHP.net, and various questions here I've got it setup properly. However something is clearly off. Any and all help would be greatly appreciated.

    解决方案

    Edit:

    You are using PHPStorm's built-in web server, which has some issues right now especially with POST requests, e.g. WEB-17317. You can also refer to this answer in this respect.

    And that might be the reason you were unable to process your form.

    Hence, the best solution would be to configure your PHPStorm to use your local web server, XAMPP in your case, for your current project. Follow the below steps for same:

    1. Configure your local server's virtual hosts with current project directory as root directory
    2. Make sure you can access the project files after your configuration in the above step. For example: http://localhost/signup.html should be accessible via browser.
    3. Now, in PHPStorm, go to Run -> Edit Configuration and select each file and configure the url for each file as show in below image. E.g. for signup.html file use http://localhost/signup.html.
      • You do not need to set url (Step 3) for all files in your project. Just do it for the default (starting) file - e.g. index.html or login.html or signup.html. All other files linked with <a> or action="register.php" in forms will automatically be served with the local server.

    Now, you can use the green button in PHPStorm to run your HTML/PHP files and it will open these files in your browser with the url you configured in Step 1. And you should be able to process your forms without any issue.


    But if your issue is caused by some other reasons, please consider checking below points:

    • The form fields should have name attribute, e.g. "name='first_name"` in signup.html file.
    • echo $_SERVER["REQUEST_METHOD"] == "POST" prints 1 when written in register.php file which means that your form is being submitted successfully.
    • The value post_max_size in your php.ini is set in #M format, e.g. 10M, any other format like 10MB is invalid. Refer php.net.
    • Check if echo file_get_contents("php://input"); displays data in format: first_name=John&last_name=Doe ..... Refer php.net.
    • Check if var_dump($_POST) displays form data in an array.

    Hopefully, it helps you. Feel free to comment.

    这篇关于PHP中POST方法的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆