2010-08-20 71 views
3

我试图从HTML表单上的选择输入控件传递一个值。 当我硬编码它时,它会得到回应,如果没有,我得到的是: 发明类型没有正确地通过。SESSION变量值未通过

这里是我的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."; 
} 

?> 

我在做什么错?

谢谢!

回答

3

应该

$_SESSION['invtype'] = $_POST['invtype']; 
+0

曾经有一段时间'$ _POST ['var']'会自动导出到'$ var' – NullUserException 2010-08-20 22:16:08

+1

是的,他们停止让默认选项打开,因为它成为最臭名昭着的安全威胁之一在整个互联网上。 – Teekin 2010-08-20 22:27:43

+0

谢谢!有效! – vlevsha 2010-08-20 22:47:33

0

你错过了 “SESSION_ID();”正好在“session_start();”的下面。我不知道为什么它是必需的,但如果我没有记错的话,就是这样。

+0

这没有必要 – NullUserException 2010-08-20 22:14:44