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 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误