Help displaying my form results on the same page

Hi guys. Well am nearly there with my first PHP project, its been difficult at time but I have got the results to display the information I want. BUT. What I would like to do is to display the results from my form to be displayed on the same page. Is it possible to do this. this is the code that handles the form

<form method=post name=f1 action='dd-check.php'>

<select name = 'day' class="listBox" tabindex="4" >
    <?php
    // generate day numbers
    for ($x = 1; $x <= 31; $x++) {
        echo "<option value = $x>$x</option>";
    }
    ?>
    </select>
    <select name = 'month' class="listBox" tabindex="4" >
    <?php
    // generate month names
    for ($x = 1; $x <= 12; $x++) {
        echo "<option value=$x>".date('F', mktime(0, 0, 0, $x, 1, 1)).'</option>';
    }
    ?>
    </select>
    <select name = 'year' class="listBox" tabindex="4" >
    <?php
    // generate year values
    for ($x = 2007; $x <= 2008; $x++) {
        echo "<option value=$x>$x</option>";
    }
    ?>
    </select>
<br />

<?

/*
If register_global is off in your server then after reloading of the page to get the value of cat from query string we have to take special care.
To read more on register_global visit.
  http://www.plus2net.com/php_tutorial/register-globals.php
*/
@$cat=$_GET['cat']; // Use this line or below line if register_global is off
//@$cat=$HTTP_GET_VARS['cat']; // Use this line or above line if register_global is off

///////// Getting the data from Mysql table for Pick Up list box//////////
$querPick=mysql_query("SELECT city, id FROM uk_place");
if (!$querPick) {
exit('<p>Unable to obtain category list from the '  .
       'database.</p>');
}
///////////// End of query for first list box////////////



///////// Getting the data from Mysql table for first list box//////////
$quer2=mysql_query("SELECT DISTINCT country,id FROM eu_place order by country");
///////////// End of query for first list box////////////

/////// for second drop down list we will check if category is selected else we will display all the subcategory/////
if(isset($cat) and strlen($cat) > 0){
$quer=mysql_query("SELECT DISTINCT city FROM eu_place where id=$cat order by city");
}else{$quer=mysql_query("SELECT DISTINCT city FROM eu_place order by city"); }
////////// end of query for second subcategory drop down list box ///////////////////////////




/// Add your form processing page address to action in above line. Example  action=dd-check.php////
//////////        Starting of first drop downlist /////////
echo "<select name='cat' onchange=\\"reload(this.form)\\"><option value=''>Select one</option>";
echo mysql_error();
while($noticia2 = mysql_fetch_array($quer2)) {
if($noticia2['id']==@$cat){echo "<option selected value='$noticia2[id]'>$noticia2[country]</option>"."<BR>";}
else{echo  "<option value='$noticia2[id]'>$noticia2[country]</option>";}
}
echo "</select>"."<BR>";
//////////////////  This will end the first drop down list ///////////



//////////        Starting of second drop downlist /////////
echo "<select name='subcat'><option value=''>Select one</option>";
while($noticia = mysql_fetch_array($quer)) {
echo  "<option value='$noticia[city]'>$noticia[city]</option>";
}
echo "</select>"."<BR>";
//////////////////  This will end the second drop down list ///////////




echo 'Full Load'."<label><input type='radio' name='load' value='full_load'</label>";
echo 'Half Load' . "<label><input type='radio' name='load' value='half_load'</label>"."<BR>";

//////////        Starting of second drop downlist /////////
echo "<select name='pickup'><option value=''>Select one</option>";
while($pickup = mysql_fetch_array($querPick)) {
echo  "<option value='$pickup[city]'>$pickup[city]</option>";
}
echo "</select>"."<BR>";
//////////////////  This will end the second drop down list ///////////


//// Add your other form fields as needed here/////
echo "<input type=submit value=Submit>";

?>

</form>

and this is the code that displays my results

<?php
$pickup=$_POST['pickup'];
$cat=$_POST['cat'];
$subcat=$_POST['subcat'];
$load=$_POST['load'];
$appointment_date = sprintf('%02d-%02d-%04d',$_POST['day'], $_POST['month'], $_POST['year']); //mm-dd-yyyy
echo "Date required $appointment_date"."<BR>";
echo "Picking up from $pickup" ."<BR>";
echo "Going to $subcat"."<BR>";




$result = mysql_query("SELECT full_price, half_price FROM eu_place WHERE city = '" . mysql_real_escape_string($subcat) . "'") or die(mysql_error());
$row = mysql_fetch_array($result);

if ($load == 'full_load') {
  $total = $row['full_price'];

} else {
  $total = $row['half_price'];

}

$rResult = mysql_query("SELECT price FROM uk_place WHERE city = '" . mysql_real_escape_string($pickup) . "'") or die(mysql_error());
while ($aRow = mysql_fetch_array($rResult, MYSQL_ASSOC))
{
    $iPrice = $aRow['price'];
	echo "£";
	echo ($iPrice) + ($total);
}
?>

I know that its go to have something to do with the form action, but don’t know where to start, have tried a couple of things but no joy

Are you posting all the information to the same page?

try like this


