SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Guru
    Join Date
    Oct 2004
    Location
    uk
    Posts
    853
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    multiple checkboxes

    Hi guys

    What I want is

    to have say 20 checkboxes

    if a user selects 1 check box the result goes into the field location if another checkbox is selected then that would go into location 1 if another was also checked it would go into location2

    How can I do this?


    PHP Code:
    Avon:<input type="checkbox" value="avon" name="location">:<br />
    Bedfordshire:<input type="checkbox" value="Bedfordshire" name="location">:<br />
    Buckinghamshire:<input type="checkbox" value="Buckinghamshire" name="location">:<br />
    Cambridgeshire:<input type="checkbox" value="Cambridgeshire" name="location">:<br />
    Cheshire:<input type="checkbox" value="Cheshire" name="location">:<br /> 

  2. #2
    Worship the Krome kromey's Avatar
    Join Date
    Sep 2006
    Location
    Fairbanks, AK
    Posts
    1,621
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    name="location[]"
    PHP questions? RTFM
    MySQL questions? RTFM

  3. #3
    SitePoint Guru
    Join Date
    Oct 2004
    Location
    uk
    Posts
    853
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    what will that do?

  4. #4
    Worship the Krome kromey's Avatar
    Join Date
    Sep 2006
    Location
    Fairbanks, AK
    Posts
    1,621
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Give you an array $_POST['location'] that will have as its contents the values of each checked checkbox.

    E.g. using your list above, if I check Avon, Cambridgeshire, and Cheshire, then $_POST['location'] would look like:
    $_POST['location'][0] = "avon"
    $_POST['location'][1] = "Cambridgeshire"
    $_POST['location'][2] = "Cheshire"

    The 'name="location[]"' that I posted above is meant to replace the 'name="location"' in your original code, just in case that wasn't clear.
    PHP questions? RTFM
    MySQL questions? RTFM

  5. #5
    SitePoint Guru
    Join Date
    Oct 2004
    Location
    uk
    Posts
    853
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    but how would this be entered into the database?

  6. #6
    SitePoint Evangelist superuser2's Avatar
    Join Date
    Aug 2006
    Posts
    598
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You'd have to implode it.

  7. #7
    SitePoint Enthusiast thenk83's Avatar
    Join Date
    Sep 2005
    Location
    Hacienda Heights, CA
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could use a for loop. Which would probably work fawlessly. But you would need to count the number of locations so the loop would know how many times to loop... otherwise it would loop forever or loop once.


    for($x=0;$x<$number_of_locations;$x++){
    $sql = "insert into.....";
    $query = mysql_query.....;
    }

  8. #8
    SitePoint Enthusiast thenk83's Avatar
    Join Date
    Sep 2005
    Location
    Hacienda Heights, CA
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry! My error.. lets try this! The for loop is more work. Foreach is even better!

    Code:
    <form action="loc.php" method="POST">
    Avon:<input type="checkbox" value="avon" name="location[]">:<br />
    Bedfordshire:<input type="checkbox" value="Bedfordshire" name="location[]">:<br />
    Buckinghamshire:<input type="checkbox" value="Buckinghamshire" name="location[]">:<br />
    Cambridgeshire:<input type="checkbox" value="Cambridgeshire" name="location[]">:<br />
    Cheshire:<input type="checkbox" value="Cheshire" name="location[]">:<br /> 
    <input type="submit" value="Go">
    </form>
    <?
    foreach($_POST['location'] as $key => $value){
    	echo 'ARRAY ID: '.$key.' CHECKBOX VALUE: '.$value.'<br />';
    }
    
    ?>
    You replace the echo line within the foreach loop with the SQL string. If you don't need the $key just remove the => $value. This will give you just the names. Let me know how that goes!


Bookmarks

Posting Permissions

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