3

Hi I've previously used this very simple php contact script with success though when I've tried implementing it on a new HTML page with the form won't submit. Can anyone see any obvious errors? Any help would be much appreciated

Here is the html of the form:

<div id="formContainer">

    <form action="form.php" method="post" id="contactForm"> 
    <fieldset>

    <legend>Your details</legend>    

    <label for="name">Name *</label>
    <input type="text" id="name">

    <label for="email">Email *</label>
    <input type="email" id="email">

    <label for="tel">Telephone</label>
    <input type="tel" id="tel">

    </fieldset>

    <fieldset> 
    <legend>Tutoring</legend>

    <label for="type">Type of lesson</label>
    <select name="type" id="type">
    <option>Individual</option>
    <option>Group</option>
    </select>

    <label for="subject">Subject</label>
    <input name="subject" list="subjects" id="subject">
    <datalist id="subjects">
    <option>English</option>
    <option>Biology</option>
    <option>Geography</option>
    </datalist>

    <label for="level">Your level</label>
    <select name="level" id="level">
    <option>Beginner</option>
    <option>GCSE</option>
    <option>A-Level</option>
    <option>University</option>
    </select>

    <label for="hours">Hours/week</label>
    <input type="number" id="hours">

    <label for="info">Additional Information</label>
    <textarea name="info" id="info" rows="10"               cols="6"></textarea>


    </fieldset>

    <input type="submit" name="submit" value="Send" id="sendButton">

    <input type="hidden" name="submit_check" value="1" />   
    </form>

    </div>

And here is the simple php script:

<?php 

if ($_POST["email"]<>'') { 
    $ToEmail = '[email protected]'; 
    $EmailSubject = 'Site contact form '; 
    $mailheader = "From: ".$_POST["email"]."\r\n"; 
    $mailheader .= "Reply-To: ".$_POST["email"]."\r\n"; 
    $mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n"; 
    $MESSAGE_BODY = "Name: ".$_POST["name"]."<br>"; 
    $MESSAGE_BODY .= "Email: ".$_POST["email"]."<br>"; 
    $MESSAGE_BODY = "Telephone: ".$_POST["tel"]."<br>";
    $MESSAGE_BODY = "Type: ".$_POST["type"]."<br>";
    $MESSAGE_BODY = "Subject: ".$_POST["subject"]."<br>";
    $MESSAGE_BODY = "Level: ".$_POST["level"]."<br>";
    $MESSAGE_BODY = "Hours required: ".$_POST["hours"]."<br>";
    $MESSAGE_BODY .= "Additional information: ".nl2br($_POST["info"])."<br>"; 
    mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure"); 

?> 

<html>

    <h3>Thanks for your email</h3>
    <h4>I'll get back to you as soon as possible</h4>
    <a href="index.html"><p>Click here to go back to previous page</p></a>

</html>


<?php 
} else { 
?> 

<html>Sorry, this form didn't work</html>

<?php 
}; 
?>
2
  • Please note that unless you're running suhosin, you've got an exploitable vulnerability here; an attacker simply submits $_POST[ email"] with \r\nCc: blah\r\n\r\nI Love Spam! and you've turned your host into a spambot. Commented Apr 19, 2011 at 10:35
  • Yes I am concerned about that, the problem was that any script that had any sort of security features seemed incredibly complicated and I couldn't seem to implement them. How would I include suhosin? Thanks Commented Apr 19, 2011 at 11:30

5 Answers 5

3

Try with

if ( !empty($_POST["email"]) )

However you can check, what is posted in that page using:

echo '<pre>';
var_dump( $_POST );
Sign up to request clarification or add additional context in comments.

8 Comments

Thanks for all the replies but unfortunately changing the first if statement doesn't seem to help. Is it possible the problems is coming from the new html5 markup? This same script worked fine on another site i was using which just had a table based html4 form
No, that's not it. Whot does not work for you ? Is anything returned from var_dump ?
Well when i submit the form it goes straight to the else message so somewhere along the line it is not working. Sorry could you precise where I have to put your code for the var_dump? Thanks again
Thanks. i receive the following upon form submission array(6) { ["type"]=> string(10) "Individual" ["subject"]=> string(0) "" ["level"]=> string(8) "Beginner" ["info"]=> string(0) "" ["submit"]=> string(4) "Send" ["submit_check"]=> string(1) "1" } Sorry, this form didn't work
Because you do not have email field here. It's obvious that else statement is executed. Look at your HTML code.. you do not have name properties in your first 3 inputs. Name them and test it again.
|
1

Change your form.php as the following

    <?php 

if ($_POST["email"]<>'') { 
    $ToEmail = '[email protected]'; 
    $EmailSubject = 'Site contact form '; 
    $mailheader = "From: ".$_POST["email"]."\r\n"; 
    $mailheader .= "Reply-To: ".$_POST["email"]."\r\n"; 
    $mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n"; 
    $MESSAGE_BODY = "Name: ".$_POST["name"]."<br>"; 
    $MESSAGE_BODY .= "Email: ".$_POST["email"]."<br>"; 
    $MESSAGE_BODY = "Telephone: ".$_POST["tel"]."<br>";
    $MESSAGE_BODY = "Type: ".$_POST["type"]."<br>";
    $MESSAGE_BODY = "Subject: ".$_POST["subject"]."<br>";
    $MESSAGE_BODY = "Level: ".$_POST["level"]."<br>";
    $MESSAGE_BODY = "Hours required: ".$_POST["hours"]."<br>";
    $MESSAGE_BODY .= "Additional information: ".nl2br($_POST["info"])."<br>"; 
    mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure"); 

echo &lt;&lt;&lt;EXCERPT

<html>

    <h3>Thanks for your email</h3>
    <h4>I'll get back to you as soon as possible</h4>
    <a href="index.html"><p>Click here to go back to previous page</p></a>

</html>

EXCERPT;

} else { 


echo "<html>Sorry, this form didn't work</html>";

} 
?>

Comments

0

in your

if ($_POST["email"] <> '') { 

change that into

 if ($_POST["email"] != '') { 

Comments

0

You should try this:

if(!empty($_POST["email"])){
  //your email preparation code
}

Insted of using this:

if($_POST["email" <> ''){
  //your email preparation code
}

The ! basically means not, so the !empty means not empty

4 Comments

Thanks for all the replies but unfortunately changing the first if statement doesn't seem to help. Is it possible the problems is coming from the new html5 markup? This same script worked fine on another site i was using which just had a table based html4 form
Possibly, but is it actually changing pages? Or just staying on the same page when you click submit? + It may sound stupid but you may want to close of your input tags, like <input type="submit" name="submit" value="Send" id="sendButton" />. You've got a few tags which need closing.
I thought input tags closed with just > Have changed though, thank you. Still no joy with the form though! I should say that the new page did come up before and since adding a "name" value to my html markup, the form works and sends an email, however not all the fields show in the mail
Ok, so does the form work now then? You've selected the answer, what was wrong with it then?
0

You could also just use:

if ($_POST["email"]) { 

This works much like the != "" or empty() check. PHP was incepted to handle forms well. And you can just let it probe incoming form fields. It uses some magic boolean conversion rules for strings, which most of the time will accomplish what you want.

Another advantage of this simpler style is that it eases debugging if you enable the E_ALL and E_NOTICE (=debug) error_reporting mode.

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.