javascript - Jquery Ajax 表单提交给 PHP 返回空响应
问题描述
我正在尝试使用 Ajax 向 PHP 提交表单,因此我不必重新加载页面。但是当我单击提交按钮时,PHP post 数组为空,并且在访问以下值时无法获取任何值:$google_recaptcha = $_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'] ?? '');
...
表格代码
<form action="" method="post" id="appointment-form" class="login-form sign-in-form" data-toggle="validator">
<div class="text_box row">
<div class="col-lg-6">
<input type="text" name="name" id="name" placeholder="Your Name *">
</div>
<div class="col-lg-6">
<input type="email" name="email" id="email" placeholder="Your Email">
</div>
</div>
<div class="text_box row">
<div class="col-lg-6">
<input type="text" name="phone" id="phone" placeholder="Mobile Number *">
</div>
<div class="col-lg-6">
<input type="text" name="company" id="company" placeholder="Company">
</div>
</div>
<div class="text_box row col-13">
<select name="service" id="service" class="selectpickers col-12 col-lg-6 col-md-6 col-sm-6" style="margin: 5px;">
<option value="">Select Service</option>
<?php for ($i = 0; $i < count($services); $i++) { ?>
<option value="<?php echo $i; ?>"><?php echo $services[$i]; ?></option>
<?php } ?>
</select>
<select name="country" id="country" class="selectpickers col-12 col-lg-6 col-md-6 col-sm-6">
<option value="">Select Country</option>
<?php for ($j = 0; $j < count($countries); $j++) { ?>
<option value="<?php echo $j; ?>"><?php echo $countries[$j]; ?></option>
<?php } ?>
</select>
</div>
<div class="text_box row col-13">
<select name="day" id="day" class="selectpickers col-12 col-lg-6 col-md-6 col-sm-6" style="margin: 5px;">
<option value="">Select a day</option>
<?php for ($k = 0; $k < count($days); $k++) { ?>
<option value="<?php echo $k; ?>"><?php echo $days[$k]; ?></option>
<?php } ?>
</select>
<div class="help-block with-errors text-danger mt-2"></div>
<select name="timing" id="timing" class="selectpickers col-12 col-lg-6 col-md-6 col-sm-6">
<option value="">Select a time</option>
<?php for ($h = 0; $h < count($timings); $h++) { ?>
<option value="<?php echo $h; ?>"><?php echo $timings[$h]; ?></option>
<?php } ?>
</select>
</div>
<div class="form-group text_box">
<textarea name="message" id="message" placeholder="Description..."></textarea>
</div>
<?php echo csrf_token_tag(); ?>
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
<div class="g-recaptcha" data-sitekey="6Lf2c6gZAAAAABMI2STA9ciP9_kYr6dNV_uDeoD_"></div>
<div class="d-flex justify-content-between align-items-center">
<button type="submit" id="btn-submit" class="btn_three mt-4">Send Now</button>
</div>
</form>
解决方案
在与 OP 的长时间交谈中,确定他们正在使用 mod-rewrite 来隐藏.php
每个文件的扩展名。
这是由于从 URL 中删除 PHP 扩展而发生的,我猜想在 htaccess 中重写 url
鉴于这种情况,AJAX 调用中的更改应该集中在 URL 上。改变这个:
url: 'private/shared/appointment_process.php',
对此:
url: 'private/shared/appointment_process',
你的 AJAX 应该可以正常工作。
推荐阅读
- ansible - 无法使用 /etc/init.d/logstash start 启动 logstash
- nginx - NGINX 将特定目录中的所有文件作为 txt 文件提供,但不提供自动索引页面
- oracle-cloud-infrastructure - 推送自定义指标时 NotAuthorizedOrNotFound
- flutter - 即使在多个页面路由之后也保持变量值,除非在颤动中改变
- python - Chrome 驱动程序和 Chromium 二进制文件不适用于 aws lambda
- javascript - JavaScript Uncaught ReferenceError 标记未定义
- xamarin.forms - 使用 Xamarin 在 RelativeLayout 中的锚点周围居中标签
- android - 房间初始查询很慢
- r - do.call 错误,提供的元素多于要替换的元素(用 rlang::invoke 修复)[R]
- python-3.x - PysimpleGUI Calc 没有正确使用函数