PHP表单:获取结果后,在输入新信息时添加新表行

时间:2010-04-30 11:59:59

标签: php

虽然很简单,但我似乎无法找到以下内容 表单采用某些数据,然后表示表中的数据。

下一步,我点击将我带回表格的超链接 现在我的问题是,当再次填写相同的表格时,我是如何才能实现这一目标的,因此两个结果都显示在同一个表格中? 然后用数据填写另一个表格会添加另一行,依此类推。

问候。

下面的代码(原谅我不是英文)。

<?php
session_start();
?>

<!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" xml:lang="en" lang="en">

<head>
<title>ExpoFormulier</title>

    

<body>

<?php
if (!empty($_POST))
{
$standnaam = $_POST["standnaam"];
$oppervlakte = $_POST["oppervlakte"];
//value in the form van checkboxes op 1 zetten!
$verdieping = isset($_POST["verdieping"]) ? $_POST["verdieping"] : 0;  //if checkbox    checked value 1 anders 0
$telefoon = isset($_POST["telefoon"]) ? $_POST["telefoon"] : 0;  
$netwerk = isset($_POST["netwerk"]) ? $_POST["netwerk"] : 0; 


if (is_numeric($oppervlakte)) 
{
    $_SESSION["standnaam"]=$standnaam;
    $_SESSION["oppervlakte"]=$oppervlakte;
    $_SESSION["verdieping"]=$verdieping;
    $_SESSION["telefoon"]=$telefoon;
    $_SESSION["netwerk"]=$netwerk;
    header("Location:ExpoOverzicht.php"); //verzenden naar ExpoOverzicht.php dmv header
}
else 
{
    echo "<h1>Foute gegevens, Opnieuw invullen a.u.b</h1>";
}  
}

?>

<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post" id="form1">
<h1>Vul de gegevens in</h1>
<table>
    <tr>
        <td>Standnaam:</td>
        <td><input type="text" name="standnaam" size="18"/></td>
    </tr>
    <tr>
        <td>Oppervlakte (in m^2):</td>
        <td><input type="text" name="oppervlakte" size="6"/></td>
    </tr>
    <tr>
        <td>Verdieping:</td>
        <td><input type="checkbox" name="verdieping" value="1"/></td>
        <!--value op 1 zetten voor checkbox! indien checked is value 1 -->
    </tr>
    <tr>
        <td>Telefoon:</td>
        <td><input type="checkbox" name="telefoon" value="1"/></td>
    </tr>
    <tr>
        <td>Netwerk:</td>
        <td><input type="checkbox" name="netwerk" value="1"/></td>
    </tr>
    <tr>
        <td><input type="submit" name="verzenden" value="Verzenden"/></td>
    </tr>
</table>

</form>

第二档:

<?php
session_start();
?>

<!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" xml:lang="en" lang="en">

<head>
    <title>ExpoOverzicht</title>
 <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <link href="StyleSheetExpo.css" rel="stylesheet" type="text/css" />  
</head>

<body>

<h1>Overzicht van de ingegeven standen in deze sessie</h1>

<?php

$standnaam = $_SESSION["standnaam"];
$oppervlakte = $_SESSION["oppervlakte"];
$verdieping = $_SESSION["verdieping"];
$telefoon = $_SESSION["telefoon"];
$netwerk = $_SESSION["netwerk"];

$result1 = 0; //telkens declaren anders fout "undefined variable"
$result2 = 0;
$result3 = 0;
$prijsCom = 0;
$prijsVerdieping = 0;

for ($i=1; $i <= $oppervlakte; $i++)
{   
    if($i <= 10)
    {
       $tarief1 = 1 * 100;
       $result1 += $tarief1;
    }

    if($i > 10 && $i <= 30)
    {
        $tarief2 = 1 * 90;
        $result2 += $tarief2;      
    }

    if($i > 30)
    {
        $tarief3 = 1 * 80;
        $result3 += $tarief3;
    }  
}
$prijsOpp = $result1 + $result2 + $result3;


if($verdieping == 1)
{
    $prijsVerdieping = $oppervlakte * 120;
}


if(($telefoon == 1) || ($netwerk == 1)) // eerst deze OR conditie  of anders gebruikt de code alleen nog maar 20
{
    $prijsCom = 20;
}

if(($telefoon == 1) && ($netwerk == 1))
{
    $prijsCom = 30;
}


$totalePrijs = $prijsOpp + $prijsVerdieping + $prijsCom; 

echo "<table class=\"tableExpo\">";

    echo "<th>Standnaam</th>";
    echo "<th>Oppervlakte</th>";
    echo "<th>Verdieping</th>";
    echo "<th>Telefoon</th>";
    echo "<th>Netwerk</th>";
    echo "<th>Totale prijs</th>";

        echo "<tr>";     
            echo "<td>".$standnaam."</td>";
            echo "<td>".$oppervlakte."</td>";
            echo "<td>".$verdieping."</td>";
            echo "<td>".$telefoon."</td>";
            echo "<td>".$netwerk."</td>";
            echo "<td>".$totalePrijs."</td>";
        echo "</tr>";

echo "</table>";

?>

<a href="ExpoFormulier.php">Terug naar het formulier</a>

</body>
</html>

</body>
</html>

1 个答案:

答案 0 :(得分:2)

您的会话变量只会存储输入的最后一段数据。不要将它们作为简单变量存储在会话中,而是尝试在会话中放入一个数组。然后,当再次提交表单时,向这些数组添加另一个条目。这样你就不会覆盖之前的那些。

ETA:

在第1页上,您设置会话变量,请尝试以下方法:

$_SESSION["standnaam"][]=$standnaam;
$_SESSION["oppervlakte"][]=$oppervlakte;
$_SESSION["verdieping"][]=$verdieping;
$_SESSION["telefoon"][]=$telefoon;
$_SESSION["netwerk"][]=$netwerk;

在第2页上,您写出表行,请尝试:

for($i = 0; $i < count($standnaam); $i++) {
    echo "<tr>";     
        echo "<td>".$standnaam[$i]."</td>";
        echo "<td>".$oppervlakte[$i]."</td>";
        echo "<td>".$verdieping[$i]."</td>";
        echo "<td>".$telefoon[$i]."</td>";
        echo "<td>".$netwerk[$i]."</td>";
        echo "<td>".$totalePrijs[$i]."</td>";
    echo "</tr>";
}

我认为应该这样做。