• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 614
  • Last Modified:

PHP Formmail script send values of multiple checkboxes

Hi,

I have an HTML form that has a few checkboxes on it which I need to send the values of through my PHP Formmail script.

Here is what I have so far:

HTML FORM

<form id="myForm" onsubmit="return submitIt(this)" method="post" action="formmail.php">
<div>
      <label>Gender<br />Male<input type="radio" name="gender" value="male" checked="true" /> Female<input type="radio" name="gender" value="female" /></label><br />

      <label>Which browsers do you use?<br />IE<input type="checkbox" name="use" value="IE"> Firefox<input type="checkbox" name="use" value="Firefox"> Netscape<input type="checkbox" name="use" value="Netscape"></label><br />

      <label>Favourite Search Engine <select name="favesearch" size="1">
      <option selected>Google
      <option>Yahoo!
      <option>Altavista
      </select></label><br />

      <label>Name <input type="text" name="urname" /></label><br />

      <label>Email <input type="text" name="email" /></label><br />

      <label>Enquiry <textarea name="message"></textarea></label><br />

      <input type="submit" value="send" /> <input type="reset" value="reset" />
</div>
</form>


PHP SCRIPT

<?php

$site = "Confirmation";
$email = $_POST['email'] ;
$gender = $_POST['gender'];
$use = $_POST['use'];
$favesearch = $_POST['favesearch'];
$urname = $_POST['urname'] ;
$message = $_POST['message'];

mail( "asdf@hotmail.com", "Form Results",
  "Gender: $gender\nUse: $use\nFave Search Engine: $favesearch\nName: $urname\nEmail: $email\nMessage: $message\n", "From: $email");
header( "Location: form.html");

mail($email, $site, "Thank you for your enquiry, you will be contacted shortly.");

?>

Everything emails correctly apart from when you select more than 1 checkbox it just sends the value of the last one selected.  I need it to send all of the values selected if possible.  If someone could help me with this I would be really grateful as I have been messing around with it for a couple of hours and got nowhere.

Thanks,

John
0
john-formby
Asked:
john-formby
  • 2
1 Solution
 
fastball1945Commented:
this is a little scirpt that i found at this website:

http://www.apptools.com/phptools/forms/forms3.php

<?php
   $listvals=$_POST['mylist'];
   $n=count($listvals);
   echo "User chose $n items from the list.<br>\n"
   foreach($i=0;$i<count($listvals);$i++)
      echo "Item $i=".$listvals[$i]."<br>\n";
?>
0
 
ldbkuttyCommented:
Checkboxes (and multiple selection) should be done with HTML arrays for easier and efficient processing. Change the form checkbox like this:

<label>Which browsers do you use?<br />
    IE<input type="checkbox" name="use[]" value="IE">
    Firefox<input type="checkbox" name="use[]" value="Firefox">
    Netscape<input type="checkbox" name="use[]" value="Netscape">
</label>

Remember that you have to give all checkboxes same name with '[]' behind it. Only the checkboxes that are checked will be passed to the next page. If the first and the third checkboxes have been checked, you will get an array $_POST["use"] that contains:

$_POST["use"][0] = "IE"
$_POST["use][1] = "Netscape"

Change the "submit" button to this:

<input type="submit" name="Submit" value="send" />

In formmail.php:
===========

<?php

// If form was submitted
if(isset($_POST["Submit"]) && !empty($_POST["Submit"]))
{
    $site = "Confirmation";
    $email = $_POST['email'] ;
    $gender = $_POST['gender'];

    $use = implode(", ", $_POST['use']); // Modified here ..

    $favesearch = $_POST['favesearch'];
    $urname = $_POST['urname'] ;
    $message = $_POST['message'];

    mail( "asdf@hotmail.com", "Form Results",
      "Gender: $gender\nUse: $use\nFave Search Engine: $favesearch\nName: $urname\nEmail: $email\nMessage: $message\n", "From: $email");

    mail($email, $site, "Thank you for your enquiry, you will be contacted shortly.");

    header( "Location: form.html");
    exit();
}

?>
0
 
john-formbyAuthor Commented:
Thanks ldbkutty, it now works perfectly :-)

John
0
 
ldbkuttyCommented:
You are welcome :=)

- Balakrishnan
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now