【发布时间】:2021-09-26 11:45:24
【问题描述】:
有人可以告诉我为什么这段代码不起作用我想通过 post 方法将表单数据发送到 PHP,但是我的 PHP 没有收到任何请求 这是我的注册表单 signupmodal.php
<form action="<?php htmlentities($_SERVER['PHP_SELF']) ?>" method="post" id="signupForm">
<div class="form-group">
<label for="forUserName">User Name:</label>
<input type="text" name="username" id="username" class="form-control" placeholder="Enter User Name" aria-describedby="helpId">
</div>
<div class="form-group">
<label for="forEmail">Email:</label>
<input type="email" name="email" id="email" class="form-control" placeholder="Enter Your Email" aria-describedby="helpId">
</div>
<div class="form-group">
<label for="">password:</label>
<input type="password" name="password" id="password" class="form-control" placeholder="Enter Password" aria-describedby="helpId">
</div>
<div class="form-group">
<label for="forConfirmPassword">Confirm Password</label>
<input type="password" name="cpassword" id="cpassword" class="form-control" placeholder="Enter Confirm Password" aria-describedby="helpId">
</div>
<input type="submit" class="btn btn-primary" value="SignUp" id="subbtn" onclick="myFunction()">
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
<?php echo json_encode($_POST);
// echo $_POST['email'];
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$username = $_POST['username'];
$email = $_POST['email'];
$password = $_POST['password'];
$cpassword = $_POST['cpassword'];
echo $username;
if ($password == $cpassword) {
//code
} else
echo '<div class="alert alert-danger" role="alert">
<strong>danger</strong>
</div>';
} ?>
</div>
script.js
function myFunction() {
console.log(window.location.href);
$('form').on('submit', function (event) {
$.ajax({
type: 'POST',
url: 'includes/signupmodal.php',
data: $('form#signupForm').serialize(),
success: function () {
data = $('form#signupForm').serialize();
console.log(data);
},
});
event.preventDefault();
});
}
我想在不加载页面的情况下使用 ajax 提交我的数据,所以请告诉我哪里错了
【问题讨论】:
-
您遇到了什么问题?
-
当我提交表单时,我的 php 不会获取我通过 ajax 发送的数据
-
是否由于默认表单提交而重新加载页面?如果不是 - 检查浏览器开发工具网络中的实际请求,以查看发送的内容是预期的内容、请求状态、响应等。您需要将其缩小到更具体的代码部分是否有效。您没有提供足够的调试细节
-
在开发工具中的状态是 200 ok 并且所有的输入也都在那里我猜我的 PHP 代码有一些问题但是当我使用正常的方法来放置数据时我的 PHP 代码运行完美
标签: javascript php html jquery bootstrap-4