tags:

views:

117

answers:

3

im new to sessions but from what i see it complicated to apply <input> with them. can you please look at this code and tell me why its not working. i had it working earlier then it died on me. the function of the program is to fill out a form and have it verified for legit information using regular expressions, i just need help with getting the sessions to save the data.

<?php session_start(); ?> 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
<html>
<head>
<title>regex</title>


</head>

<body>

<?php

$fname = $_REQUEST['fname'];

$fname = $_SESSION['fname'];


print<<<form

<form method="post"  action="">

  <input type ="text"
             name="fname"
       value="">

    <input type ="submit">

</form>

form;

$_SESSION['fname'] = $fname;

 print $_SESSION['fname'];


?>

</body>
</html>
+2  A: 

You are reading $fname from $_REQUEST, then overwriting it with the value from $_SESSION, then putting it back to $_SESSION. So far, it should work as designed :) What are you trying to do? If you want to set the $_SESSION variable with the value received through $_REQUEST, leave out the second "$fname=" line.

Pekka
A: 

Try this:

if (!isset($_SESSION['fname'])) {
    $_SESSION['fname'] = '';  // default value
}
if (isset($_POST['fname'])) {
    $_SESSION['fname'] = $_POST['fname'];
}

print<<<form

…

form;

print $_SESSION['fname'];
Gumbo
+1  A: 

Timothy, Change your code so that it checks if the session/request is empty or not

Something like:

if(isset($_REQUEST['fname'])){
  $fname = $_REQUEST['fname'];
}else if(isset($_SESSION['fname'])){
  $fname = $_SESSION['fname'];
}
TeckniX
thanks tons dood this worked perfectly. i appreciate it. :)
Timothy
If it worked perfectly, please mark it as an answer
pǝlɐɥʞ
sorry it took so long the person that referred me to this site just showed me how to mark.
Timothy
Not a problem - I appreciate it!
TeckniX