javascript - 如何在 php 代码中使用 jQuery 变量?

标签 javascript php jquery variables

<分区>

我在 jQuery scriptm 中有 PHP 代码,我想将 jQuery 变量传递给 PHP。

这是我的代码:

$(document).ready(function() {
 $('.editclass').each(function() {
  $(this).click(function(){
    var Id = $(this).attr('id');
      <?php 
            include "config.php";
            $query="SELECT * FROM users WHERE UserId=\'id\'";

      ?>
 $("#user_name").val(Id);
   });
});

});

我希望 id 的值存在于 php 代码中 ($query)

最佳答案

使用$.post:

$(this).on('click', function(e){
    e.preventDefault();

    var Id = $(this).attr('id');

    $.post("yourscript.php", { 
        Id: Id 
    }, function(data){

        var theResult = data;
}, 'json' );

});

这将发送两个参数(param1param2 到名为 yourscript.php 的 php 脚本。然后您可以使用 PHP 来检索值:

$Id= isset($_POST['Id']) ? $_POST['Id'] : '';

想法是您通过 Ajax 将变量从客户端发送到服务器端。

你的脚本.php

 <?php 
     include "config.php";
     $query="SELECT * FROM users WHERE UserId=$Id";

    /* Get query results */

    $results = use_mysql_method_here();

    /* Send back to client */

    echo json_encode($results);
    exit;    
 ?>

关于javascript - 如何在 php 代码中使用 jQuery 变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24496612/

相关文章:

javascript - 如何从远程 JSON 文件仅收集特定数据

javascript - 如何使用 Systemsj 配置我的 index.html 以使用现有的 bundle ?

javascript - Google Sheet 脚本显然没有返回数字

php - UPDATE 反射(reflect)在 DB 但不是 SELECT 查询

php报表工具

php - preg_match() : No ending delimiter '/'

jquery - 从 Gmail 下拉选择菜单

javascript - 将 HTML 文件分成固定大小的页面

javascript - 使用 javascript/jquery 获取正文边距

php - 如何使用ajax传递参数获取数据(laravel)