0

I'm trying to use Jquery to call an ajax function, which updates an MySQL database. I have several other Ajax requests in the same file and they work fine. For some reason the getinvoices value is not being passed to the PHP file. I'm calling the function on click of a button, below is the code I'm using.

Javascript

$( "#updatexero" ).button().on( "click", function(event) {
    $.ajax({
        type:'post',
        url: 'invoices.php',
        data: { getinvoices: 1 },
        success: function(){
            $( "#sql-confirm" ).dialog({
                modal: true,
                buttons: {
                    OK: function() {
                        $( this ).dialog( "close" );
                    }
                }
            });
        }
    });
});

PHP - invoices.php

if ( isset($_REQUEST['getinvoices'])) {
    //Code to do stuff
}

If I do echo $_POST['getinvoices'];, it says undefined index, as no value has been passed. I can't see why this shouldn't work, what am I doing wrong?

Edit: I have solved the issue now, the problem was another if statement in invoices.php that wasn't getting called, so nothing to do with the Ajax query. The firebug extension proved handy for debugging though.

3
  • check request in network from developer Tools, this codes must work! Commented Oct 1, 2014 at 11:32
  • It is calling the php file but not sending the value of getinvoices, i have almost the exact same code elsewhere in the same file and it works Commented Oct 1, 2014 at 11:41
  • This code should work. I've tried it and it correctly posts the getinvoices=1. Have you tried a get instead of a post? It's a little easier to debug a get request... Commented Oct 1, 2014 at 12:18

3 Answers 3

1

Your code is correctly written. I adopted your code "as is" and put it on my own server.

In invoices.php I simply put:

<?php 
    print_r( $_POST );
?>

This is the result I got when checking in the debug console:

enter image description here

I would advise you to use this debug console to check your AJAX request. It's included in both Chrome and FireFox. In chrome you need to install the extension Firebug Lite however. You'll see in my picture that getinvoices is both included in the AJAX post and received by the invoices.php script.

The debug console will also alert you if there is a syntax error in your JavaScript code.

However.. you can change the following code:

$( "#updatexero" ).button().on( "click", function(event) {

to

$("#updatexero").click(function(event){
Sign up to request clarification or add additional context in comments.

1 Comment

You're right, I tried invoices.php with just a simple sql query and it worked, it must be something else in invoices.php that is causing the problem
0
Try this:

 $( "#updatexero" ).on( "click", function(event) {
                $.ajax({
                type:'post',
                url: 'invoices.php',
                data: { getinvoices: 1 },
                success: function(){
                      $( "#sql-confirm" ).dialog({
                      modal: true,
                      buttons: {
                        OK: function() {
                          $( this ).dialog( "close" );
                        }
                      }
                    });

                }
            });
        });

Comments

0

You have syntax error in your code .

Remove .button() as it does not match the syntax here.

Try below code :-

$("#updatexero").on("click", function(event) {
         //// Your code.
    });

For more information check out below link :-

http://api.jquery.com/on/

4 Comments

If the button() returns the jQuery object - what it normally does - this is valid method chaining.
can you link to some documentation for this @DerVO?
I have removed .button() but it still doesn't work, I don't think its the problem as I have used it elsewhere in my code without any problems
@Neel I assume this is the button widget from jQuery UI - this returns the jQuery object containing the button element (api.jqueryui.com/button/#method-widget). Here is a demo for this syntax : jsfiddle.net/tolund/S8D6n

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.