laserlight: The form clears out the values I put in and looks the same as it did when I first looked at the page.
<?php
// Program ------------------------------------------------------------------------
switch($submit){
case "Add":
insert_into_pg();
break;
case "Submit":
second_form();
break;
default:
first_form();
break;
}
?>
</body>
</html>
<?php
// Functions ----------------------------------------------------------------------
// insert_into_pg
function insert_into_pg(){
global $_POST;
echo "Form is submitted
<hr />
Here are all your [POST] variables and valuables to insert data sql code etc ...
<br />They have to be named uniquely (using the i's and the j's) otherwise
later occurences will overwrite earlier ones ...
<hr />
";
foreach($_POST as $name => $value){
echo "<b>".$name."</b> = ".$value."<br />";
}
}
// first form ----------------------------------------------------------------------
function first_form(){
global $_SERVER;
?>
<form action="
<?php echo $_SERVER['PHP_SELF'];?>
" method="POST">
How many alcoholic drinks are there? <input name="alcdrnks" type="text" size="5">
<br />
How many non-alcoholic drinks are there? <input name="nalcdrnks" type="text" size="5">
<br />
<input type="submit" name="Submit" value="Submit">
</form>
<?php
}
// second form ----------------------------------------------------------------------
function second_form(){
global $_SERVER, $_POST;
?>
<h2>Insert a new cocktail</h2>
<form action="
<?php echo $_SERVER['PHP_SELF'];?>
" method="POST">
Name: <input name="name" type="text" size="50">
<br />Instructions: <textarea name="instructions" rows="6" cols="40"></textarea>
<br />
<?php
for ($i = 1; $i <= $_POST["nalcdrnks"]; $i++)
{
echo "
Mixer: <input name=\"mixer_".$i."\" type=\"text\" size=\"50\">
<br />
Amount: <input name=\"mamnt_".$i."\" type=\"text\" size=\"20\">
<br />
Flavor: <input name=\"mxrflavr_".$i."\" type=\"text\" size=\"50\">
<br />
<hr />
";
}
echo "<br/><br/>";
for ($j = 1; $j <= $_POST["alcdrnks"]; $j++)
{
echo "
Alcohol: <input name=\"alc_".$j."\" type=\"text\" size=\"50\">
<br/>
Amount: <input name=\"alcamnt_".$j."\" type=\"text\" size=\"20\">
<br/>
Flavor: <input name=\"alcflavr_".$j."\" type=\"text\" size=\"50\">
<br />
";
}
?>
<br/>
<input type="submit" name="Add" value="Add">
</form>
<?php
}
?>