简体   繁体   中英

How to run PHP script in the back by a jquery button

I need to run some PHP code from a file when I click a button from jQuery. This is the code I have:

$(document).ready(function(){

      $("#btnSubmit").button().click(function(){
        alert("button 1");

        $.ajax({
        url: 'releaseBackEnd.php',
        type: 'POST',
        async: false,
        data: {},
        dataType: 'xml',
        error: function(){
            alert('Error');
        },
        success: function(data){        
            //check error
            alert("success!");

        }
    });

    });
 });

It shows the alert of "error". I have the releaseBackEnd.php file in the same directory of the javascript file.

check the response in firebug console .. if u don't have firebug add it to your firefox using this link

https://addons.mozilla.org/en-US/firefox/addon/1843/

Change

$("#btnSubmit").button().click(

to

$("#btnSubmit").click(

and check whether the php page is sending the response or not.

Try getting the error in the error callback function using the XMLHttpRequest.

error(XMLHttpRequest, textStatus) {

}

Try this code...

$(document).ready(function(){

      $("#btnSubmit").bind('click',function() {
        alert("button 1");

        $.ajax({
        url: 'releaseBackEnd.php',
        type: 'POST',
        async: false,
        data: {},
        dataType: 'xml',
        error: function(){
            alert('Error');
        },
        success: function(data){        
            //check error
            alert("success!");

        }
    });

    });
 });

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