Tuesday, 15 July 2014

jquery AJAX requests not updating php variable -



jquery AJAX requests not updating php variable -

i have comics website, hitting trees sticks, allows user next, previous, or random comic id pressing next or pressing arrow keys.

since images stored in database, way me cycle through these images on client side store them in javascript array, , store php $imgid in javascript variable imgindex. alter index on client side when press keyboard keys.

when user presses key, i'm using pushstate alter imgid in url, that's not updating server side $imgid. need update server side $imgid because i'm associating liking function each specific id... currently, total likes associated img id won't refresh/update when press key new image.

my solution not utilize pushstate update url, when key pressed, utilize $.post, , send updated imgindex php script.

here snippets:

keyinput.php: client-side javascript:

<script type="text/javascript"> var imgarray = [<?php echo implode(',', getimages($site)) ?>]; var imgindex = <?php echo $imgid ?>; $(document).ready(function() { var img = document.getelementbyid("showimg"); img.src = imgarray[<?php echo $imgid ?>]; $(document).keydown(function (e) { var key = e.which; var rightarrow = 39; var leftarrow = 37; var random = 82; if (key == rightarrow) { imgindex++; if (imgindex > imgarray.length-1) { imgindex = 0; } img.src = imgarray[imgindex]; window.history.pushstate(null, null, '.?action=viewimage&site=comics&id=' + imgindex); $.post('./templates/viewimage.php', { _newimgid : imgindex }, function(data) { //alert(data); } ); }

viewimage.php file gets $imgid, calls keyinput.php script take key input... alters javascript imgid. testing purposes, i've tried using $.post , $.get ajax send updated imgid, can see below, that's $newid = $_post['_newimgid];. when echo out $newid, says it's not defined.

<?php /* controls display of comic on viewcomic template */ include 'include/header.php'; global $site, $imgid; $cat = (isset($_get['cat']) ? ($_get['cat']) : null); $site = (isset($_get['site']) ? ($_get['site']) : null); $title = (isset($_get['title']) ? ($_get['title']) : null); $imgid = $_get['id']; include './scripts/keyinput.php'; $newid = $_post['_newimgid]; echo $newid; //this should echo updated id, says not defined

any thoughts?

thanks!

i think problem code using ajax code after page has loaded, while trying alter $_get variables initial page load. afaik, need update entire page "facebook like" button alter it's id. alternative utilize iframe. can see, button's data-href attributes leads http://hittingtreeswithsticks.com/ - , can changed reloading page using different attribute.

if don't mind loading page each picture, can work out you:

<!-- button code --> <div [...] data-href="http://www.hittingtreeswithsticks.com/<?php echo $_get['id']; ?>"></div>

and address page be: http://www.hittingtreeswithsticks.com/?id=page_id

edit

using ajax, calling info backend used in client side, without having reload entire page. info can used alter code in client side. in code, info beingness sent not using @ all, that's why doesn't work:

$.get('./templates/viewimage.php', { _newimgid : imgindex }, function(data) { // should create utilize of info received } );

edit #2

if want dynamically alter button's url, take @ this answer.

here fiddle of working example: http://jsfiddle.net/tkfma/4/

php jquery

No comments:

Post a Comment