Jump to content

pass input to variable


Go to solution Solved by Barand,

Recommended Posts

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 by Maq
Link to comment
https://forums.phpfreaks.com/topic/284984-pass-input-to-variable/
Share on other sites

 

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 
}

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?

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 by enjiel
  • Solution

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.