title: Send data from Javascript to PHP and use PHP's response as variable in JS
body: <p>I have checked around, but can't seem to figure out how this is done.
  I would like to send form data to PHP to have it processed and inserted into a database (this is working).</p>
  
  <p>Then I would like to send a variable ($selected_moid) back from PHP to a JavaScript function (the same one if possible) so that it can be used again.</p>
  
  <pre><code>function submit_data() {
      "use strict";
      $.post('insert.php', $('#formName').formSerialize());
      $.get('add_host.cgi?moid='.$selected_moid.');
  }
  </code></pre>
  
  <p>Here is my latest attempt, but still getting errors:</p>
  
  <p><strong>PHP:</strong></p>
  
  <pre><code>$get_moid = "
      SELECT ID FROM nagios.view_all_monitored_objects
      WHERE CoID='$company'
      AND MoTypeID='$type'
      AND MoName='$name'
      AND DNS='$name.$selected_shortname.mon'
      AND IP='$ip'
      ";
  while($MonitoredObjectID = mysql_fetch_row($get_moid)){
      //Sets MonitoredObjectID for added/edited device.
      $Response = $MonitoredObjectID;
      if ($logon_choice = '1') {
          $Response = $Response'&amp;'$logon_id;
          $Response = $Response'&amp;'$logon_pwd;
      }
  }
  echo json_encode($response);
  </code></pre>
  
  <p><strong>JS:</strong></p>
  
  <pre><code>function submit_data(action, formName) {
      "use strict";
      $.ajax({
          cache: false,
          type: 'POST',
          url: 'library/plugins/' + action + '.php',
          data: $('#' + formName).serialize(),
          success: function (response) {
              // PROCESS DATA HERE
              var resp = $.parseJSON(response);
              $.get('/nagios/cgi-bin/add_host.cgi', {moid: resp });
              alert('success!');
          },
          error: function (response) {
              //PROCESS HERE FOR FAILURE
              alert('failure 'response);
          }
      });
  }
  </code></pre>