SEND SIDE (myfile.php)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <script> $(document).ready(function(){ $("#mybutton").click(function() { $.ajax({ url: "process.php", dataType:'json', success:function(data) { $('#result1').html(data.first ); $('#result2').html(data.second ); $('#result3').html(data.third); } }); });//end click function }); </script> <div id="result1"></div> <div id="result2"></div> <div id="result3"></div> <input type ="button" id="mybutton" value ="Click me"> |
RECEIVE SIDE (process.php)
1 2 3 4 5 6 7 8 9 10 11 12 13 |
//get values as you would $nowbudget = $row['budget']; $nowclicks = $row['clicks']; $nowimpressions = $row['impressions']; //Place into array $myvals = array('first'=>$nowbudget,'second'=>$nowclicks,'third'=>$nowimpressions); //senddata back header('Content-type: application/json'); echo json_encode($myvals); |
This may also help….
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
$sql = mysql_query("SELECT policynumuser FROM users where policynumuser = '$user_id' LIMIT 0,1"); $results = array(); while($row = mysql_fetch_array($sql)) { $results[] = array( 'Username' => $row['policynumuser'], 'Error' => 'false', 'Message' => '' ); } header('Content-type: application/json'); echo json_encode($results); |
Version 2 Sending side
1 2 3 4 |
{Do MYSQL query} echo json_encode($row); |
Version 2 Receiving side
(Without adding header(‘Content-type: application/json’);
in previous sending page.
You will need to add
var data = $.parseJSON(data);
to parse the json.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
$.ajax({ url: "getmystuff.php", dataType: "json", type: 'GET', data: "var1=" + mikesstuff + "&var2=" + mikestuffagain, success: function(data){ alert(data); var data = $.parseJSON(data); alert(data.myclicks); $('#amount1 span').html(data.myclicks); } //end data |
Version 3 Sending side
getmystuff.php
1 2 3 4 5 6 7 |
$results = mysql_query("SELECT * FROM table1 WHERE id = $qnum"); header('Content-type: application/json'); echo json_encode($results); |
and the button clicked page that sent the request
1 2 3 4 5 6 7 8 9 10 11 12 |
$.ajax({ url: "getmystuff.php", dataType: "json", type: 'GET', data: {accountid: accid , tagchose: tagchose}, success: function(data){ alert(data.myclicks); $('#amount1').html(data.myclicks); } }); //end ajax |