...

View Full Version : Checkbox dosn't output the correct information on my phpform



7Gte
03-10-2007, 11:37 AM
something is not right with my code for the php form, I don't know what I'm doing wrong, but everytime a user checks mutliple fields in the checkboxes, the output would only read as: "Array"

Can anyone tell me whats wrong with it?

here is the code I have for the form.php



<form action="results.php" method="post">
First Name: <input type="text" name="firstname" />
<p>
Last Name: <input type="text" name="lastname" />
</p>
<p>
What kind of music do you like?
<br />
<input type="checkbox" name="music[]" value="metal" />Metal
<br />
<input type="checkbox" name="music[]" value="punk" />Punk
<br />
<input type="checkbox" name="music[]" value="hiphop" />Hip Hop
<br />
<input type="checkbox" name="music[]" value="jazz" />Jazz
<br />
<input type="checkbox" name="music[]" value="blues" />Blues
<br />



and here is the code I have for the results.php file:



<?php
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$music = $_POST['music'];

echo "firstname: ". $firstname . "<p>";
echo "</p>lastname: " . $lastname . "<p>";
echo "</p>you like: " . $music . "<p>";

?>


can anyone help? thanks

chump2877
03-10-2007, 11:48 AM
$_POST['music] is an array, so you need to iterate through it to get at the values:


<?php
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$music = $_POST['music'];

echo "<p>firstname: ". $firstname . "</p>";
echo "<p>lastname: " . $lastname . "</p>";

echo "<p>you like: ";
foreach ($music as $k => $v)
{
echo $v;
if ($k != count($music) - 1)
echo ", ";
}
echo "</p>";

?>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum