foshoeiyyy
foshoeiyyy

Reputation: 471

posting to php using $.ajax

I am using $.ajax jquery method, to post num to php, but im not sure how to pick it up on the php side, this is what i have, gives an undefined index error on num so its not finding it.

I would like to know how to pick the variable up in php after posting it there via the $.ajax method.

<?php 

$lol =  $_POST['num'];

echo " $lol";


?>

this is the JS:

<script>
 var num = 1;
                    function ajax_post(){ 
$.ajax('javas.php', {
success: function(response) {
      $(".status").html(response);
}, 
data: "num=" + (++num)
});
}

function ajax_posta(){
$.ajax('javas.php', {
success: function(response) {
      $(".status").html(response);
}, 
data: "num=" + (--num)
});
}

$(document).ready(function() {
$('.eventer > .button').click(function () {
    ajax_post();
});
alert("lol");
});


</script>

This JS is carried out on click of a button

Upvotes: 1

Views: 155

Answers (2)

Ali
Ali

Reputation: 267077

Rather than $.ajax I recommend using $.post instead.

Upvotes: 0

Eric
Eric

Reputation: 1374

$.ajax uses GET by default, so either use $_GET instead of $_POST in your PHP script, set the type setting in your $.ajax() call to "post", or use $.post() (which uses $.ajax internally).

Upvotes: 8

Related Questions