javascript - ajax test not working -
i tried simple ajax-test working without success. trying pass variable($add1) php js, let js calculate sum , return php variable via $_post , echo out in html.
index.php:
<?php echo "<script type='text/javascript' src='script.js'></script>"; $add1 = 3; echo "<script>sum($add1)</script>"; $sum = $_post['variable']; echo $sum; ?>
script.js:
function sum(param) { var sum = 3; sum = 3 + param; $.ajax({ type: 'post', url: 'index.php', data: { 'variable': sum }, }); }
for reason code doesn't work. appreciated.
i want php file pass $add1 javascript , want javascript return var sum php , php echo $sum $post.
$sum
$_post
echoed, not shown in html page, handled response ajax.
you have add success
function in ajax closure
$.ajax({ type: 'post', url: 'index.php', data: { 'variable': sum }, success: function (data){ document.write(data); } });
you see answer show in page.
php:
if (isset($_post['variable'])) { $sum = some_futher_function($_post['variable']); echo $sum; } else { echo "<script type='text/javascript' src='script.js'></script>"; $add1 = 3; echo "<script>sum({$add1})</script>"; } function some_futher_function($param){ //do thing return $return; }
Comments
Post a Comment