0

I have a PHP script which i want to pass a value in. So example.com/test.php?command=apple. If that's entered then do its code. That all works grand in the server side but how do i accomplish this using an ajax call?

My code:

PHP:

<?php
if ($_GET['command']=='apples')
{
    //do code
    echo "hello";
}       
?>

Jquery:

$.ajax({
    url : '../command/test.php?command=apples'
}).done(function(data) {
    console.log("result: " + data);
});
1
  • 1
    Do you get any error? Commented Dec 5, 2016 at 16:43

5 Answers 5

3

The Snippet below is Pretty straight-forward and self-explanatory... however should you have any questions, don't hesitate to use the comments functionality below this Post.... ;-)

JQUERY: AJAX....

        var request = $.ajax({
            url         : "../command/test.php",
            data        : {"command": "apples"},
            dataType    : "json",
            type        : "POST"
        });

        request.done(function(data, textStatus, jqXHR){
            if(data){
                console.log(data);
            }
        });

        request.fail(function(jqXHR, textStatus) {
            alert( "Request failed: " + textStatus );
        });

PHP....

<?php
    $response   = array();
    $command    = isset($_POST['command']) ? 
                  htmlspecialchars(trim($_POST['command'])) :
                  null;
    if($command){
        // BUILD UP YOUR RESPONSE HERE
        $response['command']  = $command;
        // ADD SOME ARBITRARY DATA - JUST FOR FUN
        $response['name']     = "Albert";
        $response['lastName'] = "Einstein";
     }

     die( json_encode($response) );
Sign up to request clarification or add additional context in comments.

Comments

2

You can put the data into a data section, I think that will solve your problem.

$.ajax({
    url : '../command/test.php',
    data: {command: "apples",login: "succes"}
}).done(function(data) {
    console.log("result: " + data);
});

Also, you can see what you get as a response, open developer tools and you can see under network your request appearing when made, click on that and you can see the response.

3 Comments

How do i pass multiple commands in? So: $_GET['command']=='apples' and $_GET['login']=='success'?
Like any array, just a , and then your next value. I've updated the answer.
I was doing exactly what you did but no luck. Copied yours and worked ._. Must of been asleep last night trying haha. Thanks a mill!!
2

You can send the value in a POST request to the php page and get the returned value from there all using $.ajax

for example we will calculate 2 numbers and return the sum via ajax

Example Code

$.ajax({
   url: "/path/to/page.php",
   type: "POST",
   data: {number_1: 10, number_2: 5},
   success: function(response){
      // alert $result from the php page which is called response here.
      alert(response);
   }
});

Php page

// checking if data exists
if ($_POST['variable_1'] != null && $_POST['variable_2'] != null ){
    $variable_1 = $_POST['variable_1'];
    $variable_2 = $_POST['variable_2'];
    $result = $variable_1 + $variable_2;
}else{
    // either of the values doesn't exist
    $result = "No Data Was Sent !";
}
// returning $result which is either 15 or " no data was sent "
echo $result;

This will return 15. Hope this makes sense now.

2 Comments

This makes some sense to me but cant seem to get it going. Could you put an if statement into the php page in your answer? Or explain more? Thanks!
You can do whatever you want in the php. I will edit my answer to add a real life situation.
1

You need to post. You can use the url args or pass a data object.

$.ajax({
    method: "POST",
    url: "../command/test.php",
    data: {
        command: "apples"
    }
})

Comments

1

Can you try this

<script type="text/javascript">

        $.ajax({
            type: 'get',
            url: "../command/test.php",
            dataType: 'json',
            data: ({command: 'apples'}),
            success: function(data)
            {
                console.log("result: " + data);                           
            }
        });

</script>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.