javascript - How to pass Variable Values from JS to PHP (not via form) -
i'm new here. i'm trying pass variable values client php server. looked matter , saw many answers, of them regarding sending form values. i'm trying transfer actual variable values. simple example (multiple instances of following button):
$("#aam").click(function () { var e = confirm("are sure?") if(e == true) { $(this).hide('slow'); counter++; } });
every time button clicked counter goes 1 (after "if" checks). want counter value server, how should done. search learned ajax unfortunately couldn't adjust own scenario... tried ajax request:
$("#click_ajax").click(function() { $.ajax({ type: 'post', url: 'c:/xampp/htdocs/abby/test.php', data: {counter} }); });
with php:
<?php $counter = $_post['counter']; echo $counter ?>
unfortunately didn't work - whatever do, "notice: undefined index: counter in c:\xampp\htdocs\abby\test.php on line 9" error via xampp. hope can assist me. in advance,
abby.
you not send correct data thru ajax request. try this:
js:
var ajaxurl = 'abby/test.php'; $.ajax({ type: "post", url: ajaxurl, //check if in ajax file data: { action: "sendcounter", counter: counter } }).always(function() { }).done(function(data) { console.log(data); // should server counter number });
php (more elegant way of ajax file):
if (isset($_post['action'])) { $straction = $_post['action']; switch ($straction) { case 'sendcounter': if (isset($_post['counter'])) { echo $_post['counter']; } break; } }
hope helps.
Comments
Post a Comment