3

I'm currently working on a form that allows the user to change their password. And I'm not very good with jQuery ajax. The ajax doesn't seem to send the data to changepassword.php. The PHP file works fine when I don't use ajax. Can you guys help me out here a bit?

I updated my files according to some of the comments. When I try to submit the form, it returns Doesn't Work. So I guess the php is responding, but the value didn't get to the database.

Thanks a lot for the help guys.

Following is the form and ajax (Updated)

        <form id="form_id" method="POST">
            <label for="username">Username</label>
            <input type="text" name="username" id="username" required />
            <label for="old_password">Old Password</label>
            <input type="password" name="old_password" id="old_password" required />
            <label for="new_password">New Password</label>
            <input type="password" name="new_password" id="new_password" required />
            <button class="submit">
                Submit
            </button>
            <p class="result"></p>
        </form>
        <script>
            $.ajaxSetup({
                cache : false
            });
            $(".submit").click(function(e) {
                $.ajax({
                    url : "changepassword.php",
                    type : "POST",
                    data : $("#form_id").serialize(),
                    success : function(data) {
                        if(data == 1) {
                            $(".result").html("Works");
                        } else {
                            $(".result").html("Doesn't Work");
                        }
                    }
                });
                return false;
            });

        </script>

And this is changepassword.php (Updated)

    <?php
session_start();
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(-1);
include_once "../classes/Connect.php";
$connection = new Connect();
$username = $_POST['username'];
$old_password = $_POST['old_password'];
$new_password = $_POST['new_password'];
$result = $connection -> change_password($username, $old_password, $new_password);
if ($result) {
    echo 1;
}
    ?>

This is the method change_password() from connect.php class file

    function change_password($username, $old_password, $new_password){
    $query = "UPDATE Account SET password = ? WHERE username = ? AND password = ?";
    if ($stmt = $this -> conn -> prepare($query)){
        $stmt -> bind_param('sss', md5($new_password), $username, md5($old_password));
        if ($stmt -> execute()){
            return true;
        }
    }
}
11
  • Are you getting any error on firebug? Do you get alert as "Works"? Commented Mar 29, 2012 at 21:50
  • echo $username or whatever in your php file then alert(data); instead to see if its passing over to the php correctly. Commented Mar 29, 2012 at 21:54
  • I the alert "Works" doesn't show up. I tried to echo $username in php file, but it doesn't pass back to ajax. And I don't quite know how to use firebug Commented Mar 29, 2012 at 22:06
  • Just for kicks, also add $.ajaxSetup ({cache:false}) at top of your script. Commented Mar 29, 2012 at 22:09
  • 2
    In Firefox: Open Tools->Web Developer ->Firebug->XHR Tab. Then click your button and examine/click the console contents. Commented Mar 29, 2012 at 22:12

1 Answer 1

1

Is your request to the server successful? Because I'am not sure but if the entire request fails then u don't have success and jquery ajax (probably) will look for error function but you didn't provide it - so nothing happen'.

About setting your ajax data property probably u can do data: $('#form_id').serialize() and thid=s will create proper string for request - var1=value1&var2=value2 but with less code.

If there is problem in your php file after session_start(); inserting the following code will give you a hint what's going wrong:

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(-1);

Wish you luck. Ivelin

Sign up to request clarification or add additional context in comments.

2 Comments

I did that. And I also gave an id to the form. But it's still not working. Thanks anyways.
In your case 'Doesn't Work' mean that request is successfully sended to the script, but something in mysql request or logic goes wrong. If you can check the result of query with php.net/manual/en/function.mysql-affected-rows.php

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.