Javascript jQueryAjax表单提交到PHP返回空响应

Javascript jQueryAjax表单提交到PHP返回空响应,javascript,php,jquery,Javascript,Php,Jquery,我正在尝试使用Ajax向PHP提交表单,这样就不必重新加载页面。但当我单击Submit按钮时,PHP post数组为空,在访问以下值时无法获取任何值:$google\u recaptcha=$\u post['recaptcha'] 有什么建议吗 $(document).ready(() => { $("#appointment-form").on('submit', e => { e.preventDefault(); c

我正在尝试使用Ajax向PHP提交表单,这样就不必重新加载页面。但当我单击Submit按钮时,PHP post数组为空,在访问以下值时无法获取任何值:
$google\u recaptcha=$\u post['recaptcha']

有什么建议吗

$(document).ready(() => {
    $("#appointment-form").on('submit', e => {
        e.preventDefault();
        console.log('Event triggered!');

        const name = $("#name").val();
        const email = $("#email").val();
        const phone = $("#phone").val();
        const company = $("#company").val();
        const service = $("#service").val();
        const country = $("#country").val();
        const day = $("#day").val();
        const timing = $("#timing").val();
        const message = $("#message").val();
        const csrfToken = $('input[name="csrf_token"]').val();

        $.ajax({
            type: 'post',
            url: 'private/shared/appointment_process.php',
            data: {
                name: name,
                email: email,
                phone: phone,
                company: company,
                service: service,
                country: country,
                day: day,
                timing: timing,
                message: message,
                csrfToken: csrfToken,
                recaptcha: grecaptcha.getResponse()
            },
            success: (result) => {
                console.log('Got response back');
                console.log(result);
                if (result === "Success") {
                    $("#form-success").html('Message has been sent!');
                    $("#form-success").show();
                } else {
                    $("#form-error").html(result);
                    $("#form-error").show();
                }
            }
        });

    });
});
PHP代码

<?php

require_once('../initialize.php');

$google_recaptcha = $_POST['recaptcha']  ?? '';
$name = h($_POST['name']  ?? '');
...


您需要使用
数据:{recaptcha:grecaptcha.getResponse()}
来正确发布它

给你:

$(document).ready(() => {
    $("#appointment-form").on('submit', e => {
        e.preventDefault();
        console.log('Event triggered!');

        var name = $("#name").val();
        var email = $("#email").val();
        var phone = $("#phone").val();
        var company = $("#company").val();
        var service = $("#service").val();
        var country = $("#country").val();
        var day = $("#day").val();
        var timing = $("#timing").val();
        var message = $("#message").val();
        var csrfToken = $('input[name="csrf_token"]').val();

        $.ajax({
            type: 'post',
            url: 'private/shared/appointment_process.php',
            data: {"name": name, "email": email, "phone": phone, "company": company, "service": service, "country": country, "day": day, "timing": timing, "message": message, "csrfToken": csrfToken, "recaptcha": grecaptcha.getResponse()},
            success: (result) => {
                console.log('Got response back');
                console.log(result);
                if (result === "Success") {
                    $("#form-success").html('Message has been sent!');
                    $("#form-success").show();
                } else {
                    $("#form-error").html(result);
                    $("#form-error").show();
                }
            }
        });

    });
});

数据字段的格式不应该是这样的吗

我以为查询字符串只用于GET

$.ajax({
    type: 'post',
    url: 'private/shared/appointment_process.php',
    data: {
       "name" : name,
       "email" : email,
       "phone" : phone,
       "company" : company
    },
    success: (result) => {
        console.log('Got response back');
        console.log(result);
        if (result === "Success") {
            $("#form-success").html('Message has been sent!');
            $("#form-success").show();
        } else {
            $("#form-error").html(result);
            $("#form-error").show();
        }
     }
 });

您需要以不同的方式传递数据,如

$.ajax({
        url: 'URL',
        type: 'POST',
        data: { Parameter1: "Value", Parameter2: "Value"} ,
        success: function (response) {
            alert(response.status);
        },
        error: function () {
            alert("error");
        }
    }); 
在PHP中,如果您尝试代码

<?php
echo $_POST['Parameter1'];
?>


将从ajax请求返回参数1 Send的值。在与OP的长时间聊天期间,确定他们正在使用mod rewrite隐藏每个文件的
.php
扩展名

我想这是因为从URL中删除了PHP扩展 在htaccess中重写url

在这种情况下,AJAX调用中的更改应该集中在URL上。更改此项:

url: 'private/shared/appointment_process.php',
为此:

url: 'private/shared/appointment_process',

而且你的AJAX应该能正常工作。

评论不用于进一步讨论;这段对话已经结束。
url: 'private/shared/appointment_process',