HTML表单中的PHP会话输入

时间:2018-10-21 00:27:50

标签: php html arrays forms session-variables

我正在尝试将html表单中的输入放入php Session数组中?很难理解Sessions和PHP。我需要一个HTML页面来接受输入,然后使用php会话和数组进行计算和显示。请帮忙!谢谢

--------这是HTML页面-----

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>Project 1</title>
        <meta charset="utf-8">
        <link href="../../Styles/Project1.css" rel="stylesheet" media="screen">
    </head>
    <body>
        <div id="wrapper">
            <div id="header">
                <h1>Census Bureau</h1>
            </div>
            <div id="nav">
                <ul>
                    <li><a href="Project1Index.html">Home</a></li>
                    <li><a href="TotalHouseholdsSurveyed.php">Total Households Surveyed</a></li>
                    <li><a href="AVGHouseholdIncome.php">Average Household Income</a></li>
                    <li><a href="PercentageBelowPoverty.php">Percentage Below Poverty</a></li>
                </ul>
            </div>
            <div id="content"> 
                <table border="1" style="xborder: solid 1px black;" >
<form action = "Project1.php" name= "frmProject1" action="id" method="post">

       <tr>
 <td><b><label for="dtmDate">Date of Survey</b> </td> </label>
 <td><input type="date" name="FirstName" id="FirstName" required> </td>
       </tr>

  <tr>
 <td><label for="CountyState"><b>County and State: </b></td></label>
 <td><select name="CountyState" required>
<option value ="HamiltonOH">Hamilton, OH</option>
<option value="ButlerOH">Butler, OH</option>
<option value="BooneKY">Boone, KY</option>
<option value="KentonKY">Kenton, KY</option>
</select>
 </td>
       </tr>

<tr>
 <td><b><label for="txtNumberInHousehold">Number in household:*</b> </td></label>
 <td><input type="number" name="intNumberInHousehold" id="txtNumberInHousehold" required> </td>
       </tr>

<tr>
 <td><b><label for="txtYearlyIncome">Yearly Household Income:*</b> </td></label>
 <td><input type="number" name="dblHouseholdIncome" id="txtYearlyIncome" required> </td>
       </tr>


<td colspan="2" align="center"><input type="submit" value="Submit"> <input type="reset" value="Clear"> </td>
</table>
</form>

    </body>
</html>

-------这是PHP ----------

<html>
<body>

<?php
session_start();        

$_SESSION['User']=array();
$dtmDate=$_POST['dtmDate']; 
$ddlCounty_State=$_POST['ddlCounty_State'];
$intHouseholdNumber=$_POST['intHouseholdNumber']; 
$dblHouseholdIncome=$_POST['dblHouseholdIncome'];  
array_push($_SESSION['User'],$dtmDate,$ddlCounty_State,$intHouseholdNumber,$dblHouseholdIncome);   


?>
Date: <?php echo $_SESSION['dtmDate'];?><br>
Region: <?php echo $_SESSION['ddlCounty_State'];?><br>
Household:<?php echo $_SESSION['intHouseholdNumber'];?><br>
Income:<?php echo $_SESSION['dblHouseholdIncome'];?><br>



</body>
</html> 

1 个答案:

答案 0 :(得分:0)

您创建了一个数组并将其array_push()放入其中,但是您没有在输出中引用它。

Date: <?php echo $_SESSION['User']['dtmDate'];?><br>
Region: <?php echo $_SESSION['User']['ddlCounty_State'];?><br>
Household:<?php echo $_SESSION['User']['intHouseholdNumber'];?><br>
Income:<?php echo $_SESSION['User']['dblHouseholdIncome'];?><br>

请注意多余的['User']来创建的数组并将数据放入其中。

在HTML文件上选择的郡县与在PHP端用于输入的名称之间,您还会遇到名称不匹配的情况。