将变量从.js传递给PHP,然后在响应中接收新的var返回?

时间:2014-03-12 02:36:44

标签: javascript php mysqli

我在服务器1上创建变量,然后使用位于服务器2上的PHP文件通过post发送到服务器2上的mySQL数据库。

服务器1上的Javascript ...

$.post( "http://www.website.com/xxx/update.php", { vote:currentVote, page:currentmatch, round:roundNumber, team1: firstTeam, team2: secondTeam });

PHP接收帖子,位于服务器2 ...

$round = $_POST['round'];
//etc...

//I then set the query and insert the data, no problem.

之后,在同一个PHP文件上,计算行数并将总数分配给变量......

$result = $connection->query("SELECT * FROM {$page} WHERE 'vote' = {$team1}")) {
    $team1Count = $result->num_rows;
}

如何将更新的$ team1Count变量发送回原始页面?

谢谢!

2 个答案:

答案 0 :(得分:2)

jQuery的.post()有一个可以作为参数提供的回调功能。

$.post( "http://www.website.com/xxx/update.php", { 
    vote:currentVote, 
    page:currentmatch, 
    round:roundNumber, 
    team1: firstTeam, 
    team2: secondTeam }, 
    function(data){
        //the call back, data contains your return from your php script
        console.log(data);
});

只需从php脚本中回显该变量

即可
echo $team1Count;

答案 1 :(得分:0)

尝试从php回显更新的变量。像这样的东西 -

$result = $connection->query("SELECT * FROM {$page} WHERE 'vote' = {$team1}")) {
    $team1Count = $result->num_rows;
    echo $team1Count;
}