SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Dec 2004
    Location
    Toronto
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP forms (post) and checkboxes

    Alright, I've been mucking around with checkboxes and that have become the bane of my existence because of how tedious they can be when posting a form. I have been trying to find work arounds without having to rename each checkbox. Here is some code:

    HTML Code:
    <label><input name="q[15]" id="q[15]" type="checkbox" value="Ontario" />Ontario</label>
      <label><input name="q[15]" id="q[15]" type="checkbox" value="Quebec" />Qu&eacute;bec</label>
      <label><input name="q[15]" id="q[15]" type="checkbox" value="Autre Prov" />Autre province, laquelle?</label>
      <label><input name="q[15]" id="q[15]" type="checkbox" value="Ontario" />Ext&eacute;rieur du Canada, (dans quel pays?)</label>
    I'm trying to use an array. Usually when I GET instead of POST to submit the form, I can parse the QUERY_REQUEST $_SERVER variable ($_SERVER['QUERY_REQUEST']. In the case of POST, I cannot. The array (in this case q[15] will contain the last value of the checkbox. What I would like is the variable to contain the checkbox values separated by commas.

    For parsing the QUERY_REQUEST, I've been using the following code successfuly:
    PHP Code:
    function query2string($string$var='q[15]') {    
        if (!empty(
    $string)) {
            
    $begin strpos($string$var."=");
            
    $length strlen($string);
            
    // Check to ensure no unwanted processing
            
    if ( ($begin $length) && (!empty($length)) ) {
                
    //echo $begin."-".$length." $var - $string";

                // Remove extra data at beginning of string
                
    for ($i $begin$i<=$length$i++) $string2 .= $string[$i];
                
    // Make string into an array
                
    $a explode("&".$var."="$string2);
                
    // Add items to a string to be returned
                
    foreach ($a as $y$ans .= $y.", ";
                
    // Find position of last number
                
    $ans str_replace($var."=","",$ans);
                
    $end strpos($ans"&");
                
    $string NULL;
                
    // Remove all non-required data
                
    for ($i=0$i<=$end-1$i++) $string .= $ans[$i];
                return(
    $string);
            } else return(
    NULL); 
        }

    The function defaults to q[15] in this case.

    The only solution I have really found is using a separate array for each checkbox or different name for each checkbox. Any other suggestions?

  2. #2
    reads the ********* Crier silver trophybronze trophy longneck's Avatar
    Join Date
    Feb 2004
    Location
    Tampa, FL (US)
    Posts
    9,854
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    copy and paste this in to a PHP file. you'll see that by adding another dimension, you can get the values.
    PHP Code:
    <form>
    <label><input name="q[15][]" id="q[]" type="checkbox" value="Ontario" />Ontario</label>
    <label><input name="q[15][]" id="q[]" type="checkbox" value="Quebec" />Qu&eacute;bec</label>
    <label><input name="q[15][]" id="q[]" type="checkbox" value="Autre Prov" />Autre province, laquelle?</label>
    <label><input name="q[15][]" id="q[]" type="checkbox" value="Ontario" />Ext&eacute;rieur du Canada, (dans quel pays?)</label>
    <input type="submit" />
    </form>
    <pre>
    <?php
    print_r
    ($_REQUEST);
    ?>
    </pre>
    notice the extra set of brackets after q[15]

  3. #3
    SitePoint Guru
    Join Date
    Jul 2005
    Location
    Orlando
    Posts
    634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    longneck is correct.

    So, to expand upon that and put which ones were selected into a comma sep. string:

    Code:
    $csv = implode( ',', $_POST['q'][15] );


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
  •