<?php
$db_host = "";
$db_name = "";
$db_username = "";
$db_password = "";
mysql_connect($db_host,$db_user,$db_pass);
mysql_select_db($db_name);
if(!empty($_POST['shout']))
    {

    $shout = mysql_real_escape_string($_POST['shout']);
    mysql_query("INSERT INTO shoutbox (shout) VALUES ('$shout')");
}
$sql = mysql_fetch_array(mysql_query("SELECT shout FROM shoutbox  LIMIT 1"));
echo $sql[0];
?>
<html>
<body>
  <form action="<?php $PHP_SELF ?>" method="POST">
  Shout: <input type="text" name="shout" />
  <input type="submit" value="Shout" />
  </form>
</body>
</html>

You can put all of your code in one file and at the top check for the request type. If it’s POST then you know that the form has been submitted and you process it. If it’s not (ie. it’s GET) then you display the form. It’s not a good practice, though. You should always redirect after succesfull POST:

yeah when I click submit I want it to display below on the same page

Have 2 files: one with the form (lets call it form.php) and one that will process it (lets call it process.php).

In process.php, once you’ve inserted data into the database, pass on the last insert id to form.php:


header('Location: /form.php?id=' . $last_insert_id);

In form.php, under the form, check if $_GET[‘id’] is set. If it is then fetch the data from the database and display it as required.

dd-check.php

<?php
$dbhost = "";
$dbname = "";
$dbuser = "";
$dbpass = "";
mysql_connect($dbhost,$dbuser,$dbpass);
mysql_select_db($dbname);
$pickup=$_POST['pickup'];
$cat=$_POST['cat'];
$subcat=$_POST['subcat'];
$load=$_POST['load'];
$appointment_date = sprintf('&#37;02d-%02d-%04d',$_POST['day'], $_POST['month'], $_POST['year']); //mm-dd-yyyy
echo "Date required $appointment_date"."<BR>";
echo "Picking up from $pickup" ."<BR>";
echo "Going to $subcat"."<BR>";




$result = mysql_query("SELECT full_price, half_price FROM eu_place WHERE city = '" . mysql_real_escape_string($subcat) . "'") or die(mysql_error());
$row = mysql_fetch_array($result);

if ($load == 'full_load') {
  $total = $row['full_price'];

} else {
  $total = $row['half_price'];

}

$rResult = mysql_query("SELECT price FROM uk_place WHERE city = '" . mysql_real_escape_string($pickup) . "'") or die(mysql_error());
while ($aRow = mysql_fetch_array($rResult, MYSQL_ASSOC))
{
    $iPrice = $aRow['price'];
    echo "&#163;";
    echo ($iPrice) + ($total);
}
?>
<html><head><title>title</title></head>
<body><form method=post name=f1 action='<?php $PHP_SELF ?>'>

<select name = 'day' class="listBox" tabindex="4" >
    <?php
    // generate day numbers
    for ($x = 1; $x <= 31; $x++) {
        echo "<option value = $x>$x</option>";
    }
    ?>
    </select>
    <select name = 'month' class="listBox" tabindex="4" >
    <?php
    // generate month names
    for ($x = 1; $x <= 12; $x++) {
        echo "<option value=$x>".date('F', mktime(0, 0, 0, $x, 1, 1)).'</option>';
    }
    ?>
    </select>
    <select name = 'year' class="listBox" tabindex="4" >
    <?php
    // generate year values
    for ($x = 2007; $x <= 2008; $x++) {
        echo "<option value=$x>$x</option>";
    }
    ?>
    </select>
<br />

<?php

$querPick=mysql_query("SELECT city, id FROM uk_place");
if (!$querPick) {
exit('<p>Unable to obtain category list from the '  .
       'database.</p>');
}
$quer2=mysql_query("SELECT DISTINCT country,id FROM eu_place order by country");
if(isset($cat) and strlen($cat) > 0){
$quer=mysql_query("SELECT DISTINCT city FROM eu_place where id=$cat order by city");
}else{$quer=mysql_query("SELECT DISTINCT city FROM eu_place order by city"); }

echo "<select name='cat' onchange=\\"reload(this.form)\\"><option value=''>Select one</option>";
echo mysql_error();
while($noticia2 = mysql_fetch_array($quer2)) {
if($noticia2['id']==@$cat){echo "<option selected value='$noticia2[id]'>$noticia2[country]</option>"."<BR>";}
else{echo  "<option value='$noticia2[id]'>$noticia2[country]</option>";}
}
echo "</select>"."<BR>";
echo "<select name='subcat'><option value=''>Select one</option>";
while($noticia = mysql_fetch_array($quer)) {
echo  "<option value='$noticia[city]'>$noticia[city]</option>";
}
echo "</select>"."<BR>";

echo 'Full Load'."<label><input type='radio' name='load' value='full_load'</label>";
echo 'Half Load' . "<label><input type='radio' name='load' value='half_load'</label>"."<BR>";

echo "<select name='pickup'><option value=''>Select one</option>";
while($pickup = mysql_fetch_array($querPick)) {
echo  "<option value='$pickup[city]'>$pickup[city]</option>";
}
echo "</select>"."<BR>";
echo "<input type=submit value=Submit>";

?>

</form></body></html>

From what I see you’re not modifying the database so there’s no reason to use POST. Use GET instead.