mtech Posted July 25, 2007 Share Posted July 25, 2007 I'm learning php and got this example from a book. I am also running a WAMP server on my personal computer for my php projects so I'm not sure if the problem is the .ini file for php or the book made a mistake. Now I understand how variables are created once they are passed from the form to php, but the book explains that you can make the name in the input element an array like this <form method="POST" action=" checkboxes.php"> Have you ever eaten haggis before? <input name="Choice[]" type="checkbox" value="Haggis"> <br> Have you ever eaten snails before? <input name="Choice[]" type="checkbox" value="Snails"> <br> Have you ever eaten locusts before? <input name= "Choice[]" type="checkbox" value="Locusts"> <br> <br> <input type="submit" value=" Submit"> </form> and the basic php code to handel this is //checkboxes.php <?php echo "$_POST[Choice][0]<br>"; echo "$_POST[Choice][1]<br>"; echo "$_POST[Choice][2]<br>"; ?> now this is just the basics for learning. so, the book says that if all three checkboxes are checked the result should be Haggis Snails Locust but what I get as a result on my computer is Array[0] Array[1] Array[2] Can anyone explain what might be happening here. Is the result the book has correct or is the result that I get correct or is there some other missing information that I don't have that I need to make it work right or could it be the php.ini file settings. Thanks in advance mtech Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted July 25, 2007 Share Posted July 25, 2007 saying Choice[] in the html doesn't do anything The best way to making "static" arrays (such as in a form) is to name your inputs in a method to use a loop to recall such as <html> <form method="POST" action=" checkboxes.php"> Have you ever eaten haggis before? <input name="Choice_0" type="checkbox" value="Haggis"> <br> Have you ever eaten snails before? <input name="Choice_1" type="checkbox" value="Snails"> <br> Have you ever eaten locusts before? <input name= "Choice_3" type="checkbox" value="Locusts"> <br> <br> <input type="submit" value=" Submit"> </form> </html> now to the php part $_POST is a superglobal variable, you can't edit them and you can't have arrays in them it is an array of all values passed via post so this is how you can simply make an array of them in php Note the use of a period to create connectivity between the string and variable <?php $i = 0; while(ISSET($_POST['Choice_'.$i]){ $choice[] = $_POST['Choice_'.$i']; $i++; } //lets read it print_r($choice); ?> Quote Link to comment Share on other sites More sharing options...
deadimp Posted July 25, 2007 Share Posted July 25, 2007 The only problem you're running into is how you set up your string. PHP is only parsing the first array index, since that's all it should really scan if it should remain efficient. So it scans "$_POST[Choice][0]", sees $_POST['Choice'] there, 'converts' it to a string, which is "Array" (since $_POST['Choice'] is an array), and then goes on to the rest of the string. It sees the "[0]" sitting there, can't do anything with it, throws it on to the rest of the string, and voila, you get "Array[0]" as your output. There are several ways you could approach to displaying this: > Enclose the expression in the string in brackets {}, so all of it is scanned: echo "{$_POST[Choice][0]}<br>"; > Just echo that variable outside of the string and append the rest: echo $_POST['Choice'][0]."<br>"; > Or switch crap around: foreach ($_POST['Choice'] as $key => $choice) { echo "$choice<br>"; } saying Choice[] in the html doesn't do anything It's not necessarily HTML that handles the array. All the browser (HTML) gives a crap about is the string represented by the name attribute. It just sends that string verbatim in the POST request. It's up to PHP to do anything about it. Luckily, it does, and it has the array subscript signify that it's actually an array. Quote Link to comment Share on other sites More sharing options...
mtech Posted July 25, 2007 Author Share Posted July 25, 2007 ok thanks guys for clearing that up. you both were very helpfull Thank you mtech 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.