开发者

jquery, how to get var from $.ajax POST?

edit3: i am using this script inside a facebook app, and not sure how to do it

i am passing a var to a php file using a post:

<div id="talentnum" class="cont_talentnum"><?php echo $number; ?></div>
<a class="mine_click" href="#"></a>

js here

 var strtalentnum;
$('.mine_click').live('click', function() {
    strtalentnum = $(this).closest("li").find(".cont_talentnum").text();
    $('#mine').trigger('click');

});

$("#mine").click(function(){
 if(strtalentnum){
  $.ajax({  
  type: "POST",  
  url: "fb_test.php",  
  data: strtalentnum,  
  success: function() { 
    var talentnum = strtalentnum; //this is 6203222
  alert(strtalentnum);
  }  
 开发者_C百科 });  
 }
});

and the php file is :

<?php
function fb_test()
{ echo 'xxx';
echo $_GET['strtalentnum'];
}
?>

then i call the function in the same html page:

<?php  function fb_test(); ?>

If i run this i get only the xxx. I also get the success alert in my case '6203222' so i know that the POST is happening

if i look in Chrome Network Headers i can see that :

Request Method:POST
Status Code:200 OK
Form Data
6203222:

Why i cant get it using $_GET['strtalentnum']; ?

any ideas?

thanks

edit: $_POST['strtalentnum']; wont do the job either

edit2 added html


You need a variable name for that:

var strtalentnum = $('#strtalentnum').val();
$("#mine").click(function(){
 if(strtalentnum){
  $.ajax({  
  type: "POST",  
  url: "fb_test.php",  
  data: "strtalentnum=" + strtalentnum,
  dataType: 'html',  
  success: function() { 
    var talentnum = strtalentnum; //this is 6203222
  alert(strtalentnum);
  }  
  });  
 }
});

Form:

<form method="post">
<input id="strtalentnum" type="text" name="strtalentnum" value="526558" />
<input id="mine" type="submit" name="submit" />
</form>

Test it now:

<?php
function fb_test()
{ echo 'xxx';
echo $_POST['strtalentnum'];
}
?>


Use $_POST and not $_GET to access variables passed via the POST method


Because you have to get this by $_POST['strtalentnum'] not $_GET['strtalentnum'].


For you data you can't pass a string the variable needs a name you are essentially saying

fb_test.php?strtalentnum= 

If it was a GET.

Change your data (in $.ajax) to this:

  $.ajax({  
  type: "POST",  
  url: "fb_test.php",  
  data: {strtalentnum: strtalentnum }  
  success: function() { 
    var talentnum = strtalentnum; //this is 6203222
    alert(strtalentnum);
   }  
  });
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