weixin_33674437 2015-03-26 14:08 采纳率: 0%
浏览 19

Ajax和PHP,导致div

I want show the result of my server page in the "result" div. This is my scheme:

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!";
        }
    }
}

Without AJAX if i start the code normally it work, it give me the correct echo result ( success or error ) but when i use AJAX, nothing happen.

UPDATE

Ok guys the problem was the action url, my core file is in the folder core/core.login.php, now it display the page in the result div, but the page core show me this now: Fatal error: Call to a member function decode() on a non-object in website on line 9

Maybe ajax don't pass the variables like object?

  • 写回答

2条回答 默认 最新

  • weixin_33716941 2015-03-26 14:32
    关注

    Try with that in your js file:

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

    instead of :

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

报告相同问题?

悬赏问题

  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来