我在 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' );
});
这将发送两个参数(param1
和 param2
到名为 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;
?>