通过post.ajax()发送json数据,同时接收json响应



请帮助我目前正在编码的注册页面。我已经在这条死胡同里呆了两天了。如果有人能帮助调试我的代码,我将不胜感激。

问题

我正在使用javascript来验证表单是否存在错误。如果没有错误,脚本应该使用.ajax((通过post方法提交表单数据,并返回json响应。

如果用户名或电子邮件已经存在,则响应将是错误的。脚本处理表单验证,但不提交表单或获得任何响应。

以下是我的代码:

Javascript

var errorcheck;
var errorcheck1;
function validateForm() {

/*
error handling codes
*/
errorcheck1 = errorcheck;
errorcheck = 0;
};
$('form').on('submit', function(e) {
e.preventDefault(); 
validateForm();
if(errorcheck1 < 9){
//prevent the form form submitting
e.preventDefault();  
} else {
document.getElementById("sendbtn").innerHTML = "Registering...";
var sendInfo = { firstname: firstnameValue, lastname: lastnameValue, email: emailValue, username: usernameValue, userpassword: passwordValue, country: countryValue, state: stateValue, zip: zipcodeValue };
$.ajax({
url:"signup-exec.php",
method:"POST",
data: sendInfo,
dataType:"JSON",
success: function(result){  
//if the result is 10  
if(result.status == 10){
window.location.href = '../welcome';
}else if(result.status == '11'){
document.getElementById('formerrorbox').style.display='block';
document.getElementById('formerrorbox').innerHTML='Email is already registered';
document.getElementById("sendbtn").innerHTML = "Get Started";
}else if(result.status == 12){
document.getElementById('passwordbox').style.display='block';
document.getElementById("sendbtn").innerHTML = "Get Started";
}else{    

document.getElementById('formerrorbox').style.display='block';
document.getElementById('formerrorbox').innerHTML='Trouble connecting to server.';
document.getElementById("sendbtn").innerHTML = "Get Started";
}
}               
});

PHP

<?php
session_start();
include ('../config.php');

$firstname= strtolower($_POST['firstnameValue']);
$lastname= strtolower($_POST['lastnameValue']);
$phone= strtolower($_POST['phoneValue']);
$email= strtolower($_POST['emailValue']);
$username= strtolower($_POST['usernameValue']);
$userPassword= $_POST['passwordValue'];
$country= $_POST['countryValue'];
$state= $_POST['stateValue'];
$zipcode= $_POST['zipcodeValue'];
$check_email=mysqli_query($conn, "SELECT * FROM users WHERE email='$email'");
$count_email=mysqli_num_rows($check_email);
$check_username=mysqli_query($conn, "SELECT * FROM users WHERE username='$username'");
$count_username=mysqli_num_rows($check_username);

if($count_email !=0 || $count_username != 0){
$data["status"] = '11';
echo json_encode($data);
exit();
} 

提前感谢!

要将数据作为JSON发送,需要编辑JS代码NOTE:在这种情况下不能使用$_POST

url: "signup-exec.php",
method: "POST",
data: JSON.stringify(sendInfo),
contentType: "application/json",

并像一样在php中解析

// Takes raw data from the request
$json = file_get_contents('php://input');
// Converts it into a PHP object
$data = json_decode($json);

或者,您可以将数据发送为multipart/form-data,在这种情况下,您的JS代码将是

// Get form inputs
let formData = new FormData($('form')[0]);
$.ajax({
url: "signup-exec.php",
method: "POST",
data: formData,
enctype: 'multipart/form-data',
processData: false,
contentType: false,
....

然后您可以使用$_POST处理php中的数据

相关内容

  • 没有找到相关文章

最新更新