1

im using ajax and php on my android app to query my database.

i am able to retrive all the results but dont know how to send a variable to my php so that i can use it as a custom query and retrive the results... something like this :

$id = $_POST['id'];

$sql = "SELECT * FROM mobile_app WHERE nome LIKE '{%id%}'"; 

but cant make my ajax post the variable and retrive the result...

this is my code :

my mobile app:

    $.ajax({
        url: "http://www.example.com/mobile_read.php",    // path to remote script
        dataType: "JSON",                                // data set to retrieve JSON
        success: function (data) {                       // on success, do something...
            // grabbing my JSON data and saving it
            // to localStorage for future use.
            localStorage.setItem('myData', JSON.stringify(data));
        }
    });

my php:

$sql = "SELECT * FROM mobile_app";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        $output[] = array (
        "nome" => $row['nome'],
        "status" => $row['status'],
         "hentrada" => $row['hentrada'],
        "evento" => $row['evento']
    ); 
    }



} else {
    echo "0 results";
}


$conn->close();
echo json_encode($output);
Mehul Kuriya
  • 608
  • 5
  • 18
Bruno Alex
  • 63
  • 3
  • 13
  • You need to add the data attribute to you ajax call. My assumption is you need to POST the ID. – Neo Nov 18 '16 at 12:19

2 Answers2

5

ajax() have a parameter

data:

by using this you can send as many param you want lik:

data:{
    param1 : value1,
    param2 : value2,
    // and so on
}

In your case it is like:

data:{
    id : value
}

and you can get this param in your php code like:

$id = $_POST['id'];
Mayank Pandeyz
  • 25,704
  • 4
  • 40
  • 59
1

You need to add the following additional options to your $.ajax object:

type: 'post'

and

data: {
    id: whateverVariableHasID
} 
Manno
  • 399
  • 1
  • 3
  • 12