开发者

passing php variable through ajax

In jquery ajax function, how can php variable pas开发者_Go百科s through url?

php : send.php?id=<?php '.$news_id.' ?>

$.ajax({
 type: "POST",
 url: "ABOVE URL",
 data: str});


<head>
  <script type="text/javascript">
  ...
  $.ajax({
    type: 'POST',
    url: 'send.php?id=<?php echo $news_id; ?>',
    data: str
  });
  ...

Like that?


  1. Use PHP to create valid HTML and/or JS files on your server.
  2. Wait for them to be sent to the browser over the Internet.
  3. When they arrive, the browser will execute whatever is there.


Are you wanting to pass the value of the href to the URL of the ajax call? Assuming you're clicking on a link?

$('a.thisLink').click(function(){

    $.ajax({
     type: "POST",
     url: $(this).attr('href'),
     data: str
    });

});

If you want to pass a value from a form to send.php, you can do that like this.

function sendFormData(formData){

    $.ajax({
     type: "POST",
     url: 'send.php',
     data: formData
    })    

};

// when the form is submitted
$('form[name=foo]').submit(function(){

    // pass the entire form?
    var formData = $(this).serialize();

    // or just one input?
    var formData = $('form[name=foo] input[name=bar]').val();

    sendFormData(formData);

});
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