如何在ajax url中使用php变量?

时间:2014-09-10 11:34:40

标签: php ajax

我想在AJAX网址中使用PHP变量。我怎样才能做到这一点?

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

这里我想在AJAX url中使用这些PHP变量

我怎样才能做到这一点?

4 个答案:

答案 0 :(得分:2)

将您的PHP代码移到js之上并在js中添加php代码以获取您的php变量

<?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>

答案 1 :(得分:1)

请尝试以下方法: -

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

答案 2 :(得分:0)

试试这个:

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

答案 3 :(得分:-1)

嗯,你没有按照建议的方式在ajax网址上共享数据,但如果你想按照你上面提到的那样做,那么下面是你可能正在寻找的解决方案,这是一种肮脏的方式:

将第4行修改为:

url: "<?php echo "http://www.bulksmsgateway.in/sendmessage.php?user=Ami&password=74153&mobile=$number&message=$message&sender=INFORM&type=3"; ?>",