javascript - jQuery/Ajax - Trying to create a POST method by Ajax and get response to HTML -
guys i'm trying create simple html post method ajax.
here code:
<?php function callinstagram($url) { $ch = curl_init(); curl_setopt_array($ch, array( curlopt_url => $url, curlopt_returntransfer => true, curlopt_ssl_verifypeer => false, curlopt_ssl_verifyhost => 2 )); $result = curl_exec($ch); curl_close($ch); return $result; } $tag = mage::getstoreconfig('vivastags/vivasgroup/instagra_tag'); $client_id = "1e0f576fbdb44e299924a93cace24507"; $next_url = $_get["nexturl"]; if($next_url == ""){ $url = 'https://api.instagram.com/v1/tags/'.$tag.'/media/recent?client_id='.$client_id.'&count=24'; } else { $url = $next_url; } $inst_stream = callinstagram($url); $results = json_decode($inst_stream, true); $maxid = $results['pagination']['next_max_id']; $nexturl = $results['pagination']['next_url']; //now parse through $results array display results... ?> <div class="holder"> <?php foreach($results['data'] $item){ $image_link = $item['images']['thumbnail']['url']; $profile_name = $item['user']['username']; $postid = $item['id']; echo '<div style="display:block;float:left;">'.$profile_name.' <br> <img src="'.$image_link.'" /></div>'; } $nexturlencoded = urlencode($nexturl); ?> <div id="loadedresults"></div> </div> <?php $nexturlencoded = urlencode($nexturl); ?> <button type="button" id="loadmore">load more images!</button> <script> jquery(document).ready(function($) { jquery('#loadmore').click(function() { var nexturl = "<?php echo $nexturlencoded;?>"; $.ajax({ url: 'ajax.php', type: 'post', datatype: 'html', data: { next_url: nexturl }, }).done(function ( data ) { $('#loadedresults').append(data); }); alert("post sended!"); }); }); </script>
note have included jquery1.9.1 in head tags.
here ajax.php:
<?php function callinstagram($url) { $ch = curl_init(); curl_setopt_array($ch, array( curlopt_url => $url, curlopt_returntransfer => true, curlopt_ssl_verifypeer => false, curlopt_ssl_verifyhost => 2 )); $result = curl_exec($ch); curl_close($ch); return $result; } $tag = "bulgaria"; $client_id = "1e0f576fbdb44e299924a93cace24507"; $next_url = $_post["next_url"]; $url = $next_url; $inst_stream = callinstagram($url); $results = json_decode($inst_stream, true); $maxid = $results['pagination']['next_max_id']; $nexturl = $results['pagination']['next_url']; //now parse through $results array display results... foreach($results['data'] $item){ $image_link = $item['images']['thumbnail']['url']; $profile_name = $item['user']['username']; echo '<div style="display:block;float:left;">'.$profile_name.' <br> <img src="'.$image_link.'" /></div>'; }
the problem there no response, when click button nothing happens.
where mistake , how can make work?
note, in ajax, have type: 'post'
. php should looking "post" properties:
$_get["next_url"];
should be
$_post["next_url"];
also,
you need put quotes around this:
var nexturl = "<?php echo $nexturlencoded;?>";
finally, if still doesn't work, open developer tools in chrome , check out network tab. make sure xhr sending right data. i'm curious if $nexturlencoded
never getting set correctly. make sure response xhr includes information. determine break coming from.
Comments
Post a Comment