2

I have problems with AJAX results using jQuery.

I have defined these functions:

<script>
    function hello(callback, funct, val) {
        var ret = 0;
        console.log(val);
        $.ajax({
            type: 'GET',
            dataType: 'json',
            url: 'SGWEB/header.php',
            data: {
                'funct': funct,
                'val': val
            }
        }).done(function (data) {
            // you may safely use results here
            console.log(data);
            callback(data);
        });
    };

    function change() {
        hello(function (ret) {
                  console.log(ret);
                  $("#b1").text(ret);
              }, "hello", 1);
    }; 

    change();
</script>

SGWEB/header.php:

extract($_GET);
$validFunctions = array("readPin","hello");
if(in_array($funct, $validFunctions)) $funct();

// functions
// ....

function hello($val) {
    if ($val == 1) {
        echo "1";
    } else 
        echo "2";
}

The problem I have is that the AJAX passes only the first parameter in data {'funct': funct} and it's working, but val is completely ignored (it always echoes "2").

How can I solve this? Thanks

11
  • Does the console show anything? Commented Feb 22, 2016 at 14:17
  • You pass a function to hello() which is false when you compare it to 1.. that's why it always goes to the else branch. Commented Feb 22, 2016 at 14:20
  • 2
    @A1rPun javascript hello function (top of code) expects a callback function as it's first argument... the hello function at the bottom of code is a PHP function (I think) Commented Feb 22, 2016 at 14:30
  • 2
    I'd like to see more of SGWEB/header.php to see how it's handling the incoming query to end up calling the hello function ... surely the code marked PHP function isn't the whole content of that header.php Commented Feb 22, 2016 at 14:31
  • 1
    You need to add the proper PHP. I dont see the contents of SGWEB/header.php and I dont see how you are obtaining your GET data in PHP. Commented Feb 22, 2016 at 14:38

1 Answer 1

3

You are forgetting to pass the $val parameter to your function in PHP

Change this:

if (in_array($funct, $validFunctions)) $funct();

to this:

if (in_array($funct, $validFunctions)) $funct($val);

Another problem is that your AJAX is expecting JSON as you are defining it here dataType:'json' but you are not sending that. I would redo your ajax call like this so that you can see other errors as well:

$.ajax({
    type: 'GET',
    url: 'SGWEB/header.php',
    data: {
        'funct': funct,
        'val': val
    },
    success: function (result) {
        console.log(result);
        callback(result);
    },
    error: function (xhr, textStatus, error) {
        console.log(xhr);
        console.log(textStatus);
        console.log(error);
    }
});
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks a lot, my error was a little dumb. Tried the PHP side of the answer and is working, tomorrow I'll try the JS code!
awesome, glad i was able to help
Hi again... do you also know how can I use this type of ajax function in a for loop? can provide code if needed. Thanks
Yea, but if you can provide code, it would be best so that I can see what you mean.
I see, are you getting any errors on your console? Also, I would post a new question with this information. Would you be able to do that?
|

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.