简体   繁体   中英

Ajax and php, result in a 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?

Try with that in your js file:

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

instead of :

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

Try this 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
   });
 });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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