繁体   English   中英

Ajax和PHP,导致div

[英]Ajax and php, result in a div

我想在“结果” div中显示服务器页面的结果。 这是我的计划:

home.php

 <script src="myscripts.js"></script>
 <div id="loginform">
   <div id="result"></div>
   <?php include_once 'core.login.php'; ?>
 </div>

myscripts.js

$(document).ready(function() {
$('#login').submit(function() { // catch the form's submit event
$.ajax({ // create an AJAX call...
    data: $(this).serialize(), // get the form data
    type: $(this).attr('method'), // GET or POST
    url: $(this).attr('action'), // the file to call
    success: function(response) { // on success..
        $('#result').html(response); // update the DIV
    }
});
return false; // cancel original event to prevent form submitting
    });
});

core.login.php

<?php

 // stuff about $db variable e other stupid things like $tool = new Tools etc...

 $email = $_POST["email"];
 $password = $_POST["password"];

// Initializing login process
if (isset($email) or isset($password)) {
$tool->decode($email, $password, $db);
}

?>

 <form action="core.login.php" method="POST" id="login">
        <fieldset>
            <legend>Data login:</legend>
            Email:<input id="email" type="email" name="email" placeholder="someone@example.com" required>
            <br>
            Password:<input id="password" type="password" name="password" required>
            <br>
            <input type="submit" value="Submit">
        </fieldset>
 </form>

class.php

public function decode($email, $password, $db) {
    if ($stmt = $db->prepare("SELECT password FROM users WHERE email = ?")) {
        $stmt->bind_param('s', $email);
        $stmt->execute();
        $stmt->store_result();
        $stmt->bind_result($password_db);
        $stmt->fetch();
        if ($stmt->num_rows == 1) {
            if ($password == $password_db) {
                echo "Success"; 
            } else {
                echo "Error";      
            }
        } else {
            echo "Email didn't found!";
        }
    }
}

没有AJAX,如果我正常启动代码就可以工作,它会给我正确的回声结果(成功或错误),但是当我使用AJAX时,什么也不会发生。

更新

好的,问题是操作URL,我的核心文件在文件夹core / core.login.php中,现在它在结果div中显示该页面,但是页面核心现在向我显示此信息:致命错误:呼叫成员第9行中的网站上非对象上的函数define()

也许ajax不会像对象那样传递变量?

尝试在您的js文件中:

 (Or Document)
$('#loginform').on('submit', '#login', function() {
  //AJAX etc..
})

代替 :

$('#login').submit(function() {
       //AJAX etc..
}

试试这个js

$(document).ready(function() {
$('#login').submit(function() { // catch the form's submit event
$.ajax({ // create an AJAX call...
data: $("#login").serialize(), // get the form data
type: $(#login).attr('method'), // GET or POST
url: $(#login).attr('action'), // the file to call
success: function(response) { // on success..
    $('#result').html(response); // update the DIV
}
});
 return false; // cancel original event to prevent form submitting
   });
 });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM