在传递由php变量组成的表单时遇到问题


Having trouble passing form that consists of php variables

所以我有两页多页表单用于测试。当我单独测试表单的第一部分时,它运行良好。所有的东西都会根据它从我的数据库中检索到的信息进行相应的显示。一切看起来都很好,直到我点击提交按钮进入下一页,确认所有值。当我尝试点击submit时,它也应该传递用户的输入并将其保存为$_POST[],这会给我一个严重的错误。我是php的新手,所以我不太熟悉我可能需要的一切。

这是我的第一部分代码:

<?php
    $db = mysql_connect(
  ':/Applications/MAMP/tmp/mysql/mysql.sock',
  'root',
  'root'
);
        if(!$db) die("Error connecting to MySQL database.");
        mysql_select_db('onlineform', $db);
        $newQuery = mysql_query("SELECT newPrice,numberOfWeeks FROM onlineformdata ORDER BY id DESC LIMIT 1");
$newRow = mysql_fetch_row($newQuery);

        $newQuery1 = mysql_query("SELECT newCampSessions FROM onlineformdata ORDER BY id DESC LIMIT 1") or die('Error ' . mysql_error());
$newFoo = mysql_fetch_array($newQuery1);

        $newQuery2 = mysql_query("SELECT pricePerWeek FROM onlineformdata ORDER BY id DESC LIMIT 1") or die('Error ' . mysql_error());
$newFoo1 = mysql_fetch_array($newQuery2);
        $newQuery3 = mysql_query("SELECT numberOfSchools FROM onlineformdata ORDER BY id DESC LIMIT 1");
$newRow1 = mysql_fetch_row($newQuery3);
        $newQuery4 = mysql_query("SELECT schools FROM onlineformdata ORDER BY id DESC LIMIT 1") or die('Error ' . mysql_error());
$newFoo2 = mysql_fetch_array($newQuery4);
$newOldString = $newFoo['newCampSessions'];
$newOldString2 = $newFoo1['pricePerWeek'];
$newOldString3 = $newFoo2['schools'];

$newChangedString = unserialize($newOldString);
$newChangedString2 = unserialize($newOldString2);
$newChangedString3 = unserialize($newOldString3);
$_SESSION['quickVariable'] = $newChangedString2;
settype($newRow[1], "integer");
$limit = $newRow[1];
$anotherLimit = $newRow1[0];
mysql_close();

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">
    <head>
<script src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.7.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.10/jquery-ui.js"></script>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
<link type="text/css" rel="stylesheet" href="stylesheet.css"/>
<link href='http://fonts.googleapis.com/css?family=Alef' rel='stylesheet' type='text/css'>
<link type="text/css" rel="stylesheet" href="style3.css"/>
<script type="text/javascript" src="JavaScript.js">
</script>
 <script type="text/javascript">var jslang='EN';
 </script>
        <link href="http://activemindandbody.orgStyleSheets/ModuleStyleSheets.css" rel="StyleSheet" type="text/css" />
        <script type="text/javascript">var jslang='EN';
        </script>
        <link href="http://activemindandbody.org/style.css" rel="stylesheet" type="text/css" />
        <script type="text/javascript" src="js/modernizr.custom.04022.js"></script>
        <link href='http://fonts.googleapis.com/css?family=Open+Sans+Condensed:700,300,300italic' rel='stylesheet' type='text/css'>
        <link href='http://fonts.googleapis.com/css?family=Alef' rel='stylesheet' type='text/css'>
    </head>
    <body>
        <!-- BC_OBNW -->
        <div id="container">
            <div id="header"><div id="header_text" style="top: 118px; left: 23px;">
                <p>Make a donation today and help&nbsp;&nbsp;âCreating a Brighter Future                      
    Together.â&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; <a href="http://activemindandbody.org/donation.html">Make a donation now !</a> </p>
        </div>
        </div>
        <div id="content">
                    <section class="tabs">
                <input id="tab-1" type="radio" name="radio-set" class="tab-selector-1" checked="checked" />
                <label for="tab-1" class="tab-label-1">1. Camper's Info</label>
                <input id="tab-2" type="radio" name="radio-set" class="tab-selector-2" />
                <label for="tab-2" class="tab-label-2">2. Parent's Info</label>
                <input id="tab-3" type="radio" name="radio-set" class="tab-selector-3" />
                <label for="tab-3" class="tab-label-3">3. Emergency<br/> Contacts</label>
                <input id="tab-4" type="radio" name="radio-set" class="tab-selector-4" />
                <label for="tab-4" class="tab-label-4">4. Camper's Ride Home</label>
                <input id="tab-5" type="radio" name="radio-set" class="tab-selector-5" />
                <label for="tab-5" class="tab-label-5">5. <br/> Camp Days & Prices</label>
                 <input id="tab-6" type="radio" name="radio-set" class="tab-selector-6" />
                <label for="tab-6" class="tab-label-6">6. Confirm your details</label>
                <input id="tab-7" type="radio" name="radio-set" class="tab-selector-7" />
                <label for="tab-7" class="tab-label-7">7. <br />Paypal</label>
                    </section>
                <div class="clear-shadow"></div>
                <div class="content indent">
