Php变量从php文件到php文件使用jquery

时间:2015-07-18 07:29:29

标签: javascript php jquery ajax

我有一个小问题。我需要使用setInterval从php文件发送一个php变量到第二个php文件。我不知道如何在jquery代码中编写php变量。

first.php

<?php

$phpvariable=1;
?>

在JavaScript中

setInterval(function odswiez(id)
{

$('#chat').load('second.php?id=<here php variable how?>');
}, 3000);
});

second.php

<?php
$w=$_GET['id'];
echo $w;
?>

3 个答案:

答案 0 :(得分:0)

你需要回应你的价值

$('#chat').load('second.php?id=<?php echo $variable ?>');
}, 3000);

替代方法是这样做..

$('#chat').load('second.php?id=<?php echo "id-{$variable}" ?>');
    }, 3000);

答案 1 :(得分:0)

insted of

setInterval(function odswiez(id)
{

$('#chat').load('second.php?id=<here php variable how?>');
}, 3000);

setInterval(function odswiez(id)
{

    $.ajax({
        url: "second.php",
        type: "GET",
        data: { id: "<?php echo $php_id; ?>" }
        success: function(data){
            $('#chat').load(data);
        },
        error: function(){}             
    });


}, 3000);

答案 2 :(得分:0)

我认为这不是管理这种Ajax调用的最佳方式, 首先,我建议你不要把整个PHP文件传递给GET参数,你应该只传递params并只接收普通数据。

var myvalue = $('.whateverinput').val();

$.ajax({
      type: "POST",
      dataType: "json", //can be post or get
      url: "second.php", //you php who will recive data
      data: {myname : myvalue},
      success: function(data) {
        // Callback if all things goes Ok you will recive data 
        $(".the-return").html("here we atach the response"+data);
      },
      error: function(xhr, error){
        //if something wrong callback do something
        console.debug(xhr); console.debug(error);
      }
    });

我建议使用JSON在PHP和javascript之间发送/接收数据。 您可以在以下网址找到更多信息:

Example using ajax php and using json

祝你好运!