0

In jquery ajax function, how can php variable pass through url?

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

$.ajax({
 type: "POST",
 url: "ABOVE URL",
 data: str});
4
  • 2
    You can't. php is a serverside language. Javascript client side. Best thing would be to store the id in a hidden input or something in html so you can retrieve it with javascript/jQuery. Commented Dec 8, 2010 at 20:18
  • cant jquery post to links like send.php?id=111 ?? Commented Dec 8, 2010 at 20:31
  • Of course it can. If you write your JS file to include that text there, or dynamically generate the URL in client-side JS use some other variable written to JS or HTML by PHP. Commented Dec 8, 2010 at 20:47
  • Brad Christie posted the right answer! Commented Dec 8, 2010 at 22:02

3 Answers 3

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

Like that?

Sign up to request clarification or add additional context in comments.

2 Comments

Both insightful and humorous. :)
If you can't have a little fun it's not worth the expense. ;-)
0
  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.

1 Comment

Yes, I know the The issue theorically :))
0

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);

});

4 Comments

I want to insert value of a form in database
Check out the update, is that what you are looking for? You might want to elaborate a bit more on your question.
sendData(formData); --> sendFormData(formData); when calling the function. Just to be correct. :) (But I think that's not the right answer, I think Brad Christie is on the good way. :) )
Whoops, you are correct sir. I was just stabbing in the dark. My ESP aint so great. ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.