2,580   0 ลองใช้ Ajax ส่งค่าแบบ json แล้วไม่ insert ลงฐานข้อมูล



Pennapa


พลังงานการช่วยเหลือ
( Level 1 )


คะแนนขอบคุณจากสมาชิก
( 0 )


ตอบกระทู้ ( 6 )
เขียนบทความ ( 0 )

Code (JS)


$(function(){
function ajaxindicatorstart(text)
{
if(jQuery('body').find('#resultLoading').attr('id') != 'resultLoading'){
jQuery('body').append('<div id="resultLoading" style="display:none"><div><img src="ajax-loader.gif"><div>'+text+'</div></div><div class="bg"></div></div>');
}

jQuery('#resultLoading').css({
'width':'100%',
'height':'100%',
'position':'fixed',
'z-index':'10000000',
'top':'0',
'left':'0',
'right':'0',
'bottom':'0',
'margin':'auto'
});

jQuery('#resultLoading .bg').css({
'background':'#000000',
'opacity':'0.7',
'width':'100%',
'height':'100%',
'position':'absolute',
'top':'0'
});

jQuery('#resultLoading>div:first').css({
'width': '250px',
'height':'75px',
'text-align': 'center',
'position': 'fixed',
'top':'0',
'left':'0',
'right':'0',
'bottom':'0',
'margin':'auto',
'font-size':'16px',
'z-index':'10',
'color':'#ffffff'

});

jQuery('#resultLoading .bg').height('100%');
jQuery('#resultLoading').fadeIn(300);
jQuery('body').css('cursor', 'wait');
}

function ajaxindicatorstop()
{
jQuery('#resultLoading .bg').height('100%');
jQuery('#resultLoading').fadeOut(300);
jQuery('body').css('cursor', 'default');
}



jQuery(document).ajaxStart(function () {
//show ajax indicator
ajaxindicatorstart('loading data.. please wait..');
}).ajaxStop(function () {
//hide ajax indicator
ajaxindicatorstop();
});


$('#frmRegister').validator().on('submit', function (e) {
if (e.isDefaultPrevented()) {
// handle the invalid form...
console.log('Invalide');
} else {
// everything looks good!
console.log('Valid');

var data= {
email:$('#email').val(),
inputPassword:$('#inputPassword').val()
}

//Show Loading

$.ajax({
type: "POST",
dataType: "json",
contentType: "application/json; charset=utf-8",
url: "view/login/insert_account.php", //Relative or absolute path to response.php file
data: data,
success: function(data) {
//Hide loading
console.log(data);
}
});

return false;

}//end else
});//end register
});






Code (PHP)
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Login to Everdwell</title>
<link rel="stylesheet" href="bootstrap-3.3.4/css/bootstrap.min.css">
<!-- Optional theme -->
<link rel="stylesheet"
href="bootstrap-3.3.4/css/bootstrap-theme.min.css">
<!-- Latest compiled and minified JavaScript -->



</head>
<body>
<link
href="//maxcdn.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css"
rel="stylesheet">
<div id="login-overlay" class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">
<span aria-hidden="true">×</span><span class="sr-only">Close</span>
</button>
<h4 class="modal-title" id="myModalLabel">Login to site.com</h4>
</div>
<div class="modal-body">
<div class="row">
<div class="col-xs-6">
<div class="well">
<form data-toggle="validator" role="form" action="" method="post">
<div class="form-group">
<label for="inputName" class="control-label">Email</label> <input
type="email" class="form-control" id="mail" name="mail"
placeholder="Enter Email" required>
</div>
<div class="form-group">
<label for="inputName" class="control-label">Password</label> <input
type="password" class="form-control" id="pass" name="pass"
placeholder="Enter Password" required>
</div>
<div id="loginErrorMsg" class="alert alert-error hide">Wrong
username og password</div>
<div class="checkbox">
<label> <input type="checkbox" name="remember" id="remember">
Remember login
</label>
<p class="help-block">(if this is a private computer)</p>
</div>
<button type="submit" class="btn btn-primary btn-block">Login</button>
<a href="/forgot/" class="btn btn-default btn-block">Forgot
Password</a>
</form>
</div>
</div>
<div class="col-xs-6">
<form data-toggle="validator" role="form" id="frmRegister">
<div class="form-group">
<label for="inputName" class="control-label">Email</label> <input
type="email" class="form-control" id="email" name="email"
placeholder="Enter Email" required>
</div>
<div class="form-group">
<label for="inputPassword" class="control-label">Password</label>
<input type="password" data-minlength="6" class="form-control"
id="inputPassword" name="inputPassword" placeholder="Password" required> <span
class="help-block">Minimum of 6 characters</span>
</div>
<div class="form-group ">
<label for="inputPassword" class="control-label">Confirm Password</label>
<input type="password" class="form-control"
id="inputPasswordConfirm" name="inputPasswordConfirm" data-match="#inputPassword"
data-match-error="Whoops, these don't match"
placeholder="Confirm" required>
<div class="help-block with-errors"></div>
</div>


<div id="loginErrorMsg" class="alert alert-error hide">Wrong
username og password</div>
<div class="checkbox">
<p>&nbsp;</p>
</div>
<button type="submit" class="btn btn-primary btn-block" id='btnNewAcount' >Create My Account</button>
</form>
</div>

</div>
</div>
</div>
</div>

<script src="js/northernSmile/register.js"></script>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
</body>
</html>






Code (PHP)

<?php


require 'connect_mysqli.php';


$email = isset($_POST['email']) ? trim($_POST['email']) : "";

$sqli = "select * from usertable where Login ='{$email}' ";

$result = $mysqli->query($sqli);
if(mysqli_num_rows($result)>0)
{


/* echo 'Please enter a new duplicate emails',"<br><br>"; */
echo "<script>alert('Please enter a new duplicate emails');window.location='index.php';</script>";
exit(0);
}




$sqli = " insert into usertable (Login, Password)
values
(
'{$mysqli->escape_string($_POST['email'])}'
, '{$mysqli->escape_string(md5($_POST['inputPassword']))}'

);
";

$result = $mysqli->query($sqli);



if($result){


echo json_encode(array('status' => 'true'));


}else{

echo json_encode(array('status' => 'false'));

exit(0);

}


?>

แสดงความเห็น

อัพโหลดรูปภาพ..คลิก *** เพื่อความปลอดภัยในด้านสิทธิ ไม่อนุญาติให้นำลิ้งรูปภาพจากที่อื่นมาแปะ ถ้าพบเจอ ลบกระทู้ ทันที ***

Back to Top