This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 7 years ago.
trying to setup the webpage with this PHP. I first got the blank page, then had it display the error(s) which was an internal server error. After a bit of research I saw that it's mostly the code that causes issues. If anyone would be kind enough to look this through for any possible solutions, I'd appreciate it. There is some more html code before this although it does not include any of the PHP.
<?php
$subjectPrefix = 'Site Contact Request';
$emailTo = '***#****.com';
if($_SERVER['REQUEST_METHOD'] == 'POST') {
$name = stripslashes(trim($_POST['form-name']));
$email = stripslashes(trim($_POST['form-email']));
$tel = stripslashes(trim($_POST['form-tel']));
$assunto = stripslashes(trim($_POST['form-assunto']));
$mensagem = stripslashes(trim($_POST['form-mensagem']));
$pattern = '/[\r\n]|Content-Type:|Bcc:|Cc:/i';
if (preg_match($pattern, $name) || preg_match($pattern, $email) || preg_match($pattern, $assunto)) {
die("Header injection detected");
}
$emailIsValid = preg_match('/^[^0-9][A-z0-9._%+-]+([.][A-z0-9_]+)*[#][A-z0-9_]+([.][A-z0-9_]+)*[.][A-z]{2,4}$/', $email);
if($name && $email && $emailIsValid && $assunto && $mensagem){
$subject = "$subjectPrefix";
$body = "Name: $name <br /> Subject: $assunto <br /> Email Address: $email <br /> Telephone Number: $tel <br /> Message: $mensagem";
$headers = 'MIME-Version: 1.1' . PHP_EOL;
$headers .= 'Content-type: text/html; charset=utf-8' . PHP_EOL;
$headers .= "From: $name <$email>" . PHP_EOL;
$headers .= "Return-Path: $emailTo" . PHP_EOL;
$headers .= "Reply-To: $email" . PHP_EOL;
$headers .= "X-Mailer: PHP/". phpversion() . PHP_EOL;
mail($emailTo, $subject, $body, $headers);
$emailSent = true;
} else {
$hasError = true;
}
}
?>
<?php if(isset($emailSent) && $emailSent): ?>
<div class="col-md-6 col-md-offset-3">
<div class="alert alert-success text-center">Your message has been sent.</div>
</div>
<?php else: ?>
<?php if(isset($hasError) && $hasError): ?>
<div class="col-md-5 col-md-offset-4">
<div class="alert alert-danger text-center">An error occurred. Please try later.</div>
</div>
<?php endif; ?>
<form action="<?php echo $_SERVER['REQUEST_URI']; ?>" id="contact-form" class="comment_form" role="form" method="post">
<div class="row-fluid">
<div class="span6">
<input type="text" class="form-control" id="form-name" name="form-name" placeholder="Name" required>
</div>
<div class="span6">
<input type="email" class="form-control" id="form-email" name="form-email" placeholder="Email" required>
</div>
</div>
<div class="row-fluid">
<div class="span6">
<input type="tel" class="form-control" id="form-tel" name="form-tel" placeholder="Telephone (Optional)">
</div>
<div class="span6">
<input type="text" class="form-control" id="form-assunto" name="form-assunto" placeholder="Subject" required>
</div>
</div>
<div class="row-fluid">
<div class="span8">
<textarea id="form-mensagem" name="form-mensagem" placeholder="Message"></textarea>
</div>
<div class="span4">
<button class="btn" type="submit"><i class="li_paperplane"></i>Send message</button>
</div>
</div>
</form>
You are not closing one if condition at starting, please modify it like this:-
<?php if(isset($emailSent) && $emailSent): ?>
<div class="col-md-6 col-md-offset-3">
<div class="alert alert-success text-center">Your message has been sent.</div>
</div>
<?php else: ?>
<?php if(isset($hasError) && $hasError): ?>
<div class="col-md-5 col-md-offset-4">
<div class="alert alert-danger text-center">An error occurred. Please try later.</div>
</div>
<?php endif; ?>
<?php endif; ?> // this is missing and based on your functionality you can move it to anywhere else
Output:- http://prntscr.com/74n8d3
Note:- this is very first html started with if condition. depend on your functionality you need to adjust it whereever you want.
Related
I have a form that I want to add ReCaptcha to, however, somewhere along the line something isnt working correctly because my form will still submit whether or not reCaptcha is verified.
This is the form:
<?php if(isset($_GET[ 'CaptchaPass'])){ ?>
<div>Thank you! Your Form was Successfully Submitted</div>
<?php } ?>
<?php if(isset($_GET[ 'CaptchaFail'])){ ?>
<div>Captcha Error. Please verify that you are human!</div>
<?php } ?>
<form action="http://vmobileautoglass.com/php/func_contact.php">
<label>Name</label> <span class="color-red">*</span>
<div class="row margin-bottom-20">
<div class="col-md-6 col-md-offset-0">
<input class="form-control" type="text" name="name">
</div>
</div>
<label>Email
<span class="color-red">*</span>
</label>
<div class="row margin-bottom-20">
<div class="col-md-6 col-md-offset-0">
<input class="form-control" type="text" name="email">
</div>
</div>
<label>Phone
</label>
<div class="row margin-bottom-20">
<div class="col-md-6 col-md-offset-0">
<input class="form-control" type="text" name="phone">
</div>
</div>
<label>Message</label> <span class="color-red">*</span>
<div class="row margin-bottom-20">
<div class="col-md-8 col-md-offset-0">
<textarea rows="8" class="form-control" name="message"></textarea>
</div>
</div>
<div class="g-recaptcha" data-sitekey="MYSITEKEYFROMGOOGLE"></div>
<p>
<button type="submit" class="btn btn-primary" name="ContactButton">Send Message</button>
</p>
</form>
This goes at the very top of the page where the form is located:
<?php
if (isset($_POST['ContactButoon'])) {
$url = 'https://google.com/recaptcha/api/siteverify';
$privatekey = "MYPRIVATEKEYFROMGOOGLE";
$response = file_get_contents($url . "?secret=" . $privatekey . "&response=" . $_POST['g-recaptcha-response'] . "&remoteip=" . $_SERVER['REMOTE_ADDR']);
$date = json_decode($response);
if (isset($data->success) AND $data->success == true) {
header('Location: contact.php?CaptchaPasss=True');
} else {
header('Location: contact.php?CaptchaFail=True');
}
}
?>
And this is the forms functionality:
// Receiving variables
#$pfw_ip = $_SERVER['REMOTE_ADDR'];
#$name = addslashes($_GET['name']);
#$email = addslashes($_GET['email']);
#$phone = addslashes($_GET['phone']);
#$message = addslashes($_GET['message']);
// Validation
if (strlen($name) == 0) {
header("Location: http://vmobileautoglass.com/php/err_name.php");
exit;
}
if (strlen($email) == 0) {
header("Location: http://vmobileautoglass.com/php/err_email.php");
exit;
}
if (strlen($message) == 0) {
header("Location: http://vmobileautoglass.com/php/err_message.php");
exit;
}
//Sending Email to form owner
$pfw_header = "From: $email\n"
. "Reply-To: $email\n";
$pfw_subject = "vMobile Contact Form";
$pfw_email_to = "vmobileag#gmail.com";
$pfw_message = "Visitor's IP: $pfw_ip\n"
. "name: $name\n"
. "email: $email\n"
. "phone: $phone\n"
. "message: $message\n";
#mail($pfw_email_to, $pfw_subject, $pfw_message, $pfw_header);
header("Location: http://vmobileautoglass.com/php/successform.php");
You have a typo:
$date = json_decode($response);
if(isset($data->success) AND $data->success==true){
$date should be $data.
Okay, so I've followed a tutorial and just changed some of the variables and created them. I have a HTML and PHP code below, what's wrong with the PHP that's causing it not too work? Any ideas.
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = " MY EMAIL ";
$email_subject = "FORM SUBMISSION";
// validation expected data exists
if(!isset($_POST['name']) ||
!isset($_POST['address']) ||
!isset($_POST['email']) ||
!isset($_POST['telephone']) ||
!isset($_POST['message'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$name = $_POST['name']; // required
$address = $_POST['address']; // required
$email = $_POST['email']; // required
$telephone = $_POST['telephone']; // not required
$message = $_POST['message']; // required
$error_message = "";
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "Name: ".clean_string($name)."\n";
$email_message .= "Address: ".clean_string($address)."\n";
$email_message .= "Email: ".clean_string($email)."\n";
$email_message .= "Telephone: ".clean_string($telephone)."\n";
$email_message .= "Message: ".clean_string($message)."\n";
// create email headers
$headers = 'From: '.$email."\r\n".
'Reply-To: '.$email."\r\n" .
'X-Mailer: PHP/' . phpversion();
#mail($email_to, $email_subject, $email_message);
?>
<?php
header('Location: MY LINK IS HERE BUT NOT WORKING');
?>
<?php
}
?>
Form
<div class="row contact-form">
<div class="container">
<h3>Get In Touch</h3>
<div class="col-sm-6">
<div class="row">
<form name="email" action="php/mail.php" method="POST">
<div class="col-sm-6">
<p class="input-label">Name <span class="star-red">*</span>
</p>
<input type="text" name="name" id="name" class="form-input-wide" required>
</div>
<div class="col-sm-6">
<p class="input-label">Address</p>
<input type="text" name="address" id="address" class="form-input-wide">
</div>
</div>
<div class="row">
<form name="send-mail" action="php/mail.php" method="POST">
<div class="col-sm-6">
<p class="input-label">Telephone <span class="star-red">*</span>
</p>
<input type="tel" name="telephone" id="telephone" class="form-input-wide" required>
</div>
<div class="col-sm-6">
<p class="input-label">Email <span class="star-red">*</span>
</p>
<input type="email" name="email" id="email" class="form-input-wide" required>
</div>
</div>
<div class="row">
<div class="col-sm-12">
<p class="input-label">Message <span class="star-red">*</span>
</p>
<textarea name="message" id="message" style="width:100% !important;" rows="6" required></textarea>
</div>
</div>
<div class="row">
<div class="col-sm-12">
<input type="submit" class="submit-button">
</div>
</div>
<div class="row">
<div class="col-sm-6">
 
</div>
</div>
</form>
</div>
</div>
</div>
</div>
If there is anything that I need to change? I think the error messages may be breaking the PHP.
It seems there are two things wrong in the PHP script:
!isset($_POST['address`']) ||
needs to be
!isset($_POST['address']) ||
and
if(strlen($messages) < 2) {
needs to be
if(strlen($message) < 2) {
Maybe you can look into the usage of some "form validators" for some better usability. It would be a bit nicer to show the same form again but then with error messages attached to them, in stead of stopping the script with an error message.
EDIT:
For the header redirect: remove the whitespaces. The header('...') function needs to run before any browser output. Now there are whitespaces outputted to the browser. So remove "? >" and "< ? php"
?>
<?php
header('Location: MY LINK IS HERE BUT NOT WORKING');
I am using a simple html form as a contact, and when fields and submitted the form does not clear the fields.
this is my php
I read online in few places and I've learned that I have to use the .reset , but I am not familiar with php a lot. I am not sure where would I add the .reset and how.
<?
$name = $_REQUEST["name"];
$email = $_REQUEST["email"];
$msg = $_REQUEST["msg"];
$to = "example#example.com";
if (isset($email) && isset($name) && isset($msg)) {
$subject = "Message / Closure Film";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= "From: ".$name." <".$email.">\r\n"."Reply-To: ".$email."\r\n" ;
$msg = "Name: $name:<br/> Email: $email <br/> Message: $msg";
$mail = mail($to, $subject, $msg, $headers);
if($mail)
{
echo 'success';
}
else
{
echo 'failed';
}
}
?>
my html
<div id="contact">
<div class="container">
<div class="row-fluid PageHead">
<div class="span12">
<h3>CONTACT US<span> <img src="images/underline.png" alt="______"></span></h3>
</div>
</div>
<div class="row-fluid ContactUs">
<div class="span6 offset3">
<form class="form-horizontal" id="phpcontactform">
<div class="control-group">
<input class="input-block-level" type="text" placeholder="Full Name" name="name" id="name">
</div>
<div class="control-group">
<input class="input-block-level" type="email" placeholder="Email" name="email" id="email">
</div>
<div class="control-group">
<textarea class="input-block-level" rows="10" name="message" placeholder="Your Message" id="message"></textarea>
</div>
<div class="control-group">
<p>
<input class="btn btn-danger btn-large" type="submit" value="Send Message">
</p>
<span class="loading"></span> </div>
</form>
</div>
added this to the head of my html, but didnt get any result
<script type="javascript">
$('#phpcontactform').trigger("reset");
</script>
Try:
<script type="javascript">
$(document).ready(function() {
$('#phpcontactform')[0].reset();
});
</script>
I'm not sure if you're trying to do an ajax submit and keep the user on the page, or just submit the form. But the line you're looking for is $("#phpcontactform")[0].reset();, you could wrap that in a $(document).ready() if you needed to!
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 8 years ago.
I'm new to coding so i basically copied the below code from various sources until i thought it may work but it's not, what i need is to send form info from an html page to an email once filled. Please Help.
php code for the form:
<?php ini_set('display_errors',1);?>
$emailTo = '<abc#gmail.com>';
if($_SERVER['REQUEST_METHOD'] == 'POST') {
$Name=$_POST['Name'];
$Mobile=$_POST['Mobile'];
$Email=$_POST['Email'];
$City=$_POST['City'];
$emailIsValid = preg_match('/^[^0-9][A-z0-9_]+([.][A-z0-9_]+)*[#][A-z0-9_]+([.][A-z0-9_]+)*[.][A-
z]{2,4}$/', $Email);
if($Name && $Mobile $Email && $emailIsValid && $City){
$subject = "[Contact via Site] $subject";
$body = "Name: $Name <br /> Email: $Email <br /> Mobile: $Mobile <br /> City: $City";
$headers = 'MIME-Version: 1.1' . PHP_EOL;
$headers .= 'Content-type: text/html; charset=utf-8' . PHP_EOL;
$headers .= "From: $name <$Email>" . PHP_EOL;
$headers .= "Return-Path: $emailTo" . PHP_EOL;
$headers .= "Reply-To: $Email" . PHP_EOL;
mail($emailTo, $subject, $body, $headers);
$emailSent = true;
} else {
$hasError = true;
}
}
?>
html code:
Thank You, we'll call you soon
error pls check all fields.
<div class="col-md-6 col-md-offset-3">
<form action="<?php echo $_SERVER['/Thanks.html']; ?>" id="FORM" class="form-horizontal"
role="form" method="post" style="height: 300px; padding-left:10px; padding-top:10px;">
<div class="formContainer" >
<div class="label">
<label>Name: </label>
</div>
<div class="textbox">
<input type="text" id=name name="Name"/>
</div> <br>
<div class="label">
<label>Mobile: </label>
</div>
<div class="textbox">
<input type="tel" id=mobile name="Mobile" />
</div> <br>
<div class="label">
<label>Email: </label>
</div>
<div class="textbox">
<input type="email" id=email name="Email"/>
</div> <br>
<div class="label">
<label>City: </label>
</div>
<div class="textbox">
<input type="text" id=city name="City"/>
</div> <br>
<div class="submit">
<input type="submit" value="Submit">
</div>
</form>
Just use this PHPMailer class.. It is optimized and fully featured to send emails, set attachments, change header attributes, etc... without even having to know how they are actually implemented:
https://github.com/PHPMailer/PHPMailer
I've looked at various solutions but I just can't get my contact form to work. **The issue im having is that the email wont actually send out to me, everything works client side but I dont get the email. So I have come here to surely get the duplicate question label. Here is my code:
<form method="post" class="reply" id="contact" action="process.php">
<fieldset>
<div class="row">
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-12">
<label>Name: <span>*</span></label>
<input class="form-control" id="name" name="name" type="text" value="" required>
</div>
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-12">
<label>Email: <span>*</span></label>
<input class="form-control" type="email" id="email" name="email" value="" required>
</div>
</div>
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<label>Subject: <span>*</span></label>
<input class="form-control" id="subject" name="subject" type="text" value="" required>
</div>
</div>
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<label>Message: <span>*</span></label>
<textarea class="form-control" id="text" name="text" rows="3" cols="40" required></textarea>
</div>
</div>
</fieldset>
<button class="btn btn-normal btn-color submit bottom-pad" type="submit">Send</button>
<div class="success alert-success alert" style="display:none">Your message has been sent successfully.</div>
<div class="error alert-error alert" style="display:none">E-mail must be valid and message must be longer than 100 characters.</div>
<div class="clearfix">
</div>
</form>
Here is my process.php
<?php
// Email Submit
// Note: filter_var() requires PHP >= 5.2.0
if ( isset($_POST['email']) && isset($_POST['name']) && isset($_POST['subject']) && isset($_POST['text']) && filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) ) {
// detect & prevent header injections
$test = "/(content-type|bcc:|cc:|to:)/i";
foreach ( $_POST as $key => $val ) {
if ( preg_match( $test, $val ) ) {
exit;
}
}
// PREPARE THE BODY OF THE MESSAGE
$message = '<html><body>';
$message .= '<table rules="all" style="border-color: #666;" cellpadding="10">';
$message .= "<tr style='background: #eee;'><td><strong>Name:</strong> </td><td>" . strip_tags($_POST['name']) . "</td></tr>";
$message .= "<tr><td><strong>Email:</strong> </td><td>" . strip_tags($_POST['email']) . "</td></tr>";
$message .= "<tr><td><strong>Message:</strong> </td><td>" . htmlentities($_POST['text']) . "</td></tr>";
$message .= "</table>";
$message .= "</body></html>";
// CHANGE THE BELOW VARIABLES TO YOUR NEEDS
$to = 'iknowichange#this.com';
$subject = $_POST['subject'];
$headers = "From: " . $_POST['email'] . "\r\n";
$headers .= "Reply-To: ". strip_tags($_POST['req-email']) . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
mail($to, $subject, $message, $headers);
}
?>
I am completely new to forms, so thanks in advance for the help. If there are any resources that you can suggest that would be great. Thanks!
It looks like you just forgot to include an action in your form element.
(Unless your binding an onsubmit event somewhere else)
Try
<form method="post" class="reply" id="contact" action="process.php">
You haven't set any action in your form element. You've to set the path of your "process.php" in form element like following:
<form action="process.php" method="post" id="contact" class="reply">
...
</form>
More about form: http://www.w3schools.com/html/html_forms.asp