0

I want to call a PHP function from JavaScript.

I am calling the PHP function by passing two arguments and I want that PHP function to simply echo those arguments. For that, I have written the following code but it is not working, meaning that the code prints nothing. Please help.

JavaScript

jQuery.ajax(
{
    type: "POST",
    url: 'save.php',
    dataType: 'json',
    data: {functionname:'saveUser', arguments:[username, password]},

    success: function (obj, textstatus) {
        if( !('error' in obj) ) {
            alert(obj.result);
        }
        else {
            console.log(obj.error);
        }
    }
});

PHP

<?php
    header('Content-Type: application/json');   
    $aResult="done";

    if( $_POST['functionname'] == 'saveUser' ) {

        echo $_POST['arguments'][0] +" "+ $_POST['arguments'][1];
        echo json_encode($aResult);
    }
?>
11
  • What is output of json_encode($aResult); ? PHP file is not sending valid json either.. Commented Dec 27, 2015 at 7:56
  • @T.J.Crowder, echo $_POST['arguments'][0] +" "+ $_POST['arguments'][1]; echo json_encode($aResult); will not return valid json I suppose. Only echo json_encode($aResult); would do.. Commented Dec 27, 2015 at 7:58
  • + is being used to concatenate.. Use . Commented Dec 27, 2015 at 8:01
  • 1
    @RayonDabre: I just saw your comment about .. If you'd like, I can remove my answer so you can post that as an answer. (I don't do PHP much, was off figuring out why it wasn't working.) Commented Dec 27, 2015 at 8:09
  • 1
    @RayonDabre: I'm just using a boring old PHP install. I get back the keys I used. But the documentation uses strings, so I'll add them back. Commented Dec 27, 2015 at 8:25

2 Answers 2

3

By using +, you're trying to convert the arguments to numbers and add them together. For string concatenation, you'd want . instead of +:

echo $_POST['arguments'][0] . " " . $_POST['arguments'][1];

But as Rayon points out in the comments, you'll get invalid JSON as a result.

The following accesses the arguments just fine, and returns JSON:

<?php

    header('Content-Type: application/json');   
    $aResult="done";


    if( $_POST['functionname'] == 'saveUser' ) {

        echo json_encode(Array(
            'arg0' => $_POST['arguments'][0],
            'arg1' => $_POST['arguments'][1],
            'message' => $aResult
        ));
    }
?>

Of course, your JavaScript isn't looking for those property names. This would alert the name using your current JavaScript:

<?php

    header('Content-Type: application/json');   

    if( $_POST['functionname'] == 'saveUser' ) {

        echo json_encode(Array(
            'result' => $_POST['arguments'][0] . ' ' . $_POST['arguments'][1]
        ));
    }
?>
Sign up to request clarification or add additional context in comments.

6 Comments

@AliSalman: It works at this end (I tried it before posting it, with your ajax code). Of course, the JavaScript alerts undefined because what I'm sending back isn't what it's looking for. But the keys are easily changed. I've added another block that sends back the arguments, concatenated, as result so it's alerted by your JavaScript code.
Yes it will work u just need to change your java script ajax success response if u r using firefox check console error in firebug
@devpro: The above works without any JavaScript changes.
Yes my brother its definitely work. I have also test by using thw same way +1
I have updated my question please see and test it now, it is not working for me
|
0

Try this in your php:

header('Content-Type: application/json'); 
$aResult="done";

 if( $_POST['functionname'] == 'saveUser' ) {
 echo $_POST['arguments'][0] +" "+ $_POST['arguments'][1];

 echo json_encode(array('result'=>$aResult)); 

}

You are using alert in ajax success by using obj.result. so I have used array in json_encode function with result index.

The value that you are echo in json_encode php this value you can get in ajax success.

Use console log like

console.log(obj);

You will get the response in obj.

1 Comment

@ali-salman: check console.log( obj); in ist line of ajax success

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.