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?
- Use PHP to create valid HTML and/or JS files on your server.
- Wait for them to be sent to the browser over the Internet.
- 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);
});
精彩评论