advertisement

In this php tutorial, we will learn how to create multidimensional arrays in PHP, we will be creating a form, and another page to handle the form to demonstrate the use of multi-dimensional arrays in PHP.

We want to start off by creating a new HTML document.

<html> 
<head> 
<title>Tell US About Yourself</title> 
</head> 
</body> 

This is just some basic HTML code, if you dont understand it then you may want to go read some HTML tutorials because it is the most basic “coding” you will find anywhere.

Now we begin our HTML form.

<form action="handle_form.php" method="post"> 
<fieldset>
<legend>Enter your information in the form below:</legend> 
<p>
<b>Name :</b>
 <input type="text" name="name" size="20" maxlength="40" />
</p>

Our form will use handle_form.php to send all it’s data to be processed to. The first input in our form is going to be the users Name, so we create a text input and give it a name=” ” of name.

Now we need to create a series of check boxes so that users can select what there interests are.

<p>
<b>Interests:</b> 
<input type="checkbox" name="interests[]" value="Music" /> Music 
<input type="checkbox" name="interests[]" value="Movies" /> Movies 
<input type="checkbox" name="interests[]" value="Sports" /> Sports 
<input type="checkbox" name="interests[]" value="Reading" /> Reading 
<input type="checkbox" name="interests[]" value="Relaxing" /> Relaxing 
</p>

In the interests section of the form, the user is given the choice to select multiple interests using a checkbox. Instead of giving the checkbox the same name as it’s value, we are going to give it the name of interests[ ] so that whatever the user chooses will be put into the interests[ ] array which we will later on be able to access by using the $_POST[‘interests’] global variable that will have all there selections saved.

Now we just need to finish off our document by closing off all of our tags.

</fieldset> 
<div align="center">
<input type="submit" name="submit" value="Submit my Info!" />
</div>
 </form> 
</body> 
</html>

Save this file as about.html.

Now we need to create handle_form.php since that is where our form submits all of it’s information, and in this file is where we will use the $_POST[‘interests’] multidimensional array. So we start off by creating a new HTML document.

<html><head>
<title>Your form feedback</title> 
</head>
<body> <?php

Now we need to validate that they actually entered in there name.

if (!empty($_POST['name'])) 
{ $name = stripslashes($_POST['name']; } 
else 
{ $name = NULL; echo '<p><font color="red">Please go back and enter your name into the form.</font></p>'; }

Now we validate the users interests.

if (isset($_POST['interests'])) { $interests = TRUE; }
 else 
{ $interests = NULL; echo '<p><font color="red">Please go back and enter in your interests into the form.</font></p>'; }

This piece of code validate that the user actually selected at least one of the interests on the previous page, if they did then the variable $interests is set to TRUE, if they selected none of them then $variables is set to NULL.

Now we just need to start our final conditional statements.

if ($name && $interests) 
{ echo "Thank you, <b>$name</b>. You entered your interests as:<ul>"; 

If the form was filled out properly both the $name and $interests varialbe will be set to TRUE, this last conditional statement just checks if both variables are set to TRUE, if they are, then you can continue.

Now we need to print out the selected interests from the previous page.

foreach ($_POST['interests'] as $value) 
{ echo "<li>$value</li>\n"; }

To access the interests from the previous page we will use a foreach loop, we will print out the array $_POST[‘interests’] using this foreach loop. Seeing as how checkboxes only have a value if they are selected, then this will only print out the selections made by the user. Now we need to finalize that final conditional.

echo '</ul>'; } 
else { echo '<p><font color="red">Please go back and fill out the form again</font></p>';}

The first echo ends the first <ul> tag, then we just create an else statement so that if there were any errors in the form it will just tell the user to go back and fill it in properly. Now we just end off our PHP document, and our HTML document.

?> </body> </html>

Save the file as handle_form.php, upload both about.html and handle_form.php to your web server and test it out!

The complete php script code is below:

<html><head>
<title>Your form feedback</title> 
</head>
<body> <?php

if (!empty($_POST['name'])) 
{ $name = stripslashes($_POST['name']; } 
else 
{ $name = NULL; echo '<p><font color="red">Please go back and enter your name into the form.</font></p>'; }

if (isset($_POST['interests'])) { $interests = TRUE; }
 else 
{ $interests = NULL; echo '<p><font color="red">Please go back and enter in your interests into the form.</font></p>'; }


if ($name && $interests) 
{ echo "Thank you, <b>$name</b>. You entered your interests as:<ul>"; 

foreach ($_POST['interests'] as $value) 
{ echo "<li>$value</li>\n"; }

echo '</ul>'; } 
else { echo '<p><font color="red">Please go back and fill out the form again</font></p>';}

?> </body> </html>

I hope you enjoyed this tutorial, if you need any help with it don’t hesitate to contact us.

advertisement