我想使用ajax获取表单数据,然后将其提交到我的php页面,然后将其插入我的数据库中。成功插入后,我想显示"您已成功注册"并且失败"无法注册"如果您已经是会员,我会显示"已经是会员,只需登录"。
出于某种原因,我的ajax代码无法正常工作。它跳到最后一部分(否则声明甚至没有关心if语句)。我的PHP代码是正确的。因为我已经单独测试过它并且工作正常。
有人可以向我建议解决方案或其他方法来实现此目的请记住,我不想转到另一个页面。
这是我的JavaScript / jQuery代码:
$(document).ready(function){
//Signup Js
$("#Register").click(function(){ //#Register is my submit button id
var action = $("#sp-form").attr('action'); //#sp-form is the register form id.
var spform_data = {
myusername: $("#myusername").val(),
myemail: $("#myemail").val(),
mypassword: $("#mypassword").val() //Getting data from the input forms using their respective id's
}
$.ajax({
type: "POST",
url: action,
data: spform_data,
success: function(outcome)
{
if(outcome == "success"){
$("#sp-form").slideUp('slow', function(){
$("#message_sp").html('<p class="success">You have been registered successfully!</p><p style="color:#1c1c1c;">Redirecting....</p>');
});
}
else if(outcome == "alreadymember") {
$("#modal_signup").css("height","380");
$("#message_sp").html('<p class="error">ERROR: Email match found,Just Sign in.</p>');
}
else if(outcome == "failed"){
$("#modal_signup").css("height","380");
$("#message_sp").html('<p class="error">ERROR: Failed to Register</p>');
}
else{
$("#modal_signup").css("height","380");
$("#message_sp").html('<p class="error">ERROR: Unknown ERROR</p>');
}
}
});
return false;
});
});
这是我的PHP / MySQLi代码
<?php
if ($_POST)
{
$host = "localhost";
$user= "root";
$password = " ";
try {
$conn = mysqli_connect($host , $user , $password) or die("unable to connect to Local Host"); //Establishing connection with the server
$sql = mysqli_select_db($conn,'audacity') or die("unable to connect to database"); //selecting a database from the server #after connecting
} catch (Exception $e) {
echo $e->getMessage();
}
$name = mysqli_real_escape_string($conn,$_POST['myusername']);
$mail = mysqli_real_escape_string($conn,$_POST['myemail']);
$pass = mysqli_real_escape_string($conn,$_POST['mypassword']);
// Query of SQL
$Query =mysqli_query($conn,"INSERT INTO registration (Username,Email,Password) VALUES ('$name', '$mail' , '$pass')")) //if successful outcome is true
if($Query ==true){
echo "success";
}
elseif ($Query==false) { #if failed
$sqlbd = "SELECT Email FROM registration WHERE Email='$mail' ";
$result = mysqli_query($conn,$sqlbd);
if(mysqli_num_rows($result)==1){ //possible rsn for failure
echo "alreadymember";
}
else{
echo "failed"; //unknown reason.
}
}
mysqli_close($conn);
?>
问题在哪里
答案 0 :(得分:0)
我认为你必须在你的ifs语句的开头检查num_rows是否= = 1 ...你的php只需输入email然后将num行检查为else语句...你首先检查num行然后设置if语句或者您已为电子邮件列设置了唯一的内容?
答案 1 :(得分:0)
我可以确认您的代码有很多错误。 我附上了修正版的代码;但是,我将假设(来自php代码)目标是防止多次注册;如果是这种情况,则应检查您的逻辑。您在检查之前插入,因此永远不会运行检查代码。所以在尝试插入之前进行检查。整体代码看起来不太好。 我只修复了错误,你应该研究逻辑。
JQuery的 $(document).ready(function(){
$("#sp-form").submit(function (e) {
return false;
});
$("#Register").click(function () {
var action = $("#sp-form").attr('action'); //#sp-form
is the register form id.
var spform_data = {
myusername: $("#myusername").val(),
myemail: $("#myemail").val(),
mypassword: $("#mypassword").val()
//Getting data from the input forms using their respective id's
}
$.ajax({
type: "POST",
url: action,
data: spform_data,
success: function (outcome)
{
if (outcome.indexOf("success")>=0) {
$("#sp-form").slideUp('slow', function () {
$("#message_sp").html('<p
class="success">You have been registered successfully!</p><p
style="color:#1c1c1c;">Redirecting....</p>');
});
}
else if (outcome.indexOf("alreadymember")>=0) {
$("#modal_signup").css("height", "380");
$("#message_sp").html('<p
class="error">ERROR: Email match found,Just Sign in.</p>');
}
else if (outcome.indexOf("failed")>=0) {
$("#modal_signup").css("height", "380");
$("#message_sp").html('<p
class="error">ERROR: Failed to Register</p>');
}
else {
$("#modal_signup").css("height", "380");
$("#message_sp").html('<p
class="error">Unknown Error</p>');
}
}
});
return false;
});
});
// Php
$host = "localhost";
$user = "root";
$password = "";
$conn = "";
try {
$conn = mysqli_connect($host, $user, $password) or
die("unable to connect to Local Host"); //Establishing connection with
the server
$sql = mysqli_select_db($conn, 'audacity') or die("unable
to connect to database"); //selecting a database from the server #after
connecting
} catch (Exception $e) {
echo $e->getMessage();
}
$name = mysqli_real_escape_string($conn, $_POST['myusername']);
$mail = mysqli_real_escape_string($conn, $_POST['myemail']);
$pass = mysqli_real_escape_string($conn, $_POST['mypassword']);
// Query of SQL
$Query = mysqli_query($conn, "INSERT INTO registration
(Username,Email,Password) VALUES ('$name', '$mail' , '$pass')"); //if
successful outcome is true
if ($Query == true) {
echo "success";
} elseif ($Query == false) { #if failed
$sqlbd = "SELECT Email FROM registration WHERE
Email='$mail' ";
$result = mysqli_query($conn, $sqlbd);
if (mysqli_num_rows($result) == 1) { //possible rsn for
failure
echo "alreadymember";
} else {
echo "failed"; //unknown reason.
}
}
mysqli_close($conn);
}