I'm trying to pass a value from a select input control on an HTML form.
Here is the PHP of page1.php:

session_start(); 
$_SESSION['invtype'] = $invtype;
header("location: offerform_switch.php");

Here is the HTML:

    <select  id="invtype" name="invtype">
        <option value="0" selected="selected">Select type</option>
        <option value="product">PRODUCT</option>
        <option value="software">SOFTWARE</option>
    </select>

This is offerform_switch.php:

session_start(); 
echo $_SESSION['invtype'];

switch ($invtype){
    case "product":
        include("page2_product.php");
        break;
    case "software":
        include("page2_software.php");
        break;
    default:
       echo "The invention type did not go through correctly."; 
}

I get this:

The invention type did not go through correctly.

all the time.

Although, if I hardcode the SESSION variable value in page1.php, like this:

$_SESSION['invtype'] = 'product';

I get this:

productThe invention type did not go through correctly.
all the time.

which means that the value does pass from one page to the other, right?
Can't figure out what I'm doing wrong.

Why are you trying to pass the value as a SESSION variable ?? would'nt it be wise to use a form/POST-GET ?!
(btw, the snippets are still not clear enought to debug ... I suggest you post ~all the code)

i hope this helps. :icon_wink:

<!-- try this in page1.php -->
	
<?php

	session_start(); 
	$_SESSION['invtype'] = $invtype;
	
?>	

	<form method = "post">
		<select  id="invtype" name="invtype">
			<option value="0" selected="selected">Select type</option>
			<option value="product">PRODUCT</option>
			<option value="software">SOFTWARE</option>
		</select>
	</form>
	
<?php

	$invtype = $_POST['invtype'];

?>

	<!-- try this in offerform_switch.php -->
	
	<?php
	
		session_start(); 
		echo $_SESSION['invtype'];
		
		$invtype = $_SESSION['invtype'];
		  
		switch ($invtype){
			case "product":
				include("page2_product.php");
				break;
			case "software":
				include("page2_software.php");
				break;
			default:
			   echo "The invention type did not go through correctly.";	
		}
	
	?>

Alas. Still doesn't work. :-/
What do we gain using $_POST anyway?
In fact I experimented with it myself earlier, and it didn't work for me.

tsk. :-/

maybe your php.ini have a problem with your sessions.
check if the session is active in your php.ini .

looking forward :
what do you get from echo $_SESSION; anyway?

Where's the "code" I asked for ? :D

Where's the "code" I asked for ? :D

yeah. it's better to show the summary of your codes if you don't mind. :D

tsk. :-/

maybe your php.ini have a problem with your sessions.
check if the session is active in your php.ini .

looking forward :
what do you get from echo $_SESSION; anyway?

When I hardcode the value, I get it on the screen, when not, nothing gets output.

page1.php:

<?php
session_start(); 

$_SESSION['invtype'] = $invtype;

$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];

if (isset($_POST['Submit'])) {

        if ($_POST['firstname'] != "") {
            $_POST['firstname'] = filter_var($_POST['firstname'], FILTER_SANITIZE_STRING);
            if ($_POST['firstname'] == "") {
                $errors .= 'Please enter a valid first name.<br/><br/>';
            }
        } else {
            $errors .= 'Please enter your first name.<br/>';
        }
		
		if ($_POST['lastname'] != "") {
            $_POST['lastname'] = filter_var($_POST['lastname'], FILTER_SANITIZE_STRING);
            if ($_POST['lastname'] == "") {
                $errors .= 'Please enter a valid last name.<br/><br/>';
            }
        } else {
            $errors .= 'Please enter your last name.<br/>';
        }
		
    if (!$errors) {header("location: offerform_switch.php");
       }


		else {
            echo '<div style="color: red">' . $errors . '<br/>
				</div>';
			
				
			
        }
    }

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
	<head>
		<title>Offer Form, Part 1</title>
		<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
		<link rel="stylesheet" href="inventron_sage_short.css" type="text/css" />
		<link rel="stylesheet" href="form.css" type="text/css" />
		
	</head>
	<body>
		<div id = "logo">
			<img src = "img/top.jpg" alt = "logo" />
		</div>	
		<div id = "wrapper">
			
			

			
			<div id="stylized" class="myform">
<form id="form" action="page1.php" method="post">



	<p>
		<label for="firstname">FIRST NAME*:
		</label>
		<input type="text" name="firstname" id="firstname" value="<?php echo $firstname?>" />
	</p>	

	<p>
		<label for="lastname">LAST NAME*:
		</label>
		<input type="text" name="lastname" id="lastname" value="<?php echo $lastname?>" />
	</p>	

	<div id = "category">Categorize your invention:</div>
	
<div class="spacer"></div>
	
	<p>
		<select  id="invtype" name="invtype">
			<option value="0" selected="selected">Select type</option>
			<option value="product">PRODUCT</option>
			<option value="software">SOFTWARE</option>
			
		</select>
		<input type="submit" name="Submit" value="Next!" />

	</div>
      </div>
	</body>
