javascript - 将变量从 .js 传递到 PHP,然后接收新的 var 作为响应?

标签 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 变量发送回原始页面?

谢谢!

最佳答案

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;

关于javascript - 将变量从 .js 传递到 PHP,然后接收新的 var 作为响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22340909/

相关文章:

javascript - 获取 ExtJS 中开始/结束日期之间的日期

php - 登录 php 和 mysqli

PHP 与 MySQLi 并使用变量执行 MySQL 脚本?

php - 我可以在 PHP 中混合使用 MySQL API 吗?

javascript - 500 错误 : Failed to lookup view "index" in views directory

javascript - 我可以使用什么选择器来排除带后缀的 ID?

javascript - 同一根上的路由器链接中的 Angular 6 冲突

php - 我可以使用 preg_replace 在多行模式下去除尾随\n 吗?

php - 谷歌分析第22条

javascript - 如何针对不同的国家/地区测试我的网站?