简体   繁体   English

使用$ .ajax发布到php

[英]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. 我正在使用$ .ajax jquery方法,将num发布到php,但是我不确定如何在php端进行拾取,这就是我所拥有的,在num上给出了未定义的索引错误,因此找不到它。

I would like to know how to pick the variable up in php after posting it there via the $.ajax method. 我想知道如何通过$ .ajax方法将变量发布到那里后,如何在php中选择变量。

<?php 

$lol =  $_POST['num'];

echo " $lol";


?>

this is the JS: 这是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 单击按钮即可执行此JS

$.ajax默认情况下使用GET,因此可以在PHP脚本中使用$_GET而不是$_POST ,将$.ajax()调用中的type设置设置为"post" ,或使用$.post() (使用$.ajax内部)。

我建议使用$.post而不是$.ajax

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM