首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在jQuery加载时传递PHP变量?

如何在jQuery加载时传递PHP变量?
EN

Stack Overflow用户
提问于 2012-10-19 11:39:04
回答 3查看 2.1K关注 0票数 2

我的代码是这样的:

代码语言:javascript
复制
<?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?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-10-19 11:58:27

<body>中,您可以添加隐藏字段

代码语言:javascript
复制
<input type="hidden" value="<?=$username?>" id="username" />

在你的jquery中,

代码语言:javascript
复制
$('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();
});
票数 1
EN

Stack Overflow用户

发布于 2012-10-19 12:05:06

使用jQuery的.ajax()而不是.load()http://api.jquery.com/jQuery.ajax/

代码语言:javascript
复制
$('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属性附加到元素(如正文);也可以只在标头中使用一个脚本块来定义用户名。例如:

代码语言:javascript
复制
<input type="hidden" name="username" value="<?php echo $username;?>>

或者:

代码语言:javascript
复制
<body data-username="<?php echo $username;?>">

或者:

代码语言:javascript
复制
<head>
    <script>
        var username = "<?php echo $username;?>";
    </script>
</head>
票数 2
EN

Stack Overflow用户

发布于 2012-10-19 11:58:46

代码语言:javascript
复制
$('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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12967035

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档