Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Jul 2006
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Checkbox dosn't output the correct information on my phpform

    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

    Code:
    <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 Code:
    <?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

  • #2
    Senior Coder chump2877's Avatar
    Join Date
    Dec 2004
    Location
    the U.S. of freakin' A.
    Posts
    2,778
    Thanks
    19
    Thanked 155 Times in 146 Posts
    $_POST['music] is an array, so you need to iterate through it to get at the values:

    PHP Code:
    <?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>";

    ?>
    Regards, R.J.

    ---------------------------------------------------------

    Help spread the word! Like my YouTube-to-Mp3 Conversion Script on Facebook !! :)
    [Related videos and tutorials are also available at my YouTube channel and on Dailymotion]
    Get free updates about new software version releases, features, and bug fixes!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •