gpt4 book ai didi

javascript - 在 AJAX 请求中传递表单数据时出现问题

转载 作者:行者123 更新时间:2023-12-02 17:29:59 24 4
gpt4 key购买 nike

我在通过 AJAX 请求将表单值传递给 PHP 时遇到问题。根据我下面的代码,变量可以传回,因此我怀疑问题可能出在 data: $('#signup-form').serialize()

JS:

$.ajax
({
type: "POST",
url: "http://www.domain.com/includes/register.php",
data: $('#signup-form').serialize(),
success: function(data)
{
$('#signup-response').hide();
$('#signup-response').fadeIn();
$('#signup-response').html(data);
},
error: function()
{
alert("fail");
}
})

表格:

<form id="signup-form" action="" method="POST">
<input name="email" type="email" class="form-control" id="signupEmail" placeholder="Email address">
<input name="password1" type="password" class="form-control" id="signupPassword" placeholder="Password">
<input name="password2" type="password" class="form-control" id="signupPassword2" placeholder="Password">
<select name="country" id="signupCountry" class="selectpicker">
<option value="0">Country</option>
<option>United States</option>
<option>United Kingdom</option>
<option>Canada</option>
</select>
<select name="gender" id="signupGender" class="selectpicker">
<option value="0">Gender</option>
<option>Female</option>
<option>Male</option>
</select>
<button id="signup" class="btn btn-success btn-block signup" type="submit">Sign up</button>
</form>

注册.php

<?php
$username = $_POST['signupEmail'];
echo "hello"; // works
echo $username; // doesn't work
?>

最佳答案

试试这个,你需要使用输入名称 <input name="email" type="email" class="form-control" id="signupEmail" placeholder="Email address">而是使用输入字段 id。

 $username = $_POST['email'];

而不是

  $username = $_POST['signupEmail'];

关于javascript - 在 AJAX 请求中传递表单数据时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23158642/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com