从jquery文件中为php中的全局变量赋值


Assign value of a global variable in php from jquery file

我有一个html文件,在其中我定义了一个全局变量为

<?php
include("lib/db.class.php");
$db = new db();
$rings = $db->query("SELECT * FROM rings");
$mainRingId = 1;
?>

现在我有一个图像

<li><input type="image" src="<?php echo $ring['ringThumbNailImagePath']?> " name="checked" value="<?php echo $ring['id']?>" data-my-info="<?php echo $ring['ringSetName']?>"  width="150" height="150" /></li>

在我的jquery jquery.ring.js中,我将其点击定义为

$(document).on('click','input[type="image"]',function()
{
    $ringDesc = $(this).data('my-info');
    $('label#ringName').text($ringDesc);
    $ringId = $(this).val();
}

现在我想将这个$ringId分配给在我的html文件中定义的$mainRingId。我该怎么做呢?如果我想在我的html文件中做一些处理,这是如何实现的。

为了将数据从用户浏览器发送到服务器,您需要使用AJAX调用。你可以用纯javascript编写AJAX调用,也可以使用一些现有的库,比如jQuery,这个例子取自jQuery文档:

$.ajax({
    type: "POST",
    url: "some.php",
    data: { name: "John", location: "Boston" }
}).done(function( msg ) {
    alert( "Data Saved: " + msg );
});

AJAX调用的POST值可以通过以下方式在PHP脚本中访问:

$name = $_POST['name'];
$location = $_POST['location'];

在你的情况下,它看起来会有点不同,但原理是一样的。

你不能那样做。。唯一的方法是使用$_SESSION变量,并通过向将更改该变量值的文件发出$.post$.get ajax请求来设置该变量。