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);
});
chris85
  • 23,846
  • 7
  • 34
  • 51
William
  • 1,009
  • 15
  • 41

5 Answers5

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) );
Poiz
  • 7,611
  • 2
  • 15
  • 17
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.

Michael Yousrie
  • 1,132
  • 2
  • 10
  • 20
  • 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! – William Dec 05 '16 at 16:55
  • You can do whatever you want in the php. I will edit my answer to add a real life situation. – Michael Yousrie Dec 06 '16 at 00:17
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.

Nytrix
  • 1,139
  • 11
  • 23
  • How do i pass multiple commands in? So: `$_GET['command']=='apples'` and `$_GET['login']=='success'`? – William Dec 07 '16 at 17:47
  • 1
    Like any `array`, just a `,` and then your next value. I've updated the answer. – Nytrix Dec 07 '16 at 17:51
  • 1
    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!! – William Dec 08 '16 at 10:46
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>
Nytrix
  • 1,139
  • 11
  • 23
Muya
  • 142
  • 1
  • 15
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"
    }
})
Jeff
  • 607
  • 1
  • 7
  • 17