简体   繁体   English

使用javascript / JQuery传递2个php变量

[英]Passing 2 php variables using javascript/JQuery

I have a Jquery code that listens for the selection from a user and displays the information that the user needs. 我有一个Jquery代码,用于侦听用户的选择并显示用户所需的信息。 The user selects a "folder" and then the content appears after they selected. 用户选择一个“文件夹”,然后在他们选择之后出现内容。

There are 2 pages. 共有2页。 The first page has the jquery code and the selection box and the div that the information is the be displayed in. Our problem is I got the folder name to pass but I need to pass along the $username as well so that it knows what folders to look up. 第一页包含jquery代码,选择框和显示信息的div。我们的问题是我可以传递文件夹名称,但是我也需要传递$ username,以便它知道要使用的文件夹去查查看。

This is the first page. 这是第一页。 It's the selection box and also jquery code. 这是选择框,也是jquery代码。

<script type="text/javascript" src="jquery-1.9.1.min.js">
</script>

<script type="text/javascript">
$(document).ready(function () {
    $("#theselect").change(function() {
var option = $(this).val();
        $.get("selectfolders.php", {select:option},function(data){
            $("#theresult").html(data).hide().fadeIn(1000);
        });
    });
});
</script>




echo "
<select name='thename' id='thename' style='visibility:hidden;' >
<option value='$username'>$username</option>
</select>";
?>
<select name="theselect" id="theselect">
<option value="">Select</option>
<?
$thelistquery = mysql_query("SELECT * FROM folders WHERE username='$username'");
while ($lrows = mysql_fetch_array($thelistquery)) {
$id = $lrows['ID'];
$username = $lrows['username'];
$foldername = $lrows['foldername'];
$newfoldername = mysql_real_escape_string($foldername);

echo "<option value='$newfoldername'>$newfoldername</option>";
}
?>
</select>

This is the second page. 这是第二页。 It generates the list of folders and displays the folder the user selects on the first page: 它生成文件夹列表,并在第一页上显示用户选择的文件夹:

$thelistquery = mysql_query("SELECT * FROM folders WHERE username='$username'");
while ($lrows = mysql_fetch_array($thelistquery)) {
$id = $lrows['ID'];
$username = $lrows['username'];
$foldername = $lrows['foldername'];
$newfoldername = mysql_real_escape_string($foldername);

if($_GET['select'] == '$newfoldername') {
echo "This is the $newfoldername folder.";
}
}

We need to add code the the javascript to take the $username from the first page and pass it to the second page so it knows whose folders to look up. 我们需要在JavaScript中添加代码以从第一页获取$ username并将其传递给第二页,以便它知道要查找的文件夹。 Any help? 有什么帮助吗?

$.get("selectfolders.php", {select:option, username:username},function(data){});

I've had a similar problem and this is how i fixed it: 我有一个类似的问题,这就是我如何解决它:

 $("#theselect").change(function() {
    var option = $(this).val();
    var selectedUser = $("#thename").val();
    $.get("selectfolders.php", {select:option,username:selectedUser},function(data){
        $("#theresult").html(data).hide().fadeIn(1000);
    });
 });

For this to work make sure that the username is selected at the moment adding the "selected" HTML attribute to the option. 为此,请确保在将“选定的” HTML属性添加到选项的同时选择了用户名。 However I think it will be easier if you just use a hidden block: 但是我认为如果只使用隐藏块会更容易:

<input type="hidden" value="$username" name="username" id="username" >

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM