0

I have been using php to try and email the user entered content from a web form to an email address. Been messing around with the php code to no avail. Here's the web form:

<form action="form2email.php" method="post">
<div id="problem" class="fluid">
    <textarea name="problem" cols="50" rows="4" placeholder="enter problem here" row="4"></textarea>   
</div>
<div id="email" class="fluid">
    <input type="email" name="email" placeholder="email optional">
</div>
<div id="userOptions" class="fluid">     
        <label for="yes">Yes</label>
        <input name="publish" type="radio" value="Yes">
        <label for="no">No</label>
        <input type="radio" name="publish" value="No">    
        <label for="responder"><b>Michael</b></label>
        <input name="responder" type="checkbox" value="Michael" id="responder">
        <label for="responder"><b>Jennifer</b></label>
        <input type="checkbox" name="responder" value="Jennifer" id="responder">
        <label for="responder"><b>Mei</b></label>
        <input type="checkbox" name="responder" value="Mei" id="responder">
        <label for="responder"><b>Random</b></label>
        <input type="checkbox" name="responder" value="Random" id="responder">
</div>
<div id="submit" class="fluid">
    <input type="submit" value="Submit">
</div>
</form>

and here's the php:

<?php
    $emailBody= 'problem: '.$_POST['problem']."\n".
    'email: '.$_POST['email']."\n".
    'publish: '.$_POST['publish']."\n".
    'responder: '.$_POST['responder']."\n"
if(isset($_POST['submit'])) {
    mail('[email protected]', 'problem' , $emailBody);
    header('location: thankyoupage.HTML');
}
else{
    header('location: testing.html');
}
?>

The problems vary when I mess around with the code but it always has a problem with everything after the variable. The only way not to generate an error message is by making th variable at the end of the if else statements.

In this case shown above the form wont work at all it just takes me to a page that says "Object not found error 404". And on the php file it says there's an unexpected T_if but if I put a semi-colon at the end of the variable the script doesn't work at all. Help will be much appreciated and thank you in advance.

2
  • what variable are you talking about Commented Dec 8, 2013 at 21:35
  • leaving out the semi colon just swaps the 'main' error with a syntax error Commented Dec 8, 2013 at 21:37

1 Answer 1

1

2 issues with your form and your handler. (N.B.: See my EDIT a little further down).

1) Form: Missing name for submit button <input type="submit" value="Submit">

Without it being named, your handler will never execute the mail() function.

2) PHP handler: Missing semi-colon at the end of 'responder: '.$_POST['responder']."\n"

Tested and working for me

HTML FORM

<form action="form2email.php" method="post">
<div id="problem" class="fluid">
    <textarea name="problem" cols="50" rows="4" placeholder="enter problem here" row="4"></textarea>   
</div>
<div id="email" class="fluid">
    <input type="email" name="email" placeholder="email optional">
</div>
<div id="userOptions" class="fluid">     
        <label for="yes">Yes</label>
        <input name="publish" type="radio" value="Yes">
        <label for="no">No</label>
        <input type="radio" name="publish" value="No">    
        <label for="responder"><b>Michael</b></label>
        <input name="responder" type="checkbox" value="Michael" id="responder">
        <label for="responder"><b>Jennifer</b></label>
        <input type="checkbox" name="responder" value="Jennifer" id="responder">
        <label for="responder"><b>Mei</b></label>
        <input type="checkbox" name="responder" value="Mei" id="responder">
        <label for="responder"><b>Random</b></label>
        <input type="checkbox" name="responder" value="Random" id="responder">
</div>
<div id="submit" class="fluid">
    <input type="submit" value="Submit" name="submit">
</div>
</form>

PHP handler

<?php
    $emailBody= 'problem: '.$_POST['problem']."\n".
    'email: '.$_POST['email']."\n".
    'publish: '.$_POST['publish']."\n".
    'responder: '.$_POST['responder']."\n";
if(isset($_POST['submit'])) {
    mail('[email protected]', 'problem' , $emailBody);
    header('location: thankyoupage.HTML');
}
else{
    header('location: testing.html');
}

?>

EDIT

Another thing I noticed is that you have multiple choices used as checkboxes.

There are a few more things that need to be added in order for it to work properly and show each checked box in the Email.

1) Using a foreach in the PHP handler:

foreach ($_POST['responder'] as $value) {
    $check_msg .= "Checked: $value\n";
}

2) Adding brackets to treat each checkbox as an array:

I.e.: name="responder[]"

New Handler:

<?php

foreach ($_POST['responder'] as $value) {
    $check_msg .= "Checked: $value\n";
}

    $emailBody= 'problem: '.$_POST['problem']."\n".
    'email: '.$_POST['email']."\n".
    'publish: '.$_POST['publish']."\n".

 '' . $check_msg . "\n";

if(isset($_POST['submit'])) {
    mail('[email protected]', 'problem' , $emailBody);
    header('location: thankyoupage.HTML');
}
else{
    header('location: testing.html');
}
?>

New form:

<form action="form2email.php" method="post">
<div id="problem" class="fluid">
    <textarea name="problem" cols="50" rows="4" placeholder="enter problem here" row="4"></textarea>   
</div>
<div id="email" class="fluid">
    <input type="email" name="email" placeholder="email optional">
</div>
<div id="userOptions" class="fluid">     
        <label for="yes">Yes</label>
        <input name="publish" type="radio" value="Yes">
        <label for="no">No</label>
        <input type="radio" name="publish" value="No">    
        <label for="responder"><b>Michael</b></label>
        <input name="responder[]" type="checkbox" value="Michael" id="responder">
        <label for="responder"><b>Jennifer</b></label>
        <input type="checkbox" name="responder[]" value="Jennifer" id="responder">
        <label for="responder"><b>Mei</b></label>
        <input type="checkbox" name="responder[]" value="Mei" id="responder">
        <label for="responder"><b>Random</b></label>
        <input type="checkbox" name="responder[]" value="Random" id="responder">
</div>
<div id="submit" class="fluid">
    <input type="submit" value="Submit" name="submit">
</div>
</form>

Choosing 3 names from the form will produce something similar to the following:

Checked: Jennifer
Checked: Mei
Checked: Random

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

2 Comments

thanks alot for all the help.The script is working perfectly now.The only issue is that when I test it I don't receive an email.I am testing it using dreamweaver and XAMPP.Would you happen to know if that is why Im not receiving the email?
You're welcome. All I can think of is that to check your settings and what you're using for mail. To "close" the question and mark as correct, click the white checkmark till it turns green. Otherwise your question will remain in the unanswered category. @user3080821

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.