javascript - How do i make my php variable accessible? -
i trying implement timer. learned idea post.
<?php if(($_server['request_method'] === 'post') && !empty($_post['username'])) { //secondsdiff declared here $remainingday = floor($secondsdiff/60/60/24); } ?>
this php code. php,html , js codes in same page. have button in html. when user clicks on html page, call ajax function
//url:"onlinetest.php", //datatype: 'json', beforesend: function() { $(".startmytest").off('click'); setcountdown(); }
it call setcountdown() method, contains line @ beginning
var days = <?php echo $remainingday; ?>;
when run page, says[even before clicking button] "expected expression, got '<'"
in above line. doubt is
why php variable replaced before triggering button. please let me know hoe solve or how change idea.
the problem is, since initial load, $_post
values aren't populated (empty on first load),
that variable set undefined, make sure initialize variable fist.
<?php // initialize $remainingday = 1; if(($_server['request_method'] === 'post') && !empty($_post['username'])) { //secondsdiff declared here $remainingday = floor($secondsdiff/60/60/24); echo json_encode(array('remaining_day' => $remainingday); exit; } ?> <script> var days = <?php echo $remainingday; ?>; $('.your_button').on('click', function(){ $.ajax({ url: 'something.php', datatype: 'json', type: 'post', beforesend: function() { // whatever processes need }, success: function(response) { alert(response.remaining_day); } }); }); </script>
that basic idea, added other codes particular example, add/change rest of logic thats needed on side.
Comments
Post a Comment