I want to use a PHP variable in AJAX url. How can I achieve this?
my.php
function displayRecords() {
$.ajax({
type: "GET",
url: "http://www.bulksmsgateway.in/sendmessage.php?user=Ami&password=74153&mobile=$number&message=$message&sender=INFORM&type=3",
data: "show="+numRecords+"&pagenum="+pageNum,
cache: false,
beforeSend: function () {
$('#content').html('<img src="loader.gif" alt="" width="24" height="24" style=" padding-left:469px;">');
},
success: function(html) {
$("#results").html( html );
}
});
}
<?php
$message="hi"
$number=8888888888;
?>
Here I want to use these PHP variables in AJAX url
How can I achieve this?
MrTux
34.4k30 gold badges120 silver badges158 bronze badges
asked Sep 10, 2014 at 11:34
anil kumar
151 gold badge3 silver badges9 bronze badges
-
Move the php block above the javascript function.Steve– Steve2014年09月10日 11:36:21 +00:00Commented Sep 10, 2014 at 11:36
-
1Though having your api credentials in your javascript for all to see seems a particularly bad ideaSteve– Steve2014年09月10日 11:36:59 +00:00Commented Sep 10, 2014 at 11:36
-
1php.net/manual/en/tutorial.phpSverri M. Olsen– Sverri M. Olsen2014年09月10日 11:37:04 +00:00Commented Sep 10, 2014 at 11:37
-
One down vote for including API credentials in the question. Don't be dumb again. @anil kumaruser3962692– user39626922014年09月10日 11:49:35 +00:00Commented Sep 10, 2014 at 11:49
-
Here this is not my original apianil kumar– anil kumar2014年09月10日 11:51:08 +00:00Commented Sep 10, 2014 at 11:51
3 Answers 3
move your php code above js and add php code in js to get your php variables
<?php
$message="hi"
$number=8888888888;
?>
<script>
function displayRecords() {
$.ajax({
type: "GET",
url: "http://www.bulksmsgateway.in/sendmessage.php?user=Ami&password=74153&mobile=<?php echo $number;?>&message=<?php echo $message;?>&sender=INFORM&type=3",
data: "show="+numRecords+"&pagenum="+pageNum,
cache: false,
beforeSend: function () {
$('#content').html('<img src="loader.gif" alt="" width="24" height="24" style=" padding-left:469px;">');
},
success: function(html) {
$("#results").html( html );
}
});
}
</script>
answered Sep 10, 2014 at 11:39
Rakesh Sharma
13.7k5 gold badges41 silver badges44 bronze badges
Sign up to request clarification or add additional context in comments.
3 Comments
Khushboo
Oh... we have replied same :)
Rakesh Sharma
@Khushboo yeah you need to upvote if you got correct rather another answer
Smokey
A little fight in you. I like that. @anil kumar
Try below :-
<?php
$message="hi"
$number=8888888888;
?>
function displayRecords() {
$.ajax({
type: "GET",
url: "http://www.bulksmsgateway.in/sendmessage.php?user=Ami&password=74153&mobile=<?php echo $number; ?>&message=<?php echo $message; ?>&sender=INFORM&type=3",
data: "show="+numRecords+"&pagenum="+pageNum,
cache: false,
beforeSend: function () {
$('#content').html('<img src="loader.gif" alt="" width="24" height="24" style=" padding-left:469px;">');
},
success: function(html) {
$("#results").html( html );
}
});
}
answered Sep 10, 2014 at 11:40
Khushboo
1,8191 gold badge11 silver badges17 bronze badges
1 Comment
Taryn
Moderator Note I've removed all the unnecessary comments here. If you feel the need to discuss these issues, then take it to chat. I see no evidence that this was a copied answer so stop arguing about it.
try this:
<script>
function displayRecords() {
var numRecords = '<?php echo hi; ?>';
var pageNum = '<?php echo 8888888888; ?>';
$.ajax({
type: "GET",
url: "http://www.bulksmsgateway.in/sendmessage.php?user=Ami&password=74153&mobile=
<?php echo $number;?>&message=<?php echo $message;?>&sender=INFORM&type=3",
data: "show="+numRecords+"&pagenum="+pageNum,
cache: false,
beforeSend: function () {
$('#content').html('<img src="loader.gif" alt="" width="24" height="24" style="
padding- left:469px;">');
},
success: function(html) {
$("#results").html( html );
}
});
}
</script>
answered Sep 10, 2014 at 11:43
munsifali
1,7432 gold badges24 silver badges44 bronze badges
1 Comment
Rakesh Sharma
where is $message and other php var values?
lang-php