Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 29th, 2012  |  syntax: None  |  size: 2.31 KB  |  hits: 11  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Query string parameters from Javascript do not make it to processing PHP  script
  2. <div id="ListBox">
  3.                 <script type="text/javascript">
  4.                 popList("name", "categories");
  5.             </script>
  6.             </div>
  7.        
  8. function popList(field, table) {
  9.     $.ajax({
  10.         type: "GET",
  11.         url: 'getListOfValues.php',
  12.         data: 'field='+escape(field)+'&table='+escape(table),
  13.         dataType: 'json',
  14.         success: function(response) {
  15.             var source = $.parseJSON(response);
  16.             $("#ListBox").jqxListBox({ source: source, checkboxes: true, width: '400px', height: '150px', theme: 'summer'});
  17.         },
  18.         error: function() {
  19.             alert('sources unavailable');
  20.         }
  21.     });
  22. }
  23.        
  24. <?php
  25.     require "dbinfo.php";
  26.  
  27.     // Opens a connection to a MySQL server
  28.     $connection=mysql_connect($host, $username, $password);
  29.     if (!$connection) {
  30.         die('Not connected : ' . mysql_error());
  31.     }
  32.  
  33.     // Set the active MySQL database
  34.     $db_selected = mysql_select_db($database, $connection);
  35.     if (!$db_selected) {
  36.         die ('Can't use db : ' . mysql_error());
  37.     }
  38.  
  39.     $field = $_GET["field"];
  40.     $table = $_GET["table"];
  41.  
  42.     $field = mysql_real_escape_string($field);
  43.     $table = mysql_real_escape_string($table);
  44.  
  45.     $qryString = "SELECT " . $field . " FROM " . $table;
  46.  
  47.     $qryResult = mysql_query($qryString) or die(mysql_error());
  48.  
  49.     $source = array();
  50.  
  51.     while ($row = mysql_fetch_array($qryResult)){
  52.         array_push($source, $row[$field]);
  53.     }
  54.  
  55.     mysql_close($connection);
  56.  
  57.     echo json_encode($source);
  58. ?>
  59.        
  60. ajaxRequest.onreadystatechange = processAjaxResponse;
  61.        
  62. function processAjaxResponse() {
  63.     if (ajaxRequest.readySTate == 4) {
  64.         var response = ajaxRequest.responseText;
  65.         //do something with the response
  66.         //if you want to decode the JSON returned from PHP use this line
  67.         var arr = eval(response);
  68.     }
  69. }
  70.        
  71. print json_encode($listOfReturnedValues);
  72.        
  73. function popList(field,table) {
  74.     $.ajax({
  75.             type: "GET",
  76.             url: 'getListofValues.php',
  77.             data: 'field='+escape(field)+'&table='+escape(table),
  78.             dataType: "json",
  79.             success: function(response) {
  80.                 //the response variable here would have your array automatically decoded
  81.             }
  82.         });
  83. }