简体   繁体   中英

Having trouble passing form that consists of php variables

So I have 2 pages of my multi-page form that I am using to test. When I test the first part of the form seperately, it works fine. Everything is being displayed accordingly to the information that it's retrieving from my database. Everything looks great until I click the submit button to the next page, that confirms all the values. When I try and hit submit, which is supposed to also pass the user's inputs and hold them as $_POST[], it's giving me a sever error. I'm very new to php so I'm not too familiar with everything I might need to be.

Here's my code for the first part:

<?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>

And for the following page: (The one that doesn't load when I click submit on my previous page, and instead gives me a "server error")

<?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>

Sorry if it's too much code let me know so I can snip it down, I got penalized a lot for not providing proper information last time.

But as a more general question, is what I'm doing, about using php variables to display my form, ok? Or is that the big problem?

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

This will be an infinite loop because the variable will never become unset.

Use if , not while

Your code is quite littered with errors - but I would say what is throwing the server off is that

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

as isset($_SESSION['campsessions']) equals to true you might as well have written

while(true)

which is an infinite loop. PHP will time out and give you an error. Use an if instead.

That's only one of the bad things going on though. I'm not sure why you're issuing 5 queries to retrieve 6 fields of a row in the table, and there's a if (empty($_POST['campsessions[]'])) that doesn't look right.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM