1

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.

1
  • 1
    It may not help answer your question, but you should stop using mysql_* functions. They're being deprecated. Instead use PDO (supported as of PHP 5.1) or mysqli (supported as of PHP 4.1). If you're not sure which one to use, read this article. Commented Aug 22, 2012 at 15:51

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

2

Assuming PHP is generating the page you've got this "must be disabled" form on, there's no need for an ajax call - PHP can output a JS variable when it builds the page, e.g.

<script type="text/javascsript">
var disableForm = <?php echo (($_SESSION['somevar'] == 'whatever' ? 'true' : 'false') ?>;
if (disableForm) {
   ...
}
</script>

1 Comment

Beat me to the punch. Yeah, you can simply inject PHP code in a JS block.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.