0

I'm asking this question in regards to my friend, so I do not have code samples to post here at the moment. Hopefully I'm clear enough that someone can help.

So he has a simple contact form except it has multiple checkboxes that the user can choose to send their requests to multiple recipients... like so...


x I would like to know about flight school
x I'm interested in becoming a teacher
x I would like someone to contact me about your degrees

Name
Email
Comments


And so based on which checkboxes are selected it should add that recipient to the email function so that they receive the users comments and interest.

The form is validated by jquery and uses the $.ajax function to POST the Name, Email and Comments fields over to a process.php... we are validating that at least one of the checkboxes is selected, however, we haven't been able to figure out how to pass its boolean value to the process.php and in-turn add the relevant email address to the mail() function.

I do realize this is semi-vague without posting our code, but I don't have access to it right now... and I have been searching google for about 30 minutes trying to find something to work with. Any help would be appreciated. Thanks.

4 Answers 4

0

you can simply check if the value you got is true or not:

basic idea :

if(checkbox-1-ischecked)
    //send email to first recipent
end if
if(checkbox-2-ischecked)
    //send email to 2nd recipent
end if
if(checkbox-3-ischecked)
    //send email to 3rd recipent
end if
if(checkbox-4-ischecked)
    //send email to 4th recipent
end if

Etc

Sign up to request clarification or add additional context in comments.

Comments

0

This would seem to answer you check box query. (http://stackoverflow.com/questions/908708/how-to-pass-multiple-checkboxes-using-jquery-ajax-post)

In basic terms it would post an array back to the php script which you could then parse and depending what was ticked / the vars passed back you could then append more email addresses to the 'to' part of the mail function.

For a simplier implimentation you could just keep your three check boxs seperate not in an array and ajax post them back individually. HTML

<input type='checkbox' name='flight' value='1' id='flight' />
<input type='checkbox' name='teacher' value='1' id='teacher' />

Then simply on the server via PHP

$to="";
if($_POST['teacher'] == 1) {$to = $to."[email protected],"};//append email
if($_POST['flight'] == 1) {$to = $to."[email protected],"};//append email
$to = rtrim($to, ","); //remove trailing comma

NOTE as with all web to mail scripts make sure you sanitize all vars to prevent spam abuse!

Comments

0

Name your elements as an array like this:

<input type="checkbox" name="mybox[]" value="[email protected]">Foo</input>
<input type="checkbox" name="mybox[]" value="[email protected]">Bar</input>
<input type="checkbox" name="mybox[]" value="[email protected]">Hello</input>
<input type="checkbox" name="mybox[]" value="[email protected]">World</input>

After POSTing the form to your PHP, $_POST['mybox'] will be an array holding the values of the boxes checked.

In your PHP

if(isset($_POST['my_box']))
{
    $subject = "sub";
    $body = "body";
    if (is_array($_POST['mybox']))
    {
        //multiple items were selected.
        $to = implode(',',$_POST['my_box']);

        mail($to,$subject,$body);
    }
    else    //only one item was selected
    {
        echo $_POST['my_box'];
        $to = $_POST['my_box'];
        mail($to,$subject,$body);
    }
}
else
    //none were selected

1 Comment

Just a note and I know this is beyond the scope of the question and answer but you MUST make sure you sanitize all post variables to prevent getting your form hijacked by spammers.
0

You can simply assign the same name to all checkboxes, which actually results in a checkbox array.

<form name="someform" onsubmit="return validate(this)" action="process.php" method="post">
    <input type="checkbox" name="names[]" value="Saji">Saji
    <input type="checkbox" name="names[]" value="Muhaimin">Muhaimin
    <input type="checkbox" name="names[]" value='Muhsin'>Muhsin
    <input type="submit" value="Submit">
    <input type="reset" value="Reset">
</form>

In process.php, you could have-

    $name_val=$_POST['names'];
    foreach($name_val as $values){
      //Here $values will contain only the values of the checkboxes you had selected.
      echo $values."<br />";
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.