Jump to content

[SOLVED] form variables passed to an array question


Krunk

Recommended Posts

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.

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>';

?>

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.

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>';

?>

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.