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.

Link to comment
Share on other sites

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

?>

Link to comment
Share on other sites

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

?>

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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