duankangzi766767 2012-08-22 15:50
浏览 58
已采纳

使用Jquery在页面加载上测试php会话变量

I have a session variable that is set when the user submits a form with a certain option selected. When the page refreshes I need to test this session variable and if it exists then make some of the form read only. This is my code so far, php:

<?php
require("header.php");

if(isset($_REQUEST['searching'])){ //check if form has been submitted

echo"<h2>Submitted</H2><p>";
    connect('final');//connect to DB

  $fName = $_POST['addFname']; 
  $lName = $_POST['addLname']; 
  $address = $_POST['address']

  $dropdown = $_POST['field']; // yes or no option 

    $fName = htmlspecialchars($fName); 
    $fName = mysql_real_escape_string($fName); 
    $lName = htmlspecialchars($lName); 
    $lName = mysql_real_escape_string($lName);
    $address = htmlspecialchars($line2); // stop HTML characters
    $address = mysql_real_escape_string($line2); //stop SQL injection


if($dropdown== "no"){
  $_SESSION['name'] = "$fName";    
}     
?>

'field' is the name of my dropdown with 2 options yes and no.

MY JS for getting the variable:

<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
<script type = "text/javascript">
var session;
function multi(){
    $.ajaxSetup({cache: false})
    $.get('getSession.php', {requested: 'foo'}, function (data) {
        session = data;
});
}
</script>

I use that to get the variable from the session, getSession.php has the following:

    <?php
session_start();
if (isset($_GET['name'])) {
    // return requested value
    print $_SESSION[$_GET['name']];
} else {
    print json_encode($_SESSION);
}
?>

finally I have this function to disable the text fields

    <script language="JavaScript">
<!--
function enable_text2()
{
if (session !=""){
status = "true";
document.add.addFname.readonly=readonly;
document.add.addLname.readonly=readonly;
}
}
//-->
</script>

the rest of my html is just a form, this is all in one document with the php code at the top, and the javascript functions in the head tag.

I call a wrapper function in the body onload tag, which then calls both of those functions, I thought the first function would get the session variable if it existed from the php document and then the second function would test if it was not empty, if it wasn't then it would make the fields read only.

However when I select no in the drop down and submit the form, the page refreshes and nothing happens, the fields are not read only.

  • 写回答

2条回答 默认 最新

  • duanpoqiu0919 2012-08-22 16:05
    关注

    Why You are using javascript for it??

    session_start();
    if (isset($_GET['name'])) {
        $disable=true;
    } else {
        $disable=false;
    }
    
    
    
     <input type="text" name="addFname" <?php if($disable) { ?> readonly="readonly" <?php } ?>
    

    Here i have taken "addFname" .you can disable any element inside that php if condition

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。
  • ¥20 CST怎么把天线放在座椅环境中并仿真
  • ¥15 任务A:大数据平台搭建(容器环境)怎么做呢?
  • ¥15 YOLOv8obb获取边框坐标时报错AttributeError: 'NoneType' object has no attribute 'xywhr'
  • ¥15 r语言神经网络自变量重要性分析
  • ¥15 基于双目测规则物体尺寸
  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?