Phpdynamic

PHP and MySQL Programming Guides, Tutorials, & Articles

Working with Forms in PHP – Part 2

Working with Forms in PHP – Part 2

NOV 18

Form Elements with Multiple Values

In the first part of this tutorial, we worked with input boxes and radio buttons, which only allow a single data value to be submitted per form element. We are not limited to single values, because HTML also allows form elements such as check-boxes and select-boxes that allow the user to choose multiple items simultaneously for a single form field, so we need a way to process them a little differently in PHP. Lets start with an example:

<!-- multiple values example with select-box -->

<p>What products to you currently own? (select all that apply)</p>

<from action="process.php" action='GET'>
<select name="products[]" multiple>
	<option value="mp3 player">Mp3 Player</option>
	<option value="pc">PC</option>
	<option value="laptop">Laptop</option>
	<option value="cellphone">Cellphone</option>
</select>
<br>
	<input type="submit" value="Submit">
</form>

The form will look similar to this:

select form example

The select element in this example allows the user to make multiple selections. Thus we need a way to pass all the values selected by the user to the process.php script. The way we accomplish this is by adding an empty set of brackets after the name of the form field like this products[]. Essentially, what this does is when the user hits the submit button all the selected data values are stored in an array, in this case products[] instead of a single variable and passed to the processing script. Lets look at how to process this in PHP:

<?php
if (isset($_GET['products'])) {
	echo "You made the following selections:";
	echo "<br>";

	foreach ($_GET['products'] AS $products) {
	echo "$products";
	echo "<br>";
	}
}
?>

We can apply the same principal to any form element that can return multiple values for each form element. In the following example we’ve used check-boxes to demonstrate this.

<!-- multiple values example with check-boxes-->

<p>How did you hear about us? (check all that apply)</p>

<form action="process.php" action='POST'>
    <input type="checkbox" name="advertise[]" value="search engine"><label>Search Engine</label><br>
    <input type="checkbox" name="advertise[]" value="newsletter"><label>Newsletter</label><br>
    <input type="checkbox" name="advertise[]" value="yellow pages"><label>Yellow Pages</label><br>
    <input type="checkbox" name="advertise[]" value="online ads"><label>Online Ads</label><br>
    <input type="submit" value="Submit">
</form>

feedback form example

The form above might look similar to other feedback forms if you’ve ever purchased anything online before. It prompts the user to choose how they found a particular web site. The user can make multiple selections depending on the advertisement that influenced their decision.

<?php

if (isset($_POST['advertise'])) {
	echo "You were referred to us by the following:";
	echo "<br>";

	foreach ($_POST['advertise'] AS $advertise) {
		echo "$advertise";
		echo "<br>";
	}

echo "Thank you for your feedback!";
}
?>

The user will get the following response if they selected “yellow pages” and “newsletter”:

You were referred to us by the following:
newsletter
yellow pages
Thank you for your feedback!

An important thing to remember is that form elements with the same name that can allow users to select multiple values for each form field, need to be passed as arrays instead of regular variables.

  • Share/Bookmark

Comments are currently disabled, but you can trackback from your own site.