<form id="paymentform" action="amd8.php" method="post">
<br/>
<br />
   <label><b>Camp Sessions: 1-6 (Check off the week(s) the camper will be participating in.)</b> </label><br /><br />
   <ol>
   <?php  
for ($count = 0; $count < $limit; $count=$count+1)
        {
            echo"<li>"; echo $newChangedString[$count]; echo '($'; echo $newChangedString2[$count]; echo '/child)<input type=checkbox name="campsessions[]" value="'; echo $newChangedString[$count]; echo '"></li>'; 
        }
     ?>
   </ol>
    <br/>
    <label><b>Select camp location:</b> </label><br /><br />

    <select name="schoolChoice">
 <?php  
for ($count1 = 0; $count1 < $anotherLimit; $count1=$count1+1)
        {
            echo"<option value="; echo $newChangedString3[$count1]; echo '">'; echo $newChangedString3[$count1]; echo "</option>";        
        }
?> 
    </select>
<br/><br/>

 <input type="hidden" name="totalprice" />  
<input type="submit" name="formSubmit" value="Submit" class="button greenButton"/>
<input type="button" name="cancel" value="Cancel" class="button redButton" onclick="href='activemindandbody.org'">
</form>

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

对于以下页面:(当我在上一页上单击提交时没有加载的页面,反而会给我一个"服务器错误")

