-1

I apologize if it is stupid question. I wanted to check whether one or more checkboxes are not empty, then can process the form and print interest text for email.

HTML/PHP

<p><label for="commentsText">I am interested in:</label><br/>
<input type="checkbox" name="interest[]" id="interest" value="1" class="required requiredField email" /> 
    Exhibiting. Please send me more information<br/>
<input type="checkbox" name="interest[]" id="interest" value="2" class="required requiredField email" /> 
    Visiting. Please add me to your mailing list<br/>
<input type="checkbox" name="interest[]" id="interest" value="3" class="required requiredField email" /> 
    Speaking. Please send me more information<br/>
<input type="checkbox" name="interest[]" id="interest" value="4" class="required requiredField email" /> 
    Attending. Please send me more information<br/>

<?php if($interestError != '') { ?>
<span class="error"><?php echo $interestError; ?></span>
<?php } ?>
</p>

PHP

$interest='';    
if(empty($_POST[$interest]))
{ 
    $interestError ='Please select the interests';
    $hasError = true;
}
else{
    $numberofInterest = count($interest);
    for ($i=0; $i < $numberofInterest; $i++)
    {
        $numofInterest = $interest[i];
        echo $numofInterest . " ";
    }
}

EDIT #2

Thank you everybody for the help. I used print_r and saw all four are printed. The problem is now: if no error, send mail. It doesn't show all when I checked all checkboxes. Only displays '4'. Is there a way to display all including texts?

if(!isset($hasError)) {

        $interest = $numofInterest ;

    $subject = 'I Have A Question to Ask from '.$name;
            $thanksubject = 'Thank you for the Form Submission';
            $body = "Name: $name \n\nEmail: $email \n\nInterest: $interest\n\nComments: $comments";

*EDIT #3*

OK I have solved that last one above. managed to send email with everything.

joe
  • 1,115
  • 5
  • 21
  • 50
  • you can check this in javascript before post the form, like http://stackoverflow.com/questions/2684434/jquery-check-if-atleast-one-checkbox-is-checked – Haim Evgi Mar 28 '12 at 06:31

2 Answers2

0

let's do a print_r($_POST); and discover what's happening. You'll learn how data is passed.. and how ti solve similar problems in future.

kappa
  • 1,559
  • 8
  • 19
0

When the form is submitted the PHP script will receive an array of the checked boxes in the $_GET or $_POST variable respectively (or you could just use $_REQUEST)

I.e. when the first and third checkbox is checked, a print_r($_POST['interest']); will output:

Array
(
    [0] => 1
    [1] => 3
)

I think your main mistake, is the way you index the $_POST-variable - you use the variable $interest where you should have used the string 'interest', like above.

Flygenring
  • 3,818
  • 1
  • 32
  • 39