SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Thread: checkboxes?

  1. #1
    SitePoint Wizard lukeurtnowski's Avatar
    Join Date
    Mar 2003
    Location
    Coronado
    Posts
    1,656
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    checkboxes?

    I have a form with checkboxes
    HTML Code:
    <input type="checkbox" name="amenities[]" value="0" id="amen">Washer/Dryer
    <input type="checkbox" name="amenities[]" value="1">Kitchen Appliances
    <input type="checkbox" name="amenities[]" value="2">Patio
    <input type="checkbox" name="amenities[]" value="3">BBQ Grill
    <input type="checkbox" name="amenities[]" value="4">Balcony
    <input type="checkbox" name="amenities[]" value="5">Garage
    I get an array back.
    How do I then send an email of those values in a form, so that the checkboxes which have been checked are also checked (Here is what im thinking
    PHP Code:
    $message .= "<input type='checkbox' name='amenities[]' value='0' //if selected echo "checcked";>Washer/Dryer....."
    thanks
    "Oh, and Jenkins--apparently your mother died this morning."

  2. #2
    SitePoint Enthusiast
    Join Date
    Jan 2013
    Posts
    31
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    you can create function like this

    Code:
    function checked($amenities,$value)
    	{
    		if (is_array($amenities) && count($amenities) > 0)
    		{
    			if (in_array($value,$amenities))
    				return 'checked="checked"';
    			else
    				return '';
    		}
    	}
    and use it like this

    Code:
    $message = '<input type="checkbox" name="amenities[]" value="0" id="amen" '.checked($_POST['amenities'],0).'>';
    $message .= '<input type="checkbox" name="amenities[]" value="0" id="amen" '.checked($_POST['amenities'],1).'>';
    .........................

  3. #3
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    A more DRY approach might be to use arrays a bit more efficiently, and at the same time create a more reusable functions.

    Try and maintain amenities in a single place (maybe even get this from a db table)

    Code:
    "select id, type from amenities order by type"
    For now use an array though ...

    Code PHP:
    // this could be in an include file ameneties.php
    $amenities = array(
      'BBQ'
    , 'Garage'
    , 'Balcony'
    , 'Patio'
    );



    Code PHP:
    // this could be in an include file outputascheckboxes.php
    function outputAsCheckboxes($name, $array, $selected=NULL){
     
    $html = '';
     
        foreach($array as $k => $v){
        $html .= '<input type="checkbox" name="'. $name . '[]" value="'.  $k ;
     
        if($selected && in_array($k, $selected)){
             $html .= ' checked = "checked" ';
        }
     
        $html .= '">' . $v .'<br />' . PHP_EOL;
        }
     
    return $html;
    }

    Then you can use the same code in your web form and in your email generator

    Code PHP:
    include 'outputascheckboxes.php';
    include 'amenities.php';
     
    // these are the selected items
    $selected = array(1,2);
     
    $message .= outputAsCheckboxes('ameneties', $amenities, $selected);

    Gives:
    Code:
    <input type="checkbox" name="ameneties[]" value="0">BBQ<br />
    <input type="checkbox" name="ameneties[]" value="1 checked = "checked" ">Garage<br />
    <input type="checkbox" name="ameneties[]" value="2 checked = "checked" ">Balcony<br />
    <input type="checkbox" name="ameneties[]" value="3">Patio<br />
    On your webform you can then do:

    Code PHP:
    echo outputAsCheckboxes('ameneties', $amenities);
    // no third argument, so show all checkboxes unticked.

    Gives
    Code:
    <input type="checkbox" name="ameneties[]" value="0">BBQ<br />
    <input type="checkbox" name="ameneties[]" value="1">Garage<br />
    <input type="checkbox" name="ameneties[]" value="2">Balcony<br />
    <input type="checkbox" name="ameneties[]" value="3">Patio<br />
    Doing something similar to the above means you can then:

    • Maintain your array in a single place, so its easier to maintain
    • Show all selections unchecked on a webpage
    • Show selections already made in a webpage
    • Show selections already made in an email message
    • Reuse the function outputAsCheckboxes() in other projects


    There are drawbacks, for example to start adding css types to your checkbox you could add more parameters and so on, but for now I hope this gives you some ideas.
    Last edited by Cups; Oct 12, 2013 at 12:19. Reason: added link to wikipedia, showed output for webpage

  4. #4
    SitePoint Wizard lukeurtnowski's Avatar
    Join Date
    Mar 2003
    Location
    Coronado
    Posts
    1,656
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    thanks, it works great!
    "Oh, and Jenkins--apparently your mother died this morning."


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
  •