rajkumar
rajkumar

Reputation: 365

How to write php code inside jquery to update database table

I am working in Banking project .I want to write php code to update table upon successful Transaction using ajax . suppose i am sending request from fundtransfer.php to external API and the External API is also responding correctly .Now upon successful API respond i want to update my database table field name status from pending to completed .

    <script>
        $(document).ready(function()
        {
            $.ajax(
            {
            url:"http://someexternalwebsite.com/API",
            type:"post",
            data:"variable="+value,
            success:function(result)
            {
                if(result==100)
                {
                    $("#message").html(successful transaction);
                    //Now i want to update my database tabale status saying Successful Transation 
                    //where to write these all php mysql code to update my database table
                   // without loading and redirecting page
                }   

                else
                {
                    $("#message").html(some thing gone wrong);
                }

            }
            });
        });
    </script>

Upvotes: 1

Views: 180

Answers (2)

David
David

Reputation: 219077

without loading and redirecting page

The same thing you're doing now... Making an AJAX request. (Assuming your initial AJAX request isn't rejected by the Same Origin Policy...) Upon successfully returning from the first AJAX request, you'd make a second one to your code. Something like this:

$.ajax({
    url: 'someAPI',
    success: function (response) {
        $.ajax({
            url: 'someDBPage',
            success : function (dbResponse) {
                // notify the user of success
            }
        });
    }
});

The PHP code at someDBPage would interact with your database just like any other PHP code. And you'd send it data just like any other AJAX POST request, similar to how you're sending data to the API URL now.

Upvotes: 1

Jordi vd M
Jordi vd M

Reputation: 88

You could try:
$.get("http://someexternalwebsite.com/API/?variable="+value) for the http GET method.
or:
Use JavaScript:

         function post(data, url){
            $.post(url , data
            , function (response) {
                func(response);
            }); 
}

Then use post(....) inside jquery
I hope it helps you, Good luck :)

Upvotes: 0

Related Questions