admin管理员组文章数量:1300046
This JavaScript code uses POST method to send data from form to PHP where PHP checks in database if data is true. But I don't know how to send response from PHP back to JS that fetching was successful. Can someone explain?
JS:
$('#submit-btn').on('click', function() {
var dataString = 'username=' + document.getElementById('username').value + '&password=' + document.getElementById('password').value + '&rememberMe=' + document.getElementById('rememberMe').value;
$.ajax({
type: "POST",
url: "ajaxsubmit.php",
data: dataString,
cache: false,
success: function(){
//check if what response is
}
});
ajaxsubmit.php:
<?php
session_start();
//connect
$username =$_POST['username'];
$password =$_POST['password'];
$sql = "SELECT * FROM user WHERE email ='$username' OR username ='$username' AND password = '$password'";
$result = mysqli_query($conn, $sql);
if(!$row = mysqli_fetch_assoc($result)){
//response error
} else{
//response success
}
?>
This JavaScript code uses POST method to send data from form to PHP where PHP checks in database if data is true. But I don't know how to send response from PHP back to JS that fetching was successful. Can someone explain?
JS:
$('#submit-btn').on('click', function() {
var dataString = 'username=' + document.getElementById('username').value + '&password=' + document.getElementById('password').value + '&rememberMe=' + document.getElementById('rememberMe').value;
$.ajax({
type: "POST",
url: "ajaxsubmit.php",
data: dataString,
cache: false,
success: function(){
//check if what response is
}
});
ajaxsubmit.php:
<?php
session_start();
//connect
$username =$_POST['username'];
$password =$_POST['password'];
$sql = "SELECT * FROM user WHERE email ='$username' OR username ='$username' AND password = '$password'";
$result = mysqli_query($conn, $sql);
if(!$row = mysqli_fetch_assoc($result)){
//response error
} else{
//response success
}
?>
Share
Improve this question
edited Aug 20, 2017 at 13:08
Milan Chheda
8,2493 gold badges22 silver badges35 bronze badges
asked May 12, 2017 at 18:43
user7698730user7698730
3
- 3 Little Bobby says your script is at risk for SQL Injection Attacks. Learn about prepared statements for MySQLi. Even escaping the string is not safe! – Jay Blanchard Commented May 12, 2017 at 18:46
-
2
Never store plain text passwords! Please use PHP's built-in functions to handle password security. If you're using a PHP version less than 5.5 you can use the
password_hash()
patibility pack. It is not necessary to escape passwords or use any other cleansing mechanism on them before hashing. Doing so changes the password and causes unnecessary additional coding. – Jay Blanchard Commented May 12, 2017 at 18:47 -
You have to
echo
something in your PHP to get information returned to AJAX. – Jay Blanchard Commented May 12, 2017 at 18:47
3 Answers
Reset to default 4Whatever you echo out in the php will be sent back to the ajax.
if(!$row = mysqli_fetch_assoc($result)){
echo 0;
}
else{
echo 1;
}
success: function(response){
//check if what response is
console.log( response );
}
You have to echo something in your PHP to get something returned:
if(!$row = mysqli_fetch_assoc($result)){
//response error
echo 'there is a problem';
} else {
//response success
echo 'yippee!';
}
Then you can log the return as follows:
$('#submit-btn').on('click', function() {
var dataString = 'username=' + document.getElementById('username').value + '&password=' + document.getElementById('password').value + '&rememberMe=' + document.getElementById('rememberMe').value;
$.ajax({
type: "POST",
url: "ajaxsubmit.php",
data: dataString,
cache: false,
success: function(data){ // 'data' is the variable holding the return from PHP's echo
//check if what response is
console.log(data);
}
});
Make sure to watch the AJAX request / response in the browser's developer tools. Make sure you included the jQuery library in the project. The console will reveal errors. AJAX requires a web server.
WARNING Little Bobby says your script is at risk for SQL Injection Attacks. Learn about prepared statements for MySQLi. Even escaping the string is not safe!
DANGER Never store plain text passwords! Please use PHP's built-in functions to handle password security. If you're using a PHP version less than 5.5 you can use the password_hash()
patibility pack. It is not necessary to escape passwords or use any other cleansing mechanism on them before hashing. Doing so changes the password and causes unnecessary additional coding.
You can exit method with response parameters. like:
ajaxsubmit.php
if(!$row = mysqli_fetch_assoc($result)){
exit('error'); //exit with response 'error'
} else{
exit('success'); //exit with response 'success'
}
JS
$('#submit-btn').on('click', function() {
var dataString = 'username=' + document.getElementById('username').value + '&password=' + document.getElementById('password').value + '&rememberMe=' + document.getElementById('rememberMe').value;
$.ajax({
type: "POST",
url: "ajaxsubmit.php",
data: dataString,
cache: false,
success: function(response){
//check if what response is
console.log(response);
}
});
本文标签: javascriptHow to send response from PHP to AjaxStack Overflow
版权声明:本文标题:javascript - How to send response from PHP to Ajax? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741640062a2389863.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论