0

I am not sure how I should use the data passed along with an ajax call in the PHP script. Specifically, here is the code for the ajax call:

if (node_selected!=0 & node_selected!=null){
                $.ajax({
                    type: "POST",
                    url: "php/fetch_sensors.php",
                    data: node_selected,
                    dataType:'json',
                    success: function( options ){
                        ...
                    }
                });
            }

'node_selected' is an integer variable.

And here is the PHP script (the fetch_sensors.php):

$query = "SELECT SensorID,Variable FROM sensors WHERE SensorID IN (SELECT SensorID FROM nodesensors WHERE NodeID=node_selected)";
$result = mysql_query($query, $con) or die('query not made');

while ($row = mysql_fetch_assoc($result)) {
    $sensors[] = $row;
}

echo json_encode($sensors);

If I replace 'NodeID=node_selected' with 'NodeID=2' (2 is just an example) everything works fine. So, I figure that I don't use correctly the 'node_selected'. Any ideas?

Thanks a lot!

2
  • add in top of your php an echo '<pre>';print_r($_POST);echo '</pre>'; to see what we have . Commented Oct 22, 2014 at 14:17
  • @Danijel 'node_selected' is an integer variable Commented Oct 22, 2014 at 14:22

3 Answers 3

3

Jquery:

 if (node_selected!=0 && node_selected!=null){
            $.ajax({
                type: "POST",
                url: "php/fetch_sensors.php",
                data: {myNode: node_selected}, //like this
                dataType:'json',
                success: function( options ){
                    ...
                }
            });
  }

PHP:

$myNode = mysql_escape_string($_POST["myNode"]);
$query = "SELECT SensorID,Variable FROM sensors WHERE SensorID IN ";
$query.= "(SELECT SensorID FROM nodesensors WHERE NodeID= '".$myNode."')";
$result = mysql_query($query, $con) or die('query not made');

while ($row = mysql_fetch_assoc($result)) {
  $sensors[] = $row;
}

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

4 Comments

+1 Although you should either cast to int if you don't quote the value in the query or use a string escape function and quote it. Now your query can break.
You are right @jeroen, i post my answer under the assumtion that SensorID is an int field, but it could be alphanumeric...editing..
Yep, it works, thanks a lot. But why do I have to pass the data like that?? Why "data: node_selected" isn't enough??
According to jquery documentation: data Type: PlainObject or String or Array Data to be sent to the server. It is converted to a query string, if not already a string. It's appended to the url for GET-requests. See processData option to prevent this automatic processing. Object must be Key/Value pairs. If value is an Array, jQuery serializes multiple values with same key based on the value of the traditional setting (described below). ...source: api.jquery.com/jQuery.ajax
3

You have to send a key/value pair. You are only sending a value.

If you want to receive $_POST['NodeID'] in php then the easiest way in jQuery AJAX is to create an object using the same key

$.ajax({
    type: "POST",
    url: "php/fetch_sensors.php",
    data: {
        NodeID: node_selected
    },
    dataType: 'json',
    success: function (options) {...
    }
});

The easiest way to think of it is the object keys are the same as using name in a form control. The processing of most ajax at server is identical to processing of forms

Comments

1

Give your data a key, so you can fetch is in php via $_POST global:

//js
data: {node_selected: node_selected},

//php
$query = "SELECT SensorID,Variable FROM sensors WHERE SensorID IN (SELECT SensorID FROM nodesensors WHERE NodeID=" . $_POST['node_selected'] . ")";

Comments

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.