Using jQuery .ajax () method to submit a form in PHP

I am currently working on a form that allows the user to change their password. And I am not very good at jQuery ajax. It seems like ajax is not sending data to changepassword.php. PHP file works great when I am not using ajax. Can you guys help me here a little?

I have updated my files as per some of the comments. When I try to submit the form, it returns Doesn't Work

. So I think php is responding, but the value didn't make it to the database.

Thanks a lot for helping the guys.

Below 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 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 a method change_password()

from connect.php

a 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;
        }
    }
}

      

+3


source to share


1 answer


Is your request to the server successful? As I'm not sure, but if the whole request fails then you have no success and jquery ajax will (probably) look for an error function, but you didn't provide it - so nothing happens. "

About setting your ajax data property, maybe you can do data: $('#form_id').serialize()

, and thid = s will create the correct string for the request - var1 = value1 & var2 = value2, but with less code.

If there is a problem in your php file after session_start();

, pasting the following code will give you a hint what is going wrong:



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

      

Good luck. Yveline

+1


source







All Articles