Hi di1,
Yes, you've got into a bit of a tangle with your program flow.
It's best (if you can) to split the program flow from the detail by using functions. Try to get the order of execution/conditionals clear before you start. You should be able to write down in English exactly what you wish to do before you even look at any code.
You would have run into another problem later on anyway ... which was where you used a for...loop to create the second form listing all the possible mixers etc. All the mixer-input-texts would have all ended up with the same name ... see the code for the solution.
If you need anymore explanation of any of the changes I've made here, post them on this thread.
Here it is (I took the liberty of tidying up and removing unnecessary php and I might have messed up your layout)
...
<html>
<head>
<title>Untitled</title>
</head>
<body>
<?php
// Program ------------------------------------------------------------------------
switch($submit){
case "Add":
do_the_mysql_query_add_the_data();
break;
case "Submit":
second_form();
break;
default:
first_form();
break;
}
?>
</body>
</html>
<?php
// Functions ----------------------------------------------------------------------
// do_the_mysql_query_add_the_data
function do_the_mysql_query_add_the_data(){
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="<?=$_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
}
// first form ----------------------------------------------------------------------
function second_form(){
global $_SERVER, $_POST;
?>
<h2>Insert a new cocktail</h2>
<form action="<?=$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="submit" value="Add">
</form>
<?php
}
// End Functions ----------------------------------------------------------------
?>
🙂 🙂 🙂