Results 1 to 6 of 6

Thread: The value of a group of checkboxes

  1. #1
    Join Date
    Apr 2006
    Posts
    205
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Smile The value of a group of checkboxes

    Hello everybody,

    I've made a form that contains a lot of checkboxes. The idea is that users can check the boxes of the products they are interested in and send the form.

    I've given the name 'artwork' to all of the checkboxes and changed the value depending on which artwork it is.

    The problem I've come across is that when multiple checkboxes are checked only the value of the last one gets mentioned in the email. I'm looking for a way of sending the values of all the checkboxes that are checked, without having to create a new name for each checkbox. This is because new products are going to be continually added to the form and I don't want to have to keep adding to the php of the form sender.

    The form looks a little like this:
    HTML Code:
    <div id="purchaseForm">
       <form action="sender2.php" name="purchase" id="purchase" method="post">
        <div id="formPart1"><table cellspacing="0">
         <tr id="titleBar">
          <td id="itemDescription">Item Description</td>
          <td id="itemType">Item Type</td>
          <td id="price" colspan="2">Price</td>
         </tr>
         
         <tr>
          <td>Product 1</td>
          <td>Lazer Print</td>
          <td>$10</td>
          <td><input type="checkbox" name="artwork"  value="07-late-01" /></td>
         </tr>
         
         <tr>
          <td>Product 2</td>
          <td>Lazer Print</td>
          <td>$10</td>
          <td><input type="checkbox" name="artwork"  value="07-late-02" /></td>
         </tr>
         
         <tr>
          <td>Product 3</td>
          <td>Lazer Print</td>
          <td>$10</td>
          <td><input type="checkbox" name="artwork"  value="07-late-03" /></td>
         </tr>
         
         <tr>
          <td>Product 4</td>
          <td>Lazer Print</td>
          <td>$10</td>
          <td><input type="checkbox" name="artwork"  value="07-late-04" /></td>
         </tr>
         
        </table></div><!-- end of formPart1 -->
        
     <!-- ...the rest of the form, etc. ... -->
        
       </form>
    And the form-sender looks like this:

    PHP Code:
    <?

    $host  
    $_SERVER['HTTP_HOST'];
    $uri  rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
    $extra "result.php?sent=yes";
    $name $_POST['name'];
    $email $_POST['email'];
    $address $_POST['address'];
    $artwork$_POST['artwork'];
    $mail $_POST['mailing'];

       
    $to "monkeyzbox@dc5b.com";   
       
    $subject "Purchase";   
       
    $body "Name: ".$name."\r\n\r\n";
       
    $body .= "Email: ".$email."\r\n\r\n"
       
    $body .= "Delivery Address: ".$address."\r\n\r\n"
       
    $body .= "Artwork selected: ".$artwork."\r\n\r\n";      
       
    $from "dog@dc5b.com";

      
    mail($to$subject$body$from);
      
    header("Location: http://$host$uri/$extra");
      exit;
        
    ?>
    If anyone has any ideas please let me know.

    Thanks,

    Dog

  2. #2
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    Try changing the name of the checkboxes to artwork[] (that way it makes it an array), then change your php to something like the following:

    Code:
    <?php
    
    $host  = $_SERVER['HTTP_HOST'];
    $uri  = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
    $extra = "result.php?sent=yes";
    $name = $_POST['name'];
    $email = $_POST['email'];
    $address = $_POST['address'];
    $artwork = $_POST['artwork'];
    $mail = $_POST['mailing'];
    
       $to = "monkeyzbox@dc5b.com";   
       $subject = "Purchase";   
       $body = "Name: ".$name."\r\n\r\n";
       $body .= "Email: ".$email."\r\n\r\n"; 
       $body .= "Delivery Address: ".$address."\r\n\r\n"; 
       $body .= "Artwork selected: \r\n".
    
     foreach ($artwork as $selected) {
       $body .= " > ".$selected."\r\n";      
     }
    
       $from = "dog@dc5b.com";
    
      mail($to, $subject, $body, $from);
      header("Location: http://$host$uri/$extra");
      exit;
        
    ?>
    Hope this helps.
    Last edited by thetestingsite; 04-02-2008 at 11:59 PM. Reason: corrected error with foreach loop.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  3. #3
    Join Date
    Apr 2006
    Posts
    205
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Smile

    Hey man,

    Thanks a lot. It looks like that's nearly working.

    What happened is that in the email I received:

    Code:
    Artwork selected:
     > Array
     > Array
     > Array
    Please excuse me, I don't have any more ideas but hope that someone else does.

    Dog

  4. #4
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    Sorry, the php code should be this:

    Code:
    <?php
    
    $host  = $_SERVER['HTTP_HOST'];
    $uri  = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
    $extra = "result.php?sent=yes";
    $name = $_POST['name'];
    $email = $_POST['email'];
    $address = $_POST['address'];
    $artwork = $_POST['artwork'];
    $mail = $_POST['mailing'];
    
       $to = "monkeyzbox@dc5b.com";   
       $subject = "Purchase";   
       $body = "Name: ".$name."\r\n\r\n";
       $body .= "Email: ".$email."\r\n\r\n"; 
       $body .= "Delivery Address: ".$address."\r\n\r\n"; 
       $body .= "Artwork selected: \r\n".
    
     foreach ($artwork as $selected) {
       $body .= " > ".$selected."\r\n";      
     }
    
       $from = "dog@dc5b.com";
    
      mail($to, $subject, $body, $from);
      header("Location: http://$host$uri/$extra");
      exit;
        
    ?>
    Hope this helps.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  5. The Following User Says Thank You to thetestingsite For This Useful Post:

    dog (04-03-2008)

  6. #5
    Join Date
    Apr 2006
    Posts
    205
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Thumbs up

    Thanks once again!

    That works perfectly.

    Thanks!

  7. #6
    Join Date
    Apr 2006
    Posts
    205
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Red face

    Hello everybody. Hello thetestingsite,

    Sorry to bring an old thread back up but a new problem has come to light.

    In order for the PHP form sender to work I ended up changing the name of my checkboxes.

    Quote Originally Posted by thetestingsite View Post
    Try changing the name of the checkboxes to artwork[] (that way it makes it an array)
    Now I've got a problem with the javascript form validation.

    I'd like to use something like this:

    Code:
    var checkboxes =""
    	for(var i=0; i < document.purchase.artwork.length; i++){
    		if(document.purchase.artwork[i].checked)
    		checkboxes +=document.purchase.artowork[i].value + "\n"
    	}
    
    	if(checkboxes=="") {
    	 alert("Please check the box beside the artwork you would like to purchase.");
    		return false;
    	}
    But the square brackets in the name complicate things.

    Any advise would be much appriciated.

    Thanks,

    Dog

    PS Re. forum etiquette. If I should have posted this as a new thread in the javascript forum please let me know.

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
  •