Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to pass a javascript variable which I get when a button is clicked to php and then run a mysql query. My code:

function ajaxCall(nodeID) {
 $.ajax({
 type: "POST",
 url: "tree.php",
 data: {activeNodeID : nodeID}, 
 success: function(data) { 
 alert("Success!");
  }
 }
);
}

function onButtonClick(e, data) {
 switch (data.name) {
  case "add":
   break;
  case "edit":
   break;
  case "delete":
   nodeid = data.context.id;
   ajaxCall(nodeid);
   //query
   break;                                   
   }
}


<?php
      if (isset($_POST['activeNodeID'])) {
       $nodeid = $_POST['activeNodeID'];
      }
      else {
       $nodeid = 0;
      }
      ?>

I haven't done this before, so I am not sure why this doesn't work. Any suggestions? EDIT: Maybe I haven't explained myself well enough. I realise that php is server side and javascript is client side. I have an org chart that is displayed using javascript based on all the info is saved in my db. What I need to do is: when one of the javascript buttons is clicked (edit, delete, add) get the ID of the active node and use it as variable in php to run query. For example, delete the row in the db. What's the best way to do it?

share|improve this question
 
Is that your PHP or is it in the tree.php file? –  putvande Dec 1 '13 at 18:25
1  
The PHP code is executed when you load the page that contains the JavaScript code, not when Ajax call is made (if that was your intention). Have a look at the generated JS code. You will see that the content of the success callback is empty. Please explain what you are trying to achieve, so that we can help you better. –  Felix Kling Dec 1 '13 at 18:25
add comment

1 Answer

There's a bit of confusion here, the success value stores a callback that gets called when the Ajax call is successful. To make you a simply example, let's say you have an Ajax call like this:

function ajaxCall(nodeID) {
    $.ajax({
      type: "POST",
      url: "tree.php",
      data: {activeNodeID : nodeID}, 
      success: function(data) {
        console.log(data.my_message); 
      }
    });

This calls a PHP page called tree.php. In this page you do some computation and then return a value, in this case as JSON. So the page looks like this (note that I'm keeping it simple, you should validate input values, always):

$v = $_REQUEST['activeNodeID'];
$to_return = "the value sent is: " . $v;
return json_encode(array('my_message' => $to_return));

This simple PHP page returns that string as a JSON. In you javascript, in the specified callback (that is success), you get data as an object that contains the PHP's $to_return value. What you have written does not really makes sense since PHP is a server language and cannot be processed from the browser like JavaScript.

share|improve this answer
 
Good answer, but you really should use sentences properly, not separate everything with ,. It makes the text easier to read. I edited it. –  Felix Kling Dec 1 '13 at 18:39
 
You're right Felix, thanks for the corrections, I just switched to an American keyboard layout and I'm having a really hard time to get used to it, thanks again. –  Ende Neu Dec 1 '13 at 18:41
 
:D Keyboard layout changes are tough :) –  Felix Kling Dec 1 '13 at 18:45
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.