0

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
5
  • Move the php block above the javascript function. Commented Sep 10, 2014 at 11:36
  • 1
    Though having your api credentials in your javascript for all to see seems a particularly bad idea Commented Sep 10, 2014 at 11:36
  • 1
    php.net/manual/en/tutorial.php Commented Sep 10, 2014 at 11:37
  • One down vote for including API credentials in the question. Don't be dumb again. @anil kumar Commented Sep 10, 2014 at 11:49
  • Here this is not my original api Commented Sep 10, 2014 at 11:51

3 Answers 3

4

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
Sign up to request clarification or add additional context in comments.

3 Comments

Oh... we have replied same :)
@Khushboo yeah you need to upvote if you got correct rather another answer
A little fight in you. I like that. @anil kumar
2

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

1 Comment

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.
0

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

1 Comment

where is $message and other php var values?

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.