我的代码是这样的:
<?php
$username = 'johndoe';
?>
<head>
<script>
...
$('a.manage-content-link').click(function (e) {
var self = $(this),
file = self.siblings('input[type="hidden.block-hidden-input"]').val();
self.next(".manage-content-wrap").find(".manage-content").load("file-" + file + ".php");
e.preventDefault();
});
...
</script>
</head>
<body>
...
<li><input type="hidden" value="001" class="block-hidden-input" />
<a href="#" id="manage-1" class="manage-content-link">
<img src="images/web-block/web-block1.jpg"/>
<span class="orange-notice">Click to Edit Content</span>
</a>
</li>
<li><input type="hidden" value="002" class="block-hidden-input" />
<a href="#" id="manage-2" class="manage-content-link">
<img src="images/web-block/web-block2.jpg"/>
<span class="orange-notice">Click to Edit Content</span>
</a>
</li>
...
</body>如你所见,每次用户点击"manage-content-link“类时,manage-1,manage-2,...或者甚至manage-X (多个li标签) jQuery将加载"file-XXX.php“。XXX实际上是li标签中隐藏输入的值。
但是"file-XXX.php“需要来自PHP标签和ID本身的管理,即”$username -X“。如何传递"file-XXX.php“所需的两个变量,一个来自PHP,另一个来自ID?
发布于 2012-10-19 11:58:27
在<body>中,您可以添加隐藏字段
<input type="hidden" value="<?=$username?>" id="username" />在你的jquery中,
$('a.manage-content-link').click(function (e) {
var self = $(this),
file = self.siblings('input[type="hidden.block-hidden-input"]').val();
var username = $("username").val(); //use this variable where ever you want
var ids = $(this).attr('id'); // this is the id
self.next(".manage-content-wrap").find(".manage-content").load("file-" + file + ".php?id="+ids+"&username="+username); //and in php file usee $_GET
e.preventDefault();
});发布于 2012-10-19 12:05:06
使用jQuery的.ajax()而不是.load():http://api.jquery.com/jQuery.ajax/
$('a.manage-content-link').click(function (e) {
var self = $(this),
file = self.siblings('input[type="hidden.block-hidden-input"]').val(),
this_id = self.attr('id');
$.ajax({
url: "file-" + file + ".php",
data: { username: "<?php echo $username;?>", id: this_id },
context: this,
success: function(data) {
$(this).next(".manage-content-wrap").find(".manage-content").html(data);
}
});
e.preventDefault();
});如果你想把脚本放在外部,你就不能依赖php在脚本内部回显$username。因此,您可以通过几种方式添加用户名。您可以在页面中的某个位置使用等于用户名的值进行隐藏输入;可以将用户名作为data-username属性附加到元素(如正文);也可以只在标头中使用一个脚本块来定义用户名。例如:
<input type="hidden" name="username" value="<?php echo $username;?>>或者:
<body data-username="<?php echo $username;?>">或者:
<head>
<script>
var username = "<?php echo $username;?>";
</script>
</head>发布于 2012-10-19 11:58:46
$('a.manage-content-link').click(function (e) {
var self = $(this);
file = self.prev('.block-hidden-input').val();
self.next(".manage-content-wrap").find(".manage-content").load("file-" + file + ".php");
e.preventDefault();
});与其从脚本中传递用户名,我建议你将用户名存储在一个会话中,并使用$_SESSION['username']在php中获取该值,否则它会在将来引起安全问题。
DEMO
https://stackoverflow.com/questions/12967035
复制相似问题