I have a contact form I'm trying to run, but for whatever reason the script will die every time I attempt to submit it. Not sure why. Is there anything blantanly wrong with the code below? I wasn't using isset() before, but someone here suggested I do and now it breaks the page.
FORM
<form class="contactform" method="post" action="php/contact.php">
<h3>Name</h3>
<input class="form inputboxes" type="text" name="name">
<h3>Email</h3>
<input class="form inputboxes" type="email" name="email">
<h3>Phone</h3>
<input class="form inputboxes" type="number" name="phone">
<h3>Message</h3>
<textarea class="form inputboxes" name="message"></textarea>
<h3 class="captchastyle">Are you real? What is 2 + 2?</h3><input class="captcha captchastyle" type="text" name="captcha" maxlength="1">
<input class="form submit" name="submit" type="submit" value="Submit">
</form>
PHP
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$captcha = $_POST['captcha'];
$from = 'From: HankSmith.com Contact Form';
$to = 'thatbraxjohnsonguy#gmail.com';
$subject = 'HANK SMITH CONTACT FORM';
$body = "
From: $name\n
E-Mail: $email\n
Phone: $phone\n
Message: $message\n";
if (isset($_POST['submit'] && $captcha == 4)) {
if (mail ($to, $subject, $body)) {
echo '<p style="margin-top: 150; text-align:center; font-size: 18px;">Your message has been sent! Click here to return to the website.</p>';
} else {
echo '<p>Problem sending mail!';
}
} else {
echo '<p>Something went wrong, try again later!</p>';
}
?>
ERROR_LOG
[09-Feb-2017 12:15:46 America/New_York] PHP Fatal error: Cannot use isset() on the result of an expression (you can use "null !== expression" instead) in /home/yourerlv/public_html/hanksmith.com/php/contact.php on line 17
You're doing it wrong. You need to move the parenthesis over so it's only evaluating the one variable:
if (isset($_POST['submit']) && $captcha == 4) {
^
Change code:
if (isset($_POST['submit'] && $captcha == 4)) { // Original
if (isset($_POST['submit']) && $captcha == 4 ) { // Changed
Related
My first time posting here so apologies if i get anything wrong. I have recently built a site from a HTML5 UP template - www.vancareleeds.co.uk. I have used the contact form that came with the template and used a simple mail.php file in the root folder to action the email to be sent to my inbox.
I have also put in Google ReCaptcha but am since struggling to force the form to validate (EG. the form can be sent without the reCaptcha being ticked and it can also be sent with no information in the fields on the form.
I have provided my code here of the .php and also the webpage itself.
If i have broken protocol or best practice for psots i apologise.
mail.php
<?php $name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$mobile = $_POST['mobile'];
$vehicle = $_POST['category-vehicle'];
$service = $_POST['category-service'];
$formcontent="From: $name \n Message: $message \n Mobile: $mobile \n Vehicle: $vehicle \n Service Required: $service";
$recipient = "info#vancareleeds.co.uk";
$subject = "Contact Form from VanCare Website";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!";
?>
form html
<form method="post" action="mail.php">
<div class="field">
<label for="name">Name</label>
<input type="text" name="name" id="name" />
</div>
<div class="field">
<label for="email">Email</label>
<input type="email" name="email" id="email" />
</div>
<div class="field">
<label for="message">Message</label>
<textarea name="message" id="message" rows="4"></textarea>
</div>
<div class="g-recaptcha" data-theme="dark" class="g-recaptcha" data-sitekey="6Lf1OVMUAAAAAJv1fNtt-CJEFPK-Q0Ugc1CVCRVh"></div><br/>
<ul class="actions">
<li><input type="submit" value="Send Message" /></li>
</ul>
</form>
Welcome +Catalan Soccer! I would suggest to you if you are new to PHP to use var_dump(), print_r() or var_export() the result of the $_POST global array to see what was sent to your PHP script. All other things you can comment out and uncomment if you feel confident.
$errors = [];
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['name'], $_POST['email'], $_POST['message']) === true) {
$name = filter_var(trim($_POST['name']), FILTER_SANITIZE_STRING);
if (empty($name) === true) {
$errors['name'][] = 'Name is empty';
} elseif (ctype_alpha($name) === false) {
$errors['name'][] = 'Name contains invalid characters';
}
$email = filter_var(trim($_POST['email']), FILTER_SANITIZE_EMAIL);
if (empty($email) === true) {
$errors['email'][] = 'E-mail is empty';
} elseif ($email === false) {
$errors['email'][] = 'Invalid e-mail address';
}
$message = strip_tags(trim($_POST['message']));
if (empty($message) === true) {
$errors['message'][] = 'Message is empty';
}
if (count($errors) === 0) {
echo 'Thank you!';
// you can use here the sanitized user input to send the e-mail
}
}
if (count($errors) > 0) {
foreach ($errors as $field => $messages) {
echo implode(', ', $messages), '<br>';
}
}
This will test user input and print out error message.
I've been using the same contact form for quite a while:
<?php
if (isset($_POST['submit']) ) {
$name = $_POST['names'];
$email = $_POST['email'];
$message = $_POST['message'];
$robot = $_POST['robot'];
$from = 'online#domain.co.uk';
$to = 'info#domain.co.uk';
$subject = 'Online Enquiry';
$headers = "From: DOMAIN <no-reply#domain.co.uk> \r\n";
$headers .= 'Reply-To:'. $email . "\r\n";
$headers .= "X-Mailer: PHP/" . phpversion(); // Sender's Email
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
} ?>
<?php if (isset($_POST['submit'])) {
if ($name != '' && $email != '' && $message !='') {
if ($robot != 'yes') {
if (mail ($to, $subject, $body, $headers)) {
echo '<p class="approved"><strong>Your message has been submitted</strong></p>';
} else {
echo '<p class="warning"><strong>Something went wrong!</strong></p>';
}
} else if ($_POST['submit'] && $robot == 'yes') {
echo '<p class="warning"><strong>Looks like you are spam!</strong></p>';
}
} else {
echo '<p class="warning"><strong>Please complete all fields.</strong></p>';
}
}
?>
<form id="contact" method="post" action="#contact">
<label>Name</label>
<input name="names" type="text" placeholder="NAME">
<label>Email</label>
<input name="email" type="email" placeholder="EMAIL">
<label>Message</label>
<textarea rows="10" name="message" placeholder="MESSAGE"></textarea>
<input id="capture" name="robot" type="checkbox" value="yes">
<input id="submit" name="submit" type="submit" value="Submit">
</form>
It works absolutely fine on other sites but doesn't work on one domain? submit simply returns the error "Something went wrong!". the site is on the same cloud server as many of the other sites that work fine. It's driving me mad because I cant see an error.
Turning debugging on I get the error:
Notice: Undefined index: robot in /form-contact.php on line 6
Ive tried removing the robot check all together and it still doesn't work, but generates no error when debugging?
Can anyone offer any advice?
When your checkbox is not checked - it will not be sent.
Replace
$robot = $_POST['robot'];
to
$robot = isset($_POST['robot']) ? 'yes' : 'no';
Your Condition Matches the case. Try checking the value of $robot on submit.
<input id="capture" name="robot" type="checkbox" value="yes">
if ($robot != 'yes') {
if (mail ($to, $subject, $body, $headers)) {
echo '<p class="approved"><strong>Your message has been submitted</strong></p>';
} else {
echo '<p class="warning"><strong>Something went wrong!</strong></p>';
}
I'm wondering what is going wrong in this contact form. I'm not sure why, but it constantly defaults to the else and says Something went wrong, please try again later. I can't figure out why, and the error_log isn't showing anything. Is there anything glaringly obvious I'm missing? I'm pretty new to PHP.
PHP
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$captcha = $_POST['captcha'];
$from = 'From: HankSmith.com Contact Form';
$to = 'thatbraxjohnsonguy#gmail.com';
$subject = 'HANK SMITH CONTACT FORM';
$body = "
From: $name\n
E-Mail: $email\n
Phone: $phone\n
Message: $message\n";
if ($_POST['submit'] and $captcha == 4) {
if (mail ($to, $subject, $body, $from)) {
echo '<p style="margin-top: 150; text-align:center; font-size: 18px;">Your message has been sent! Click here to return to the website.</p>';
} else {
echo '<p>Something went wrong, try again later!</p>';
}
}
?>
HTML Form
<form class="contactform" method="post" action="php/contact.php">
<h3>Name</h3>
<input class="form inputboxes" type="text" name="name">
<h3>Email</h3>
<input class="form inputboxes" type="text" name="email">
<h3>Phone</h3>
<input class="form inputboxes" type="text" name="phone">
<h3>Message</h3>
<textarea class="form inputboxes" name="message"></textarea>
<h3 class="captchastyle">Are you real? What is 2 + 2?</h3><input class="captcha captchastyle" type="text" name="captcha" maxlength="1">
<input class="form submit" name="submit" type="submit" value="Submit">
</form>
When I copied this into my text editor, it threw a parse error because the echo statement in the if (mail)... section didn't have a closing brace. I never use 'and', I usually use '&&', but it actually doesn't seem to matter in this case.
if ($_POST['submit'] && $captcha == 4) {
if (mail ($to, $subject, $body, $from)) {
echo '<p style="margin-top: 150; text-align:center; font-size: 18px;">Your message has been sent! Click here to return to the website.</p>';
} // THIS MUST BE HERE
} else {
echo '<p>Something went wrong, try again later!</p>';
}
Also, you probably want an 'else' condition for that if (mail)... statement. If you're seeing a blank page, it will be because you aren't handling what happens if the mail() function returns false:
if (mail ($to, $subject, $body, $from)) {
echo '<p style="margin-top: 150; text-align:center; font-size: 18px;">Your message has been sent! Click here to return to the website.</p>';
} else {
echo '<p>Problem sending mail!';
}
And honestly, the native PHP mail() function sucks! Consider using SwiftMailer or PHPMailer instead.
Use isset on your condition:
if (isset($_POST['submit']) && $captcha == 4)
It checks if the field is non empty and the form has been submitted.
And && is better here than and see the reason here: https://stackoverflow.com/a/11861068
I have a simple contact form appearing on a modal in bootstrap. For some reason my form will not send. I've been looking at it for the last few hours and could use a fresh pair of eyes. It seems so simple but I'm at a total loss.
I've put a non-displayed textfield in the form as a spam filter. Since most bots would fill the textfield I want the code to send only if the textfield is blank (which it should be if the user is human since it's not displayed).
index.php
<div class = "modal fade" id = "contact" role = "dialog">
<div class = "modal-dialog">
<div class = "modal-content">
<div class = "modal-header">
<a id = "btn-close" data-dismiss = "modal">X</a>
</div>
<form>
<form method="post" action="index.php">
<fieldset id= "contact-fieldset">
<input name="name" type="text" id="name" class="text-input" placeholder="First and last name" required>
<input name="email" type="email" placeholder="Email address" required>
<textarea name="message" placeholder="Your Message" required></textarea>
<input name="bot-catch" type="text" id="bot-catch">
<input id="submit" class="submit-btn" name="submit" type="submit" value="Submit">
</fieldset>
</form>
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: Me';
$to = 'me#email.com';
$subject = 'Hello';
$human = $_POST['bot-catch'];
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if ($_POST['submit'] && $human == '') {
if (mail ($to, $subject, $body, $from)) {
echo '<p>Your message has been sent!</p>';
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
} else if ($_POST['submit'] && $human != '') {
echo '<p>You answered the anti-spam question incorrectly!</p>';
}
?>
</form>
<div class = "modal-footer">
</div>
</div>
</div>
</div>
EDIT: So I have now gotten my message to send, but only if I put the PHP code into a separate file and link to it by . Only problem is- I do not want the user to be directed off of my page once they send a message. Is there anything I can do? I still can't figure out why it won't send on my index page.
EDIT: Well I got it working!! Thank you for all your help!
For some reason i cant receive any email from my submit form - is there a problem with my formmail script?
<form action="contact_process.php" method="post" enctype="application/x-www-form-urlencoded" class="three">
<legend><strong>Form</strong></legend>
<fieldset>
<p>
<label for="name">Your Name</label>
<input type="text" name="name"></p>
<p>
<label for="email">Your Email</label>
<input type="text" name="email"></p>
<p>
<label for="subject">Subject</label>
<input type="text" name="subject"></p>
<p>
<label for="EnquiryType">Enquiry Type</label>
<select type="text" name="EnquiryType">
<option value="general">General</option>
<option value="other">Other</option>
</select></p>
<p>
<label for="message">Message</label>
<textarea type="text" name="message" class="msg"></textarea></p>
</fieldset>
<input type="submit" name="submit" value="Submit">
<input type="reset" name="Submit2" value="Reset">
</form>
contact_process.php
<?php
# bool has_injection (String $var, [String $var, ...])
function has_injection () {
$values = func_get_args();
for ($i=0, $count=func_num_args(); $i<$count; $i++) {
if ( stristr($values[$i], "%0A") || stristr($values[$i], "%0D") || stristr($values[$i], "\\r") || stristr($values[$i], "\\n")
|| stristr($values[$i], "Bcc") || stristr($values[$i], "Content-Type") ) {
return true;
}
}
return false;
}
$error = '';
if (isset($_POST) && count($_POST)>0) {
# The form has been submitted
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$EnquiryType = $_POST['EnquiryType'];
$message = $_POST['message'];
if ($name && $email && $subject && $EnquiryType && $message) {
if (has_injection($name, $email, $subject, $EnquiryType, $message)) {
# You've got another spammer at work here
$error = 'No spamming';
exit(0);
}
else {
# It's safe to send the message
mail('my#email.com', $subject, $message, $EnquiryType, $message,"From: $name <$email>");
}
}
else {
$error = 'Please fill in all the forms';
}
}
?>
u send 6 parameters..
i checked that and i get elow error
mail() expects at most 5 parameters, 6 given in C:\xampp\htdocs\parixan\contact_process.php on line 31
see here
update
$message = $_POST['EnquiryType']."\r\n".$_POST['message'];
$headers = 'From: $name <$email>' . "\r\n" .
'Reply-To: $name <$email>' . "\r\n" ;
then use
mail('my#email.com', $subject, $message, $headers);
I made a pretty good PHP email script that sends. I've been programming a website of my own (PHP, HTML) and I'm trying to find a way to receive emails. I've heard about POP3, and others but I can't ever find much on them.
<?php
$to = $_POST['to'];
$from = $_POST['from'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$body = "This email was sent from a no-reply email service. \n\n $message";
$headers = "From: $from";
mail($to, $subject, $body, $headers);
?>
Hope that helps ^^