Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

ajax-json response

By: a guest on Oct 30th, 2012  |  syntax: PHP  |  size: 1.12 KB  |  views: 30  |  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. baseurl="/forcart/";
  2. function loadProduct(id) {
  3.   $.ajax({
  4.     url: baseurl+'loadProduct.php?id='+id,
  5.     success:function(json){
  6.       obj = JSON.parse(json);
  7.       if(obj.success == 1)
  8.       {
  9.         $('#name'+id).text(obj.proName);
  10.         $('#select'+id).html(obj.proSet);
  11.        
  12.       }
  13.       console.log(json);
  14.     },
  15.     error:function(error){console.log(error);}
  16.   });
  17. }      
  18.  
  19. /***************************
  20. the php file
  21. ***************************/
  22. $proId = $_GET[id]
  23.  $proSql = "select * from products where proCode='$proId'";
  24.   $result = runQuery($proSql);
  25.   $proName = '';
  26.   $x=0;
  27.   $select='';
  28.   $jsonArray = array();
  29.   if($result)
  30.   {
  31.     while($row = mysql_fetch_array($result))
  32.     {
  33.       $proName = $row['proName'];
  34.         $select .="<option value='".$proName."'>".$proName."</option>" ;
  35.       $x++;
  36.     }
  37.    
  38.    
  39.     $jsonArray = array(
  40.       'proName' => $proName,
  41.       'proSet' => $select,
  42.       'success' => 1
  43.     );
  44.   }
  45.   else{
  46.     $jsonArray = array(
  47.       'proName' => mysql_error(),
  48.       'proSet' => '',
  49.       'success' => 0
  50.     );
  51.   }
  52.  
  53.   return json_encode($jsonArray);