</html>

offerform_switch.php:

<?php

session_start(); 
// echo variable from the session, we set this on our other page 
echo $_SESSION['invtype'];
$invtype = $_SESSION['invtype'];

//connect to your database ** EDIT REQUIRED HERE **
mysql_connect("mysql.myserver.com","myuser","mypassword"); //(host, username, password)

//specify database ** EDIT REQUIRED HERE **
mysql_select_db("invention") or die("Unable to select database"); //select which database we're using


switch ($invtype){
	case "product":
		include("page2_product.php");
		break;
	case "software":
		include("page2_software.php");
		break;
	default:
       echo "The invention type did not go through correctly.";	
}

?>

Where's the "code" I asked for ? :D

I'm sorry - you comments didn't make it to my mailbox. It's strange - comments seem to be coming to the mailbox randomly.
Anyway, here is the fuller code of the two scripts:

page1.php:

<?php
session_start(); 

$_SESSION['invtype'] = $invtype;

$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];

if (isset($_POST['Submit'])) {

        if ($_POST['firstname'] != "") {
            $_POST['firstname'] = filter_var($_POST['firstname'], FILTER_SANITIZE_STRING);
            if ($_POST['firstname'] == "") {
                $errors .= 'Please enter a valid first name.<br/><br/>';
            }
        } else {
            $errors .= 'Please enter your first name.<br/>';
        }
		
		if ($_POST['lastname'] != "") {
            $_POST['lastname'] = filter_var($_POST['lastname'], FILTER_SANITIZE_STRING);
            if ($_POST['lastname'] == "") {
                $errors .= 'Please enter a valid last name.<br/><br/>';
            }
        } else {
            $errors .= 'Please enter your last name.<br/>';
        }
		
    if (!$errors) {header("location: offerform_switch.php");
       }


		else {
            echo '<div style="color: red">' . $errors . '<br/>
				</div>';
			
				
			
        }
    }

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
	<head>
		<title>Offer Form, Part 1</title>
		<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
		<link rel="stylesheet" href="inventron_sage_short.css" type="text/css" />
		<link rel="stylesheet" href="form.css" type="text/css" />
		
	</head>
	<body>
		<div id = "logo">
			<img src = "img/top.jpg" alt = "logo" />
		</div>	
		<div id = "wrapper">
			
			

			
			<div id="stylized" class="myform">
<form id="form" action="page1.php" method="post">



	<p>
		<label for="firstname">FIRST NAME*:
		</label>
		<input type="text" name="firstname" id="firstname" value="<?php echo $firstname?>" />
	</p>	

	<p>
		<label for="lastname">LAST NAME*:
		</label>
		<input type="text" name="lastname" id="lastname" value="<?php echo $lastname?>" />
	</p>	

	<div id = "category">Categorize your invention:</div>
	
<div class="spacer"></div>
	
	<p>
		<select  id="invtype" name="invtype">
			<option value="0" selected="selected">Select type</option>
			<option value="product">PRODUCT</option>
			<option value="software">SOFTWARE</option>
			
		</select>
		<input type="submit" name="Submit" value="Next!" />

	</div>
      </div>
	</body>
</html>

offerform_switch.php:

<?php

session_start(); 
// echo variable from the session, we set this on our other page 
echo $_SESSION['invtype'];
$invtype = $_SESSION['invtype'];

//connect to your database ** EDIT REQUIRED HERE **
mysql_connect("mysql.myserver.com","myuser","mypassword"); //(host, username, password)

//specify database ** EDIT REQUIRED HERE **
mysql_select_db("invention") or die("Unable to select database"); //select which database we're using


switch ($invtype){
	case "product":
		include("page2_product.php");
		break;
	case "software":
		include("page2_software.php");
		break;
	default:
       echo "The invention type did not go through correctly.";	
}

?>

Why are you trying to pass the value as a SESSION variable ?? would'nt it be wise to use a form/POST-GET ?!
(btw, the snippets are still not clear enought to debug ... I suggest you post ~all the code)

Yes, and here is why I try using SESSION instead of form's POST:
My form is divided into two pages, and I need to collect data from both and put it into a database.
In the past I just used to insert data from the first page, get a unique row identifier, and based on it, update the row in the database with data from the second page.
I just think that using SESSION would more sufficient, no?
Anyway, if I don't succeed in this, I'll go back to my old good method. :)

I found the solution!
It's:
$_SESSION = $_POST;

:idea:

I got the solution:

$_SESSION = $_POST;

:idea:

I got the solution:

$_SESSION = $_POST;

:idea:

good for you. Ϋ

don't forget to mark this thread as solved.
happy day!

good for you. Ϋ

don't forget to mark this thread as solved.
happy day!

Thank you!
I already did. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.