我有一个主文件
index.php
其中我包括其他四个文件,例如
header_get_started.php,
content_main.php,
right_sec_home.php,
footer.php.
这是我的代码
"index.php"
<script src="js/ajax.js"></script>
<?php include_once('header_getstarted.php'); ?>
<?php include_once('content_main.php'); ?>
<?php include_once('right_sect_home.php'); ?>
<?php include_once('footer.php'); ?>
"header_getstarted.php"
<span class="select_input">
<?php
$sqlCmd = "some query";
echo combo('cmb_command','custom-class1 custom-class2','cmd_name','cmd_id','0',$sqlCmd,'sendpostmtu()' $width="style='width:250px;cursor:pointer;'")
?>
<input type="submit" name="" class="sub_bg" value="" onclick="get_request_by_command($('#cmb_command').val());">
</span>
在header_get_started.php
从选择框中选择任何命令时,我想将其 id 分配给 $_SESSION['id']
.
单击选择框时,我有一个可以刷新主要内容的ajax请求(content_main.php
)。我还希望在right_sec_home.php
的另一个页面中选择的 id 以刷新其内容。
如何在 JS 文件中为第 2 页分配 php 会话 ID?
我的 JS 文件是
function get_request_by_command (commandId) {
var cmdTitle=$('#cmb_command :selected').text();
if(cmdTitle=="") {
cmdTitle='ABC';
}
$("#main_wrap").hide();
if(cmdTitle=='--Select--'){
$("#cmdTitle").html("");
}else{
$("#cmdTitle").html(cmdTitle);
}
$("#cmbs_cmd").val(commandId);
document.getElementById('request_list').innerHTML='<img src="images/loader.gif" />';
var strURL="request_by_command_tbl.php?id="+commandId;
var req = getXMLHTTP();
if (req) {
req.onreadystatechange = function() {
if (req.readyState == 4) {
// only if "OK"
if (req.status == 200) {
var ret = req.responseText;
$("#requestTitle").html("<span id='cmdTitle'>"+cmdTitle+"</span>);
$('#request_list').html('');
$('#request_list').html(ret);
$('#main').show();
$("#searchDiv").show();
} else {
alert("There was a problem while using XMLHTTP:'n" + req.statusText);
}
}
}
req.open("GET", strURL, true);
req.send(null);
}
}
通过使用这个jQuery函数,我创建了一个ajax请求,并将id发送到"request_by_command_tbl.php"
文件。
在我分配"request_by_command_tbl.php"
文件中,
$_SESSION['id'] = $_REQUEST['id'];
我也希望这个$_SESSION['id']
在同一时刻right_sec_home.php
。在发送 ajax 请求之前,他们在 jQuery 脚本文件中分配 php $_SESSION['id']
的方法也是如此。
我的其他文件
"content_main.php"
<div id="request_list"> </div>
<div> </div>
<div id="add_space"></div>
我需要会话ID的右部分主页文件
"right_sec_home.php"
<?php
function getRequestByMonth($month, $year, $id){
$que ="SELECT distinct(MS.date) FROM commands AS MC , ranks AS MR ,steady_tours AS MST, preferred_tours AS MPT, registration AS MMS where date_format(rm_date, '%c-%Y') = '".$month."-".$year."'
AND MMS.cmd_id ='".$_SESSION['id']."'
order by MMS.date";
$res = mysql_query($que);
while($rows[]=mysql_fetch_array($res)){}
foreach ($rows as $res):
if($res['date'] == "") continue;
$dt = date("Y-n-j", strtotime($res['date']));
$return[getDateB($dt)] = $res['date'];
endforeach;
return $return;
}
我希望这一点足够清楚。有什么想法吗?
请帮忙。
您无法
使用 jquery 访问会话信息。
解释
会话是存储在服务器上的文件-->其中Java脚本只是客户端语言..
总有一个解决方法..但我想你应该更多地解释你到底想要实现什么
<?php $sessionId = session_id(); ?>
<input id="session_id" name="session_id" type="hidden" value="<?php echo $sessionId; ?>" />
使用 jquery 获取隐藏字段的值。
您可以在 php 文件中使用一些隐藏字段或脚本变量。
例:
<script>
var id = <?php echo $_SESSION['id']; ?>;
</script>
可以使用 JavaScript 或 jQuery 访问变量 ID