Check Boxes Radio Buttons

Hello all!

I am in dire need here. I have been banging my head for days now trying to complete this project and now that I have turned it in I can ask for help!

I have a form on a web page that has check boxes. Professor wanted us to echo to the next page. No DB connection. It was a final. Now before you tell me I should use a different code let me be clear that the PHP code is the way he wanted it done.

I can not get my echo to tell me which boxes were checked? I also get the address echo in the airport line? I have attempted different types of PHP but none will echo the information correctly!

Can anyone help???

Thanks!

Here is my html code:

<td class="auto-style9" valign="top">Type of Clinic:&nbsp;</td> <td class="auto-style8" valign="top"> <input name="clinic[]" type="checkbox" value="colorguard" />&nbsp;Color Guard<br /> <input name="clinic[]" type="checkbox" value="winterguard" />&nbsp;Winter Guard<br /> <input name="clinic[]" type="checkbox" value="percussion" />&nbsp;Percussion / Drum Line<br /> <input name="clinic[]" type="checkbox" value="drummajors" />&nbsp;Drum Majors<br /> <input name="clinic[]" type="checkbox" value="leadership" />&nbsp;Leadership<br /> <input name="clinic[]" type="checkbox" value="marchingtech" />&nbsp;Marching Technique<br /> <input name="clinic[]" type="checkbox" value="consultation" />&nbsp;Consultation</td> </tr> <tr>
Here is my corresponding PHP code:
[php]

<?php $name = $_POST['name']; $title = $_POST['title']; $school = $_POST['school']; $address = $_POST['address']; $city = $_POST['city']; $state = $_POST['state']; $zip = $_POST['zip']; $airport = $_POST['airport']; $miles = $_POST['miles']; $email = $_POST['email']; $phone = $_POST['phone']; $clinic = $_POST['clinic']; $length = $_POST['length']; $dates = $_POST['dates']; $students = $_POST['students']; $heard = $_POST['heard']; $comments = $_POST['comments']; echo "

Camp Request Form Confirmation!

"; echo "

Name: ".$name."
"; echo "Title: ".$title."
"; echo "School: ".$school."
"; echo "Address: ".$address."
"; echo "City: ".$city.", State: ".$state." Zip Code: ".$zip."
"; echo "Airport: ".$airport."
"; echo "Miles Away: ".$miles."
"; echo "E-mail: ".$email."
"; echo "Phone Number: ".$phone."
"; echo "Type of Clinic: ".$clinic."
"; echo "Number of Students: ".$students."
"; echo "Heard about BtD from: ".$heard."
"; echo "

Comments: ".$comments."

"; ?> [/php]

since $clinic is array try output like

[php]echo “

” . print_r($clinic, 1) . “
”[/php]

or

[php]echo $clinic[0];[/php]

for first array item value.

This may not be exactly what you are looking for, since I am pure noob, but nonetheless, try out this.

PAGE WITH THE CHECKS AND RADIOS:

[code]

[/code]

Now on to page 2, which would be called result.php in this example

[php]<?php
$rad1 = $_POST[‘radio1’];
$rad2 = $_POST[‘radio2’];

echo "You have selected: " . $rad1 . " " . $rad2;
?>
[/php]

I actually highly doubt this is working, so please nobody hate on me. I am a young coder (hence the Mini in my name) and I’m still in training, and hibernated from coding for a while

Okay maybe this helps, only picked your part of clinic because Sarthak already hinted this was an array:
used this as this to echo the $clinic array back to the screen.
this is the HTML I used to test it:

<table>
<form action="processhelp.php" method="post">
	<tr >Type of Clinic:&nbsp;<br/>
		<td>
			<input name="clinic[]" type="checkbox" value="colorguard" />&nbsp;Color Guard<br />
			<input name="clinic[]" type="checkbox" value="winterguard" />&nbsp;Winter Guard<br />
			<input name="clinic[]" type="checkbox" value="percussion" />&nbsp;Percussion / Drum Line<br />
			<input name="clinic[]" type="checkbox" value="drummajors" />&nbsp;Drum Majors<br />
			<input name="clinic[]" type="checkbox" value="leadership" />&nbsp;Leadership<br />
			<input name="clinic[]" type="checkbox" value="marchingtech" />&nbsp;Marching Technique<br />
			<input name="clinic[]" type="checkbox" value="consultation" />&nbsp;Consultation
		</td>
	</tr>
	<tr>
		<td>
			<input type="submit" value="submit">					
		</td>
	</tr>
