Krunk Posted July 1, 2007 Share Posted July 1, 2007 From what I've been learning about php I should be able to do this: 1. fill in this form: menu.html: <html> <body> <form action="menu.php" action="POST"> <input type=text name=breakfast>Breakfast<br> <input type=text name=lunch>Lunch<br> <input type=text name=snack>Snack<br> <input type=text name=dinner>Dinner<br> <input type=submit name=submit> </form> </body> </html> 2. to be processed by this form: menu.php: <? error_reporting(E_ALL); $row_color = array('red','green'); $color_index = 0; $meal = array( 'breakfast' => '$_POST["$breakfast"]', 'lunch' => '$_POST[$lunch]', 'snack' => '$_POST[$snack]', 'dinner' => '$_POST[$dinner]'); print "<table>\n"; foreach ($meal as $key => $value) { print '<tr bgcolor="' . $row_color[$color_index] . '">'; print "<td>$key</td><td>$value</td></tr>\n"; // This switches $color_index between 0 and 1$color_index = 1 - $color_index; } print '</table>'; ?> Can't these variables be passed to an array like this to be displayed in the table? I'm playing while learning here but what you may guess I'm trying to do is create menu items that I can change by using the form. The table it produces is one of those alternating colors I discovered from a script which i thou8ght was pretty cool. Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted July 1, 2007 Share Posted July 1, 2007 Try this: menu.html <html> <body> <form action="menu.php" method="POST"> <input type=text name=breakfast>Breakfast<br> <input type=text name=lunch>Lunch<br> <input type=text name=snack>Snack<br> <input type=text name=dinner>Dinner<br> <input type="submit" name="submit" value="Submit"> </form> </body> </html> menu.php <?php $row_color = array('red','green'); $color_index = 0; foreach ($_POST as $val){ if ($val != "submit" && !empty($val)){ $meal[] = $val; } } print "<table>\n"; foreach ($meal as $key => $value) { print '<tr bgcolor="' . $row_color[$color_index] . '">'; print "<td>$key</td><td>$value</td></tr>\n"; // This switches $color_index between 0 and 1$color_index = 1 - $color_index; } print '</table>'; ?> Quote Link to comment Share on other sites More sharing options...
Krunk Posted July 1, 2007 Author Share Posted July 1, 2007 Worked perfectly. Tommorow I declare Sunday July 31, 2007 as pocobueno1388 day at my house. Thanks. But why wouldn't the other work? Seems like it should'v3e Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted July 1, 2007 Share Posted July 1, 2007 I didn't test yours...so it might of, haha. Here was one of the major mistakes that would throw everything off: This is what you had: <form action="menu.php" action="POST"> You had "action" = "POST". It should have been method. Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted July 1, 2007 Share Posted July 1, 2007 I just played around with your code, and you were VERY close to getting it right. Here is what you did wrong: What I mentioned in my last post Also this part: <?php $meal = array( 'breakfast' => '$_POST["$breakfast"]', 'lunch' => '$_POST[$lunch]', 'snack' => '$_POST[$snack]', 'dinner' => '$_POST[$dinner]'); ?> First off, variables should not be in single quotes. You don't put a dollar sign in front of the word inside the brackets. change that code to this: <?php $meal = array( 'breakfast' => $_POST['breakfast'], 'lunch' => $_POST['lunch'], 'snack' => $_POST['snack'], 'dinner' => $_POST['dinner']); ?> So your full working code would look like this if you did it your way: menu.html <html> <body> <form action="menu.php" method="POST"> <input type=text name=breakfast>Breakfast <input type=text name=lunch>Lunch <input type=text name=snack>Snack <input type=text name=dinner>Dinner <input type="submit" name="submit" value="Submit"> </form> </body> </html> menu.php <?php $row_color = array('red','green'); $color_index = 0; $meal = array( 'breakfast' => $_POST['breakfast'], 'lunch' => $_POST['lunch'], 'snack' => $_POST['snack'], 'dinner' => $_POST['dinner']); print "<table>\n"; foreach ($meal as $key => $value) { print '<tr bgcolor="' . $row_color[$color_index] . '">'; print "<td>$key</td><td>$value</td></tr>\n"; // This switches $color_index between 0 and 1$color_index = 1 - $color_index; } print '</table>'; ?> Quote Link to comment Share on other sites More sharing options...
Krunk Posted July 1, 2007 Author Share Posted July 1, 2007 You had "action" = "POST". It should have been method. --interesting because the form variables showed up in the url as if it was using GET. and I can't believe I didn't see that mistake even after looking at it 50 times! Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted July 1, 2007 Share Posted July 1, 2007 --interesting because the form variables showed up in the url as if it was using GET. Yeah, that is what happens when you don't define a method. It prints out a huge query string in the URL. So if you ever see that, you will know what you did wrong, haha. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.