Need help understanding some lines of code

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 Array

How 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]

First one is generates a visible list to display on the page, the second one generates a set of hidden form fields. You could technically do this in the same loop, but it would look like a mess in html.

It makes the travel types an array which will be posted with the form data, for some reason the user isn’t supposed to interact with it, so it would only be confusing to show it. I am guessing the php code that receives the form needs the transportation types for something and someone for some reason figured this was the easiest way to send them there.

Good practice to have the submit button at the end of the form.

Cheers for the explanation. :slight_smile: How could this be written with the same outcome without using the hidden form fields?

Move the travel types to a store/database instead of instantiating it as an array in a file. Then fetch the types both in the form page and the php page the forms submits to.

Add the $travel array to the session.

[php]//top of page
session_start();

$_SESSION[‘travel’] = $travel;[/php]

Also, no need to loop through the array to generate the hidden input:

[php]echo “<input type=“hidden” name=“travel[]” value=”" . serialize($travel) . “” />\n";[/php]
Then of course unserialize() when needed.

Sponsor our Newsletter | Privacy Policy | Terms of Service