anil kumar
anil kumar

Reputation: 15

How to use php variable in ajax url?

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?

Upvotes: 0

Views: 8648

Answers (3)

munsifali
munsifali

Reputation: 1733

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>

Upvotes: 0

Khushboo
Khushboo

Reputation: 1817

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

Upvotes: 2

Rakesh Sharma
Rakesh Sharma

Reputation: 13738

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>

Upvotes: 4

Related Questions