Javascript 如何使用ajax、php和解析json创建注册表单
首先,我是jQuery和PHP新手。我的目标是创建一个注册表单,在wamp上将数据传递给我的服务器。我的查询正在工作,在数据库中创建了一条新记录,但我不明白为什么它显示的是ajax调用的错误日志而不是成功日志 Ajax调用Javascript 如何使用ajax、php和解析json创建注册表单,javascript,php,json,ajax,jsonp,Javascript,Php,Json,Ajax,Jsonp,首先,我是jQuery和PHP新手。我的目标是创建一个注册表单,在wamp上将数据传递给我的服务器。我的查询正在工作,在数据库中创建了一条新记录,但我不明白为什么它显示的是ajax调用的错误日志而不是成功日志 Ajax调用 $('#reg-button').click(function(event){ $.ajax({ url: 'http://localhost:80/project/signup.php', type: 'POST',
$('#reg-button').click(function(event){
$.ajax({
url: 'http://localhost:80/project/signup.php',
type: 'POST',
dataType: 'json',
data: {
email:$("#email").val(),
username:$("#username").val(),
password:$("#password").val()
},
error: function() {
console.log("error");
},
success:function(data){
console.log("success");
var responseData = jQuery.parseJSON(data);
}
});
event.preventDefault();
});
PHP
$response = array();
if (isset($_POST['email']) && isset($_POST['username']) && isset($_POST['password'])){
$email = $_POST['email'];
$username = $_POST['username'];
$password = $_POST['password'];
$query = "INSERT INTO user (email, username, password) VALUES ('$email', '$username', '$password')";
$result = mysqli_query($conn, $query);
if($result){
$row_cnt = mysqli_num_rows($result);
printf("Result set has %d rows.\n", $row_cnt);
mysqli_free_result($result);
$response['success'] = 1;
$response['message'] = "User registered";
echo json_encode($response);
} else {
$response['success'] = 0;
$response['message'] = 'Ops, user not registered';
echo json_encode($response);
}
} else {
$response['success'] = 0;
$response['message'] = 'Required fields are missing';
echo json_encode($response);
}
你能帮我解决这些问题,给我一些提示或教程吗 请使用PHP来处理密码安全性。如果您使用的PHP版本低于5.5,则可以使用
密码\u hash()
。您不需要执行jQuery.parseJSON(数据)代码>如您指定的数据类型:'json',
是从这个printf接收输出的javascript(“结果集有%d行。\n”,$row\u cnt)代码>记住任何输出都将作为responce@JayBlanchard谢谢,我会考虑的。