多次提交后存储$ _SESSION变量

时间:2014-07-07 14:17:51

标签: php forms session

我有一个包含多个提交的表单。首先是一个带有一些选项的下拉菜单。基于该选择,页面上的某些语言更改(降压而不是公牛......等)当选择菜单中的值时,将显示更多表单输入。用户填写剩余部分并提交该数据后,将其写入数据库。我遇到的问题是我正在尝试使用$ _SESSION存储第一个$ _POST值。

它将在第一篇文章之后存储它,但在第二篇之后它会被清除。非常令人沮丧。

<?
include_once('global_inc.php');//required for debugging
include_once('MySQL.php');//connects this form to the database
session_start();
?>
<html>
<body>
<form action="<?php print $_SERVER['PHP_SELF']?>" method="post" id="cervidSolutions">
<?
if (empty($_POST)) {
  echo "<h3>Please select the species of your farm.</h3>";
  echo "<select name='cervid'>";

  $list = array("WHITETAIL"=>"Whitetail", "RED_DEER"=>"Red Deer", "ELK"=>"Elk", "FALLOW_DEER"=>"Fallow Deer", "MULE_DEER"=>"Mule Deer");

  foreach ($list as $key=>$value) { //Creates a drop down from the above array
    echo "<option value='{$key}'";

    if ($_POST['cervid'] == $key) 
      echo " selected";

    echo ">{$value}</option>";
  }
  echo "</select>";
  echo "<input type='submit' name='speciesSelected' value='Select Species' />";
}

$_SESSION['cervid'] = $_POST['cervid'];

//Statements to assign noun values
if ($_POST['cervid'] == "WHITETAIL" || $_POST['cervid'] == "MULE_DEER" || $_POST['cervid'] == "FALLOW_DEER") {
  $maleNames = "Buck";
  $femaleNames = "Doe";
  $babyNames = "Fawn";
} else {
  $maleNames = "Bull";
  $femaleNames = "Hind";
  $babyNames = "Calf";
}

 /* Main form information including name, email, phone number and head count. */
if (!empty($_POST['cervid'])) { //These elements will only appear if $_POST['cervid'] has a value

echo "<p>Please type your name, email, and phone number. This information will help us provide you with any assistence you may need.</p>
<fieldset class='mainFormNamesFieldset'>
    <label for='firstName'>First Name</label>
    <input type='text' value='' name='firstName' class='mainFormNames' required />
</fieldset>
<fieldset class='mainFormNamesFieldset'>
    <label for='lastName'>Last Name</label>
    <input type='text' value='' name='lastName' class='mainFormNames' required />
</fieldset>
<fieldset class='mainFormEmailFieldset'>
    <label for='emailAddress'>Email Address</label>
    <input type='email' value='' name='emailAddress' class='mainFormEmail' required />
</fieldset>
<fieldset class='mainFormPhoneFieldset'>
    <label for='phoneNumber'>Phone Number</label>
    <div class='phoneWrap'>
    <input type='text' value='' maxlength='3' name='phoneNumber1' id='mainFormPhoneFirst' required /><span class='dash'>-</span>
    <input type='text' value='' maxlength='3' name='phoneNumber2' id='mainFormPhoneMiddle' required /><span class='dash'>-</span>
    <input type='text' value='' maxlength='4' name='phoneNumber3' id='mainFormPhoneLast' required />        
    </div>
    <div class='phoneWrap'>
    <span id='hashLeft'>###</span>
    <span id='hashMiddle'>###</span>
    <span id='hashRight'>####</span>
    </div>
</fieldset>
<fieldset class='mainFormHerdFieldset'>
    <label for='maleHeadCount'>Number of ". $maleNames ."</label>
    <input type='text' value='' maxlength='3' size='3' name='maleHeadCount' class='mainFormMale' />
</fieldset>
<fieldset class='mainFormHerdFieldset'>
    <label for='femaleHeadCount'>Number of ". $femaleNames ."</label>
    <input type='text' value='' maxlength='3' size='3' name='femaleHeadCount' class='mainFormFemale' />
</fieldset>
<fieldset class='mainFormHerdFieldset'>
    <label for='femaleHeadCount'>Number of ". $babyNames ."</label>
    <input type='text' value='' maxlength='3' size='3' name='babyHeadCount' class='mainFormBaby' />
</fieldset>
<input type='submit' name='custInfo' value='Submit Data' id='submitCustInfo' /><input type='reset' name='reset' value='Reset Form' id='resetForm' />";
}

这就是所有相关的代码。其余的只是一些变量计算和插入语句。我知道insert语句是正确的,因为如果我对一个值进行硬编码,它就会编写它。做一个print_r($ _ SESSION ['cervid']);在第一篇文章之后显示变量,但在第二篇文章之后清除它。

1 个答案:

答案 0 :(得分:0)

虽然您的方法不太理想,但我相信您可以通过更改此行来“修复”当前问题:

$_SESSION['cervid'] = $_POST['cervid'];

到此:

if(isset($_POST['cervid']))
  $_SESSION['cervid'] = $_POST['cervid'];