<?php
session_start();
if($_POST['formSubmit'] == "Submit")
{
    $errorMessage="";
    if (empty($_POST['campsessions[]'])) 
    {
        $errorMessage .="";
    }
    if (empty($_POST['campcare'])) 
    {
        $errorMessage ="";
    }
    if (empty($_POST['totalprice']))
    {
        $errorMessage ="";
    }
    $_SESSION['campcare'] = $_POST['campcare'];
    $_SESSION['campsessions'] = $_POST['campsessions'];
    $_SESSION['totalprice'] = $_POST['totalprice'];
    $_SESSION['schoolChoice'] = $_POST['schoolChoice'];
    $weeklyPrice =  $_SESSION['quickVariable'];
    $totalPrice = 0;

    while (isset($_SESSION['campsessions']))
    {   
        foreach ($weeklyPrice as $total)
            {
                $totalPrice = $totalPrice + $total;
            }
    }

}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Active Mind And Body</title>
        <!-- BC_OBNW -->
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
        <link href="http://activemindandbody.org/StyleSheets/ModuleStyleSheets.css"                 rel="StyleSheet" type="text/css" />
        <script type="text/javascript">var jslang='EN';</script>
        <link href="http://activemindandbody.orgStyleSheets/ModuleStyleSheets.css"                  rel="StyleSheet" type="text/css" />
        <script type="text/javascript">var jslang='EN';</script>
        <link href="http://activemindandbody.org/style.css" rel="stylesheet" type="text/            css" />
        <link rel="shortcut icon" href="../favicon.ico"> 
        <link rel="stylesheet" type="text/css" href="css/demo.css" />
        <link rel="stylesheet" type="text/css" href="style3.css" />
        <script type="text/javascript" src="js/modernizr.custom.04022.js"></script>
        <link href='http://fonts.googleapis.com/css?family=Open+Sans+Condensed:                     700,300,300italic' rel='stylesheet' type='text/css'>
        <link type="text/css" rel="stylesheet" href="stylesheet.css"/>
        <link href='http://fonts.googleapis.com/css?family=Alef' rel='stylesheet'                   type='text/css'>
        <script src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.7.js"></script>
        <script src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.10/jquery-ui.js"></script>
        <script src="Javascript2.js" type="text/javascript"></script>    
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    </head>
    <body>
        <!-- BC_OBNW -->
        <div id="container">
            <div id="header"><div id="header_text" style="top: 118px; left: 23px;">
                <p>Make a donation today and help&nbsp;&nbsp;âCreating a Brighter Future                      
    Together.â&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; <a href="http://activemindandbody.org/donation.html">Make a donation now !</a> </p>
        </div>
        </div>
        <div id="content">
                    <section class="tabs">
                <input id="tab-1" type="radio" name="radio-set" class="tab-selector-1" checked="checked" />
                <label for="tab-1" class="tab-label-1">1. Camper's Info</label>
                <input id="tab-2" type="radio" name="radio-set" class="tab-selector-2" />
                <label for="tab-2" class="tab-label-2">2. Parent's Info</label>
                <input id="tab-3" type="radio" name="radio-set" class="tab-selector-3" />
                <label for="tab-3" class="tab-label-3">3. Emergency<br/> Contacts</label>
                <input id="tab-4" type="radio" name="radio-set" class="tab-selector-4" />
                <label for="tab-4" class="tab-label-4">4. Camper's Ride Home</label>
                <input id="tab-5" type="radio" name="radio-set" class="tab-selector-5" />
                <label for="tab-5" class="tab-label-5">5. <br/> Camp Days & Prices</label>
                 <input id="tab-6" type="radio" name="radio-set" class="tab-selector-6" />
                <label for="tab-6" class="tab-label-6">6. Confirm your details</label>
                <input id="tab-7" type="radio" name="radio-set" class="tab-selector-7" />
                <label for="tab-7" class="tab-label-7">7. <br />Paypal</label>
                    </section>
                <div class="clear-shadow"></div>
                <div class="content indent">
<br />              
Please confirm the following details:
<p class="confirmation">
<b>SPECIAL CONSIDERATIONS AND PAYMENT INFORMATION:</b><br/><br/>
&nbsp;&nbsp;Is Camper picked up? <?php echo $_SESSION['camperpickedup'] ?> <br/><br/>
&nbsp;&nbsp;Who is picking up the camper? <?php echo $_SESSION['personPickingUpCamper'] ?> <br/><br/>
&nbsp;&nbsp;Chosen Camp Sessions: <br/>
<pre> <?php 
if(isset($_SESSION['campsessions']))
{
    foreach ($_SESSION['campsessions'] as $_SESSION['campsessions'])
    {
        echo "{$_SESSION['campsessions']}'n";
    }
}
?> 
</pre> <br />
</p> 
<p>
Total Price: $<?php echo $_SESSION['totalprice'] ?><br/><br/>
</p>
<form action="amd9.php" method="post">
<input type="submit" value='Confirm' class="button greenButton" name="formSubmit"> 
</form>

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

很抱歉,如果代码太多,请告诉我,这样我就可以删掉它,我上次因为没有提供正确的信息而受到了很多惩罚。

但作为一个更普遍的问题,我在做什么,关于使用php变量来显示我的表单,好吗还是这是个大问题

while (isset($_SESSION['campsessions']))
{   
    foreach ($weeklyPrice as $total)
        {
            $totalPrice = $totalPrice + $total;
        }
}

这将是一个无限循环,因为变量永远不会被取消设置。

使用if,而不是while

您的代码中充满了错误,但我想说,让服务器崩溃的是

while (isset($_SESSION['campsessions']))

由于isset($_SESSION['campsessions'])等于true,您还不如编写

while(true)

这是一个无限循环。PHP会超时并给您一个错误。请改用if。

不过,这只是其中一件糟糕的事情。我不知道为什么要发出5个查询来检索表中一行的6个字段,并且有一个if(空($_POST['camsessions[]']))看起来不正确。