PHP / JS:无法得到

时间:2016-11-03 17:50:43

标签: javascript php jquery

我想做的就是打印'赢!'如果他们在数据库中登录他们的详细信息(正常工作),如果由于某种原因导致他们的信息在数据库中找不到,则会丢失'。

所以我的问题是由于某种原因,我的代码行'echo $ email;'不起作用。似乎设置为NULL。

目前它只打印'丢失',无论我输入什么,但是,如果我在数据库中添加一行有一个空白的电子邮件和密码(email =“”,密码=“”),那么PHP脚本返回'胜利!'。

PHP代码:

<?php
// echo "php test";
//server info
$servername = "localhost";
$username = "root";
$dbpassword = "root";
$dbname = "personal_data";


//Establish server connection
$conn = new mysqli($servername, $username, $dbpassword, $dbname);


//Check connection for failure 
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

//Read in email & password
echo "reading in email & password...";
$email = mysqli_real_escape_string($conn, $_POST['email1']);
$password = mysqli_real_escape_string($conn, $_POST['password1']); 
echo $email; //this prints blank
echo $password; //this also prints blank

$sql = "SELECT Name FROM personal_data WHERE Email='$email' AND Password='$password' LIMIT 1";
$result = mysqli_query($conn, $sql);


if(mysqli_num_rows($result) > 0){
        echo "win!!";
    } else {
        echo "loss";
    }

mysqli_close($conn);
?>

JS CODE:

$(document).ready(function(){
// alert("js working");

$('#login_button').click(function(){
    var email = $('#email').val(); //prints the correct value
    var password = $('#password').val(); //prints the correct value

var dataString = 'email1=' + email 
               + '&password1=' + password;

$.ajax({
                type: "POST",
                url: "http://localhost:8888/php/login.php",
                data: dataString,   //posts to PHP script
                success: success()
            });

});//eo login_button

function success(){
alert("success");
}

});//eof

1 个答案:

答案 0 :(得分:0)

除了完全,疯狂无用且没有任何安全性这一事实之外,您只需与$.ajax()交换$.post()并执行以下操作:

var loginEmail = $('#email').val();
var loginPassword = $('#password').val();

$.post('login.php',{email:loginEmail,password1:loginPassword},function(data) {
console.log(data);
})