AJAX请求后如何访问PHP超级全局发布变量 [英] How to access PHP super global post variable after AJAX request

查看:70
本文介绍了AJAX请求后如何访问PHP超级全局发布变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在index.php中有一个表单:

I have a form in index.php:

<form action="submit.php" method="post" id="registerSubmit">
    <input type="text" name="name" id="name" placeholder="Name">
    <button id="submit" type="submit">Submit</button>
</form>
<div id="result"></div>

提交表单时,它会被Jquery拦截:

When the form is submitted it gets intercepted by Jquery:

$(document).ready(function(){
    $("#registerSubmit").submit(function( e ) {
    e.preventDefault();
        $.ajax({
            url: "load.php",
            method: "post",
            data: "",
            dataType: "text",
            success: function(Result) {
                $('#result').text(Result)
            }
        })

    });
});

但是,当AJAX请求发送到load.php时.我无法访问超级全局发布变量:

However, when the AJAX request gets sent to load.php. I am not able to access the super global post variable:

<?php
$name = $_POST['name']; 
echo $name;
?>

我能够通过Jquery访问表单数据,但是我不想解析JSON以发送到Mysql.在AJAX请求之后,如何访问超级全局邮政表单数据,如果无法访问,通过Ajax向Mysql发送php表单数据的最佳实践是什么?

I'm able to access the form data through Jquery, but I didn't want to have to parse JSON to send to Mysql. How can I access the super global post form data after the AJAX request and if it's not accessible, what are best practices for sending php form data through Ajax to Mysql?

推荐答案

您需要通过AJAX调用传递数据

You need to pass data through AJAX call

$(document).ready(function(){
    $("#registerSubmit").submit(function( e ) {
    e.preventDefault();
        $.ajax({
            url: "load.php",
            method: "POST",
            data: $(this).serialize(), // Send all form data to AJAX
            dataType: "text",
            success: function(Result) {
                $('#result').text(Result)
            }
        })

    });
});

这篇关于AJAX请求后如何访问PHP超级全局发布变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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