Hi all,
I have a few questions on the following code. The code will take user data from a form and add it to an array, then it will add additional to the existing array for as long as the user continues to add to it.
Specifically I have questions on lines of code:
- line 45: Why would you run the foreach loop again?
- line 46: What is the purpose of running a html input type=hidden for this portion of the code?
- line 50: Why would you you put the submit button after the hidden value instead of above where the original form was written on lines 37-41?
Thanks for the help understanding this.
[php]
User Input Added to ArrayHow Are You Traveling?
<?php //If form not submitted, display form. if (!isset($_POST['submit'])){ $travel=array( "Automobile", "Jet", "Ferry", "Subway" ); ?>Travel takes many forms, whether across town, across the country, or around the world. Here is a list of some common modes of transportation:
-
<?php
foreach ($travel as $t){
echo "
- $t \n"; } ?>
Please add your favorite, local, or even imaginary modes of travel to the list, separated by commas:
<?php //Send current travel array as hidden form data. foreach ($travel as $t){ echo "\n"; } ?> <?php //If form submitted, process input. }else{ //Retrieve established travel array. $travel=($_POST['travel']); //Convert user input string into an array. $added=explode(',',$_POST['added']); //Add to the established array. array_splice($travel, count($travel), 0, $added); //This could also be written $travel=array_merge($travel, $added); //Return the new list to the user. echo "
Here is the list with your additions:
\n- \n";
foreach($travel as $t){
//The trim functions deletes extra spaces the user may have entered.
echo "
- ".trim($t)." \n"; } echo"
Add more?
<?php //Send current travel array as hidden form data. foreach ($travel as $t){ echo "\n"; } ?> <?php } ?> [/php]