working with checkboxes

Hello to everyone,

This is probably very simple but I can’t figure it for the life of me. All I want is for the marked boxes to come through and all I keep getting is the last one. I am about ready to take a hammer to my personal confuser.

[code]

Let’s Get Personal…



  • Name



  • Email

  </ul>
</fieldset>
<fieldset class="study">
  <legend>Favorite Type(s) of Food</legend>
  <ul>
    <li>
      <label for="cuisine" class="text"> You can choose more than one by holding the &quot;ctrl&quot; button.</label>
      <select name="cuisine[]" multiple id="cuisine" tabindex="60">
        <option value="american">American</option>
        <option value="bakery_deli">Bakery/Deli</option>
        <option value="bbq">Barbeque</option>
        <option value="chinese">Chinese</option>
        <option value="coffee">Coffeeshop</option>
        <option value="greek">Greek</option>
        <option value="italian">Italian</option>
        <option value="latin">Latin</option>
        <option value="pizza">Pizza</option>
        <option value="portuguese">Portuguese</option>
        <option value="scottish-irish">Scottish/Irish</option>
        <option value="spanish">Spanish</option>
        </select>
    </li>
    <li class="group">If you would like information regarding any of the topics below, please check the one(s) that apply:</li>
    <li>
      <input name="info[]" type="checkbox" id="ssl" tabindex="70" value="ssl">
      <label for="ssl">Student Specials</label><br>
      <input name="info[]" type="checkbox" id="ads" tabindex="80" value="ads">
      Advertising
      <br>
      <input name="info[]" type="checkbox" id="spotlight" tabindex="90" value="spotlight">
      <label for="spotlight">Restaurant Spotlight</label><br>
      <input name="info[]" type="checkbox" id="general" tabindex="100" value="general">
      <label for="general">General Listing</label>
    </li>
    <li class="group">Would you like to join our email list?</li>
    <li>
        <label>
          <input type="radio" name="list" value="yes" id="list_0" tabindex="110">
          yes</label><br>
        <label>
          <input type="radio" name="list" value="no" id="list_1" tabindex="120">
          not at this time</label>
    </li>
    <li>
      <label for="comments" class="text">Additional comments</label>
      <textarea name="comments" id="comments" tabindex="130"></textarea>
    </li>
    <li>
      <input type="submit" name="submit" id="submit" value="Request Info" tabindex="140">
    </li>
  </ul>
</fieldset>
[/code]

Here is the php
[php]<?php

$emailSubject = ‘Information Request’;
$webMaster = ‘[email protected]’;

$name = $_POST[‘name’];
$email = $_POST[‘email’];
if($_POST[‘cuisine’]!=null){
foreach ($_POST[‘cuisine’] as $key => $value) {
$cuisine.=($key==0)?$value:",".$value;
$info = $_POST[‘info’];
$list = $_POST[‘list’];
}
}

$comments = $_POST[‘comments’];

$body = "
Name: $name
Email: $email
Type of Food: $cuisine
Info: $info
Subscribe: $list
Comments: $comments
EOD";

$headers = “From: $email\r\n”;
$headers .= “Content=tpye: text/html\r\n”;

$success = mail($webMaster, $emailSubject, $body, $headers);

header(‘Location: recommendConfirm.htm’);

?>[/php]

Like I said, everything works except for the checkboxes.
Thank you very much in advance for any help.

name=“list” value=“yes” id=“list_0”
name=“list” value=“no” id=“list_1”

try changing the name as well, give them each a unique name. and then try it. least eliminates the chance of it being the issue

list has to do with the radio buttons whichare working fine. It’s the checkboxes that I’m having trouble with.

Hi Zildjyn,

This is the correct behaviour. Instead of using the post method, try submitting your form through get method. Then, look at the url. But before that, lose the square brackets. You don’t need them. I meant:

change this: <input name=“info[]”… to <input name=“info”…

Let’s say you’ve checked the first 2 checkboxes, which is ssl and ads and and then you click submit. In the query string, you should be able to see something like this: info=ssl&info=ads because the field is the same, the $_POST[‘info’] will get the last value, which is ads. If you think the id is used as the field for the query string, then you are wrong. Query string uses the name attribute, not id.

There are a few ways you can handle this. First, change all your checkboxes name to the same value as id(it didn’t have to be, as long as each checkboxes is unique). Then in the php file, you have to add each checkboxes with corresponding $_POST variables. This method is not so elegant but its easier.

Another way is to have a javascript function that adds the value of the checked checkbox to a new input hidden type that has the name info everytime the checkbox is clicked. You don’t have to make changes to the php file using this way.

Regards,
developer.dex2908

Works like a charm. You rock. Thank you for eveything.

Hi Zildjyn,

Glad i could help… ;D

Regards,
developer.dex2908

Sponsor our Newsletter | Privacy Policy | Terms of Service