据我所知,除了 var tid 和 var open 部分之外,下面的代码可以正常工作。它们是从图像 href 提交的:
$("#chngeHref").click(function() {
var tid = $('tid').attr('value'); // VARIABLES DONT WORK
var open = $('open').attr('value'); // VARIABLES DONT WORK
$.ajax({
type: "POST",
url: "update.php",
data: "tid="+ tid +"& open="+ open,
success: function(){
$('#chnge').fadeTo('slow',0.4);
}
});
});
HTML 代码来自:
<a href="#" id="chngeHref" /><img src="<?php echo "image.php?url=" . $row[2]; ?>?tid=<?php echo $row[0]; ?>&open=<?php echo $row[1]; ?>" id="chnge" /></a>
效果非常好(输出:image.php?url=image.jpg?tid=1&open=1)。问题是我认为我没有正确设置 var tid/open 来实际读取变量并将它们传递到我的 mysql 页面(我需要值来更新数据库)。我尝试过:
var tid = $('tid').attr('value');
var tid = $('.tid').attr('value');
var tid = $('#tid').attr('value');
我只是不知道足够让它发挥作用。非常感谢任何建议。
菲利普。
最佳答案
我认为您能做的最好的事情是将变量传递到隐藏字段,以便您可以轻松访问信息。
类似的事情:
HTML
<input type="hidden" id="hfTid" value="<?php echo $row[0]; ?>" />
<input type="hidden" id="hfOpen" value="<?php echo $row[1]; ?>" />
jQuery
$("#chngeHref").click(function() {
var tid = $('input#hfTid').val();
var open = $('iput#hfOpen').val();
$.ajax({
type: "POST",
url: "update.php",
data: "tid="+ tid +"& open="+ open,
success: function(){
$('#chnge').fadeTo('slow',0.4);
}
});
});
我会这样做。更干净了。
如果有帮助我很高兴。
关于php - jQuery 将变量从 PHP 传递到 MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3537178/