vwhatizthiz
07-24-2012, 06:34 PM
Hi everyone, I'm trying to create a contact form with a simple validation question (what is 2+2) because I've been getting a lot of spam e-mails. I want it to just display a popup box on the same page saying that the message was either sent successfully, not sent, or they got the question wrong.
Unfortunately I keep getting errors with it and I can't seem to figure it out. Any help?
HTML:
<form id="contactus" method="post" action="contactengine.php">
<p>contact</p>
<label for="Name">name:</label>
<input type="text" name="name" id="name" placeholder="e.g., John Doe" />
<label for="Email">e-mail:</label>
<input type="text" name="email" id="email" placeholder="johndoe@example.com"/>
<label>what is 2+2? (anti-spam)</label>
<input name="spam" placeholder="">
<label for="Message">message:</label>
<textarea name="message" id="message" placeholder="Send us any questions, comments, or ideas you may have!"></textarea>
<input type="submit" name="submit" value="Submit" id="submit" />
</form>
PHP:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = '******';
$to = '********';
$subject = 'Message from a site visitor.';
$body = "From: $name\nE-Mail: $email\nMessage:\n $message"; /
if ($_POST['submit'] && $spam === '4') {
if (mail ($email, $subject, $body)) {
?>
<script language="javascript" type="text/javascript">
alert('Thank you for the message. We will contact you shortly.');
window.location = 'index.php';
</script>
<?php
} else { //end of PHP, start of JS ?>
<script language="javascript" type="text/javascript">
alert('Something went wrong! Please go back and try again.');
window.location = 'index.php';
</script>
<?php //end of JS, start of PHP
}
} else if ($_POST['submit'] && $spam != '4') {
?>
<script language="javascript" type="text/javascript">
alert('You answered the anti-spam question wrong! Please go back and try again.');
window.location = 'index.php';
</script>
<?php //end of JS start of PHP
}
?>
Unfortunately I keep getting errors with it and I can't seem to figure it out. Any help?
HTML:
<form id="contactus" method="post" action="contactengine.php">
<p>contact</p>
<label for="Name">name:</label>
<input type="text" name="name" id="name" placeholder="e.g., John Doe" />
<label for="Email">e-mail:</label>
<input type="text" name="email" id="email" placeholder="johndoe@example.com"/>
<label>what is 2+2? (anti-spam)</label>
<input name="spam" placeholder="">
<label for="Message">message:</label>
<textarea name="message" id="message" placeholder="Send us any questions, comments, or ideas you may have!"></textarea>
<input type="submit" name="submit" value="Submit" id="submit" />
</form>
PHP:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = '******';
$to = '********';
$subject = 'Message from a site visitor.';
$body = "From: $name\nE-Mail: $email\nMessage:\n $message"; /
if ($_POST['submit'] && $spam === '4') {
if (mail ($email, $subject, $body)) {
?>
<script language="javascript" type="text/javascript">
alert('Thank you for the message. We will contact you shortly.');
window.location = 'index.php';
</script>
<?php
} else { //end of PHP, start of JS ?>
<script language="javascript" type="text/javascript">
alert('Something went wrong! Please go back and try again.');
window.location = 'index.php';
</script>
<?php //end of JS, start of PHP
}
} else if ($_POST['submit'] && $spam != '4') {
?>
<script language="javascript" type="text/javascript">
alert('You answered the anti-spam question wrong! Please go back and try again.');
window.location = 'index.php';
</script>
<?php //end of JS start of PHP
}
?>