0

I'm trying to set up a simple PHP contact form for a website and I need some help modifying the PHP to list multiple items from a select menu and would appreciate the help. I'm a graphic designer, not a developer, so a lot of this is way over my head. This is the problem area here:

HTML:

<form method="post" action="projectplanner.php">
<label>Name*</label><input class="text" name="name" placeholder="Typ hier uw naam">
<label>Email*</label><input class="text" name="email" type="email" placeholder="Typ hier uw email">
<label>Telefoon*</label><input class="text" name="telefoon" type="tel" placeholder="Telefoon of mobiel">
<label>Organisatie*</label><input class="text" name="organisatie" placeholder="Ik vertegenwoordig..">
<label>Bestaande website (indien van toepassing)</label><input class="text" name="website" placeholder="http://www">
<label>Soort project</label>
<input name="project[]" type="checkbox" value="huisstijl" class="check" />Huisstijl<br />
<input name="project[]" type="checkbox" value="websiteontwerp" class="check"/>Website ontwerp<br />
<input name="project[]" type="checkbox" value="websiteontwikkeling" class="check"/>Website ontwikkeling<br />
<input name="project[]" type="checkbox" value="onlinemarketing" class="check-last"/>Online marketing<br /> 
<label>Beschrijving van uw project</label>
<textarea class="project_text" name="beschrijving" placeholder="Een globale beschrijving. Doelstelling, publiek, concurenten?"></textarea>
<label>Inspiratie</label>
<textarea class="project_text" name="inspiratie" placeholder="Kopieer links naar website / apps / afbeeldingen die u inspireren"></textarea>
<label>* 2+2= ? (Anti-spam)</label>
<input name="human" placeholder="Antwoord">
<input id="submit" name="submit" type="submit" value="Verzend">

PHP:

<?php
$naam = $_POST['naam'];
$email = $_POST['email'];
$telefoon = $_POST['telefoon'];
$organisatie = $_POST['organisatie'];
$website = $_POST['website'];
$beschrijving = $_POST['beschrijving'];
$project = $_POST['project'];
$inspiratie = $_POST['inspiratie'];
$from = 'From: the website'; 
$to = '[email protected]'; 
$subject = 'onderwerp';
$human = $_POST['human'];

$body = 
"Van: $name\n 
E-Mail: $email\n 
Telefoon: $telefoon\n 
Organisatie: $organisatie\n 
Website: $website\n 
Project Soort: $project\n 
Omschrijving: $beschrijving\n
Inspiratie: $inspiratie";
if ($_POST['submit'] && $human == '4') {                 
    if (mail ($to, $subject, $body, $from)) { 
    print ("Dank u wel");
    /*echo '<p>Uw bericht is verzonden!</p>';*/
} else { 
    echo '<p>Oeps. Er ging iets fout. Probeer nogmaals.</p>'; 
} 
} else if ($_POST['submit'] && $human != '4') {
/*echo '<p>Uw anti-spam antwoord is niet goed ingevuld.</p>';*/
}
1
  • you can access your checkboxes like this $_POST['project'], run a var_dump($_POST['project']) to see what it contains. Commented Aug 30, 2011 at 15:49

2 Answers 2

4

This will list the projects (if any have been checked) like so:

Project Soort: websiteontwerp, websiteontwikkeling, onlinemarketing

<?php
// insure form variables exist
$name         = isset($_POST['name'])         ? $_POST['name']         : '';
$email        = isset($_POST['email'])        ? $_POST['email']        : '';
$telefoon     = isset($_POST['telefoon'])     ? $_POST['telefoon']     : '';
$organisatie  = isset($_POST['organisatie'])  ? $_POST['organisatie']  : '';
$website      = isset($_POST['website'])      ? $_POST['website']      : '';
$beschrijving = isset($_POST['beschrijving']) ? $_POST['beschrijving'] : '';
$inspiratie   = isset($_POST['inspiratie'])   ? $_POST['inspiratie']   : '';
$human        = isset($_POST['human'])        ? $_POST['human']        : '';
$submit       = isset($_POST['submit'])       ? true                   : false;

$project = isset($_POST['project'])
         ? implode(', ', $_POST['project'])     // gather selected checkboxes
         : 'Er geen projecten geselecteerd';    // (Unsure of translation)

$from = 'From: the website'; 
$to = '[email protected]'; 
$subject = 'onderwerp';

$body = 
"Van: $name\n 
E-Mail: $email\n 
Telefoon: $telefoon\n 
Organisatie: $organisatie\n 
Website: $website\n 
Project Soort: $project\n 
Omschrijving: $beschrijving\n
Inspiratie: $inspiratie";

if ($submit && $human == '4') {
    if (mail ($to, $subject, $body, $from)) {
        print ("Dank u wel");
        /*echo '<p>Uw bericht is verzonden!</p>';*/
    } else {
        echo '<p>Oeps. Er ging iets fout. Probeer nogmaals.</p>';
    }
} else if ($submit && $human != '4') {
        /*echo '<p>Uw anti-spam antwoord is niet goed ingevuld.</p>';*/
}
?>

If any checkboxes are select when a user sends the form, PHP receives them as an array. The implode() function, pulls all of them together into a comma separated string. Feel free to change the comma (and/or the space following it) to something else if you like.

Hope this helps.

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

Comments

1

When you add trailing brackets to your field names (ie: project[]), PHP will create an array with the given name, minus the brackets. In your case, the quickest solution would probably be to use a foreach loop to iterate through all the checked boxes like so:

$body = 
"Van: $name\n 
E-Mail: $email\n 
Telefoon: $telefoon\n 
Organisatie: $organisatie\n 
Website: $website\n
Project Soort: ";

foreach ($project as $checkbox) {
    $body .= "$checkbox, ";
}

$body .= "\n 
Omschrijving: $beschrijving\n
Inspiratie: $inspiratie";

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.