</form>
</table>

the code below is in the file processhelp.php

[php]

<?php $clinic = $_POST['clinic']; foreach ($clinic as $key => $value) { echo $value ."
\n"; } ?>

[/php]

This will print out the checked box values.

Best regards, Jelger

Preface: less than a week into learning php; question asked not as a challenge but for clarification of something I might be (probably am) missing.

Jelger, thank you for your response, I have been playing with that code. I added a “

print_r($_POST)
” to help understand what is going on. My question is:

You wrote:
[php]
foreach ($clinic as $key => $value) {
echo $value ."
\n";
}
[/php]

And I used that, and then altered it:
[php]
foreach ($clinic as $x) {
echo “$x
\n”;
}
[/php]
…and it worked in the same way so I wondered why you used the “$key=>$value”?

And an intriguing side “discovery” I don’t quite follow:
[php]
foreach ($clinic as $key=>$x) {
echo “$value
\n”;
}
[/php]
…I didn’t expect that to produce any results because “$value” wasn’t stipulated in the foreach, but it does. The results are the last value selected, and that value is repeated for however many selections were made. Given the form you provided, if the first 4 are selected then “drummajors” is printed 4 times. I don’t understand this; it’s like the foreach is only cycling through the last selection, but doing it as many times as the number of selections made.

I’m more interested in the first question; the second question/comment is just simple, idle curiosity.
Thanks, -R.

Well, to understand arrays in PHP is easy. A standard array is a list of values. The index or KEY to the array is a number starting at 0. (ZERO) So, in the sample in this post, the field used the [] to indicate it’s an array.

SO:  Color Guard

Creates an entry clinic[0], then, the next input creates an entry clinic[1], etc…

Since these are placed into the the $_POST global variable, the entire array is pulled as one item in this manor:
$checkboxes = $_POST[‘clinic’];
This turns the variable $checkboxes into a PHP array. Note that each entry in the array is actually in this format and will explain the key/value that was suggested… $key=>$value The first entry which was the one above would be: 0 => “colorguard” (ONLY if that checkbox was checked!)
So, to actually use these values, you would go thru the array and do something with the values. Most people would list them in order and numbered. Like:
Options selected:
1 - colorguard
2 - someotheroption
3 - etc…
To to that, you would use a foreach clause and loop thru displaying the values. Sometimes this data is stored in a database and returned as needed. If you only need the values themselves, you can do it without using the KEY. Since in this case it it just a number of a checkbox, it is not important to use.
This “key” format is useful when the checkboxes are lists pulled from a database. The “key” can actually be the table’s ID col and then can be used as an index to pull further data from the DB.
Sometimes you need to find a certain group of key values. SO, you could create a key that is 1,2,3,4,5 for one group of fields and 101,102,103 for others, etc. Then, you could use these in special ways. If the key is >100, then it is a special group and can be handled differently than the others. Lots of possible uses for this type of coding. But, usually only in advanced applications.

Now, I am not sure if I explained it enough or went overboard… Just remember $_POST’s and $_GET’s and even $_FILES’s are all plain arrays. Single-dimensional and easy to use. Good luck…

Thanks, ErnieAlex, makes sense.

I did note, via print_r($_POST), that a cool feature of not defining the array check boxes, ie not doing this:
[php]

[/php]
…is that whatever is selected gets sent to the $_POST array, starting at a fresh index even if, say, the first selection is the fifth item in the list. And, only items selected get put into the post-array (not the post array, as I know(?) several arrays can be in POST.) As you, Ernie, allude to in your comments.

(Note: as I write this I don’t know if “clinic[0]” coding works, I just assume it does; easy enough to test.)

…and I’ll take your luck freely given! Thanks! Now to go roll the bones, er, practice coding…

Yes, all checkboxes are NOT sent in the $_POST array. ONLY the ones that have been checked.
I seem to usually do it the old fashioned way and give each one a unique name and check for it.

But, it is much easier to use the array if you have a long list of options. For, instance, if it was a blog
and you wanted to list all recent notes and allow the user to select a bunch to view, then you would
not want to use a unique name for each. Arrays can be handy once you understand how to use them…

Now, next time, we will cover Multidimensional-Arrays… LOL…

Sponsor our Newsletter | Privacy Policy | Terms of Service