enjiel Posted December 30, 2013 Share Posted December 30, 2013 (edited) I know this topic has been hit, I've read answers to similar issues between this site and stackoverflow but for some reason it doesn't seem to want to work and I feel like I'm missing a very small part of the puzzle. The error I'm getting is that the variable "pass" is not found. index.php <?php $lifetime = 86400; session_set_cookie_params($lifetime, '/'); session_start(); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Web Survey</title> <link href="styles.css" rel="stylesheet"></link> <style type="text/css"> body {padding-top: 115px;} .main p {margin-left: 45px;} h3 {margin-left:45px;} </style> </head> <body> <h1>Appendix D:</h1> <h1>Text</h1> <div class="main"> <p class="intro">Text</p> <form action ="images.php" method="post"> <h3><input type="submit" name="Submit" value="Begin" class="button"/> Code: <input type="text" name="pass" size="2"/><br /> <?php if (isset($_POST['Submit'])) { $_SESSION['pass'] = $_POST['pass']; } ?> </h3> </form> </div> </body> </html> images.php <?php session_start(); if ($_SESSION['pass'] == 00001) { $_SESSION['version'] = "version1"; } else if ($_SESSION['pass'] == 00002) { $_SESSION['version'] = "version2"; } else if ($_SESSION['pass'] == 00003) { $_SESSION['version'] = "version3";} $version = $_SESSION['version']; $picture1 = "images/".$version."/1.jpg"; $picture2 = "images/".$version."/2.jpg"; $picture3 = "images/".$version."/3.jpg"; $picture4 = "images/".$version."/4.jpg"; ?> <head> <title>Text</title> <link href="styles.css" rel="stylesheet"> </head> <body> <center> <img src="" name="Rotating" id="Rotating" width=800 height=700> </center> <script language="JavaScript"> var ImageArr1 = new Array("<?php echo $picture1; ?>","<?php echo $picture2; ?>","<?php echo $picture3; ?>","<?php echo $picture4; ?>"); var ImageHolder1 = document.getElementById('Rotating'); function RotateImages(whichHolder,Start) { var a = eval("ImageArr"+whichHolder); var b = eval("ImageHolder"+whichHolder); if(Start>=a.length) Start=0; b.src = a[Start]; window.setTimeout("RotateImages("+whichHolder+","+(Start+1)+")",2500); } RotateImages(1,0); setTimeout(function(){window.location.href='form1.php'},10000); </script> </body> </html> I'm still learning php, I went through the Mike Murach php book, it's pretty good but doesn't really seem to help that much in this area. It seems that when I post the index.php form, it should store the variable in the session using if (isset($_POST['Submit'])) { $_SESSION['pass'] = $_POST['pass']; so that I can call it in images.php. For some reason, though, 'pass' doesn't seem to be stored at all. What step am I missing? Edited December 30, 2013 by Maq Quote Link to comment Share on other sites More sharing options...
Maq Posted December 30, 2013 Share Posted December 30, 2013 Please use the code tags around your code in the future. Quote Link to comment Share on other sites More sharing options...
taquitosensei Posted December 30, 2013 Share Posted December 30, 2013 I know this topic has been hit, I've read answers to similar issues between this site and stackoverflow but for some reason it doesn't seem to want to work and I feel like I'm missing a very small part of the puzzle. The error I'm getting is that the variable "pass" is not found. index.php <?php $lifetime = 86400; session_set_cookie_params($lifetime, '/'); session_start(); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Web Survey</title> <link href="styles.css" rel="stylesheet"></link> <style type="text/css"> body {padding-top: 115px;} .main p {margin-left: 45px;} h3 {margin-left:45px;} </style> </head> <body> <h1>Appendix D:</h1> <h1>Text</h1> <div class="main"> <p class="intro">Text</p> <form action ="images.php" method="post"> <h3><input type="submit" name="Submit" value="Begin" class="button"/> Code: <input type="text" name="pass" size="2"/><br /> <?php if (isset($_POST['Submit'])) { $_SESSION['pass'] = $_POST['pass']; } ?> </h3> </form> </div> </body> </html> images.php <?php session_start(); if ($_SESSION['pass'] == 00001) { $_SESSION['version'] = "version1"; } else if ($_SESSION['pass'] == 00002) { $_SESSION['version'] = "version2"; } else if ($_SESSION['pass'] == 00003) { $_SESSION['version'] = "version3";} $version = $_SESSION['version']; $picture1 = "images/".$version."/1.jpg"; $picture2 = "images/".$version."/2.jpg"; $picture3 = "images/".$version."/3.jpg"; $picture4 = "images/".$version."/4.jpg"; ?> <head> <title>Text</title> <link href="styles.css" rel="stylesheet"> </head> <body> <center> <img src="" name="Rotating" id="Rotating" width=800 height=700> </center> <script language="JavaScript"> var ImageArr1 = new Array("<?php echo $picture1; ?>","<?php echo $picture2; ?>","<?php echo $picture3; ?>","<?php echo $picture4; ?>"); var ImageHolder1 = document.getElementById('Rotating'); function RotateImages(whichHolder,Start) { var a = eval("ImageArr"+whichHolder); var b = eval("ImageHolder"+whichHolder); if(Start>=a.length) Start=0; b.src = a[Start]; window.setTimeout("RotateImages("+whichHolder+","+(Start+1)+")",2500); } RotateImages(1,0); setTimeout(function(){window.location.href='form1.php'},10000); </script> </body> </html> I'm still learning php, I went through the Mike Murach php book, it's pretty good but doesn't really seem to help that much in this area. It seems that when I post the index.php form, it should store the variable in the session using if (isset($_POST['Submit'])) { $_SESSION['pass'] = $_POST['pass']; so that I can call it in images.php. For some reason, though, 'pass' doesn't seem to be stored at all. What step am I missing? It won't exist the first time you go to the page. As soon as you submit your form it will. So you'll need to check that your for has been submitted before trying to process $_POST['pass']. Something like if(isset($_POST['yourfieldname'])) { // do your processing here } Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted December 30, 2013 Share Posted December 30, 2013 also, any 2nd though last parameters of the session_set_cookie_params() statement need to be identical for all calls to that statement to insure you are starting/resuming the same session. Quote Link to comment Share on other sites More sharing options...
enjiel Posted December 31, 2013 Author Share Posted December 31, 2013 Thanks Maq; sorry I'm a first time poster here. I can usually find the answers somewhere so I've never needed to post. Also, thanks for the response taquitosensei. I tried moving the process to index.php and run it all together before it moves to the next page. <form action ="images.php" method="post"> <h3><input type="submit" name="Submit" value="Begin" class="button"/> Code: <input type="text" name="pass" size="2"/><br /> <?php if (isset($_POST['Submit'])) { $_SESSION['pass'] = $_POST['pass']; if ($_SESSION['pass'] == 00001) { $_SESSION['version'] = "version1"; } else if ($_SESSION['pass'] == 00002) { $_SESSION['version'] = "version2"; } else if ($_SESSION['pass'] == 00003) { $_SESSION['version'] = "version3";} } ?></h3></form> I think I'm misunderstanding something. You said to make sure that I'm not trying to use 'pass' until after the form is submitted; but it seems like I'm doing that. if (isset($_POST['Submit'])) { $_SESSION['pass'] = $_POST['pass']; ...ect... } The sessions seem to be working fine; if I declare 'pass' at the beginning of index.php, then it follows throughout the session. It's just not wanting to save as a variable when I try to declare it after posting the page. btw; did I do the tags right this time? Quote Link to comment Share on other sites More sharing options...
enjiel Posted December 31, 2013 Author Share Posted December 31, 2013 (edited) This code works: <?php$lifetime = 86400;session_set_cookie_params($lifetime, '/');session_start(); $_SESSION['pass'] = '00003';?><!DOCTYPE html><body><form action ="images.php" method="post"> <h3><input type="submit" name="Submit" value="Begin" class="button"/> Code: <input type="text" name="pass" size="2"/><br /> <?php if (isset($_POST['Submit'])) { $_SESSION['pass'] = $_POST['pass']; } if ($_SESSION['pass'] == 00001) { $_SESSION['version'] = "version1"; } else if ($_SESSION['pass'] == 00002) { $_SESSION['version'] = "version2"; } else if ($_SESSION['pass'] == 00003) { $_SESSION['version'] = "version3";} ?></form></body></html> But that is giving the variable a static value. If I try to replace $_SESSION['pass'] = '00003'; with if (isset($_POST['Submit'])) { $_SESSION['pass'] = $_POST['pass']; } then it breaks it. Edited December 31, 2013 by enjiel Quote Link to comment Share on other sites More sharing options...
Solution Barand Posted December 31, 2013 Solution Share Posted December 31, 2013 When you display the form the $_POST data does not yet exist so the code is not executed. When you submit the form the post data is sent to images.php (because that is the specified form action). So the code still isn't executed because it isn't in that file. Quote Link to comment Share on other sites More sharing options...
enjiel Posted December 31, 2013 Author Share Posted December 31, 2013 Okay. I understand now. Thanks guys. Moving the entire statement to images.php worked. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.