Form submission not working - php

I have a problem with the Form I'm trying to create. Basically, it does not allow me to send the email to the recipient, even though the PHP code is correct. Few people from SO already tried to help, but it seems the code is not working.
<?php
$error = false;
$sent = false;
if(isset($_POST['submit'])) {
if(empty($_POST['name']) || empty($_POST['email']) || empty($_POST['comments'])) {
$error = true;
}
else {
$to = "linardsberzins#gmail.com";
$name = trim($_POST['name']);
$email = trim($_POST['email']);
$comments = trim($_POST['comments']);
$subject = "Contact Form";
$message = "Name: $name \r\n Email: $email \r\n Comments: $comments";
$headers = "From:" . $name;
$mailsent = mail($to, $subject, $message, $headers);
if($mailsent) {
$sent = true;
}
}
}
?>
HTML:
<?php if($error == true){ ?>
<p class="error">Text</p>
<?php } if($sent == true) { ?>
<p class="sent">Text</p>
<?php } ?>
<div id="form">
<form name="contact" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<fieldset>
<h4>Contact Me!</h4>
<label for="name">Name:</label>
<input type="text" name="name" id="name"/>
<label for="email"/>Email:</label>
<input type="text" name="email" id="email"/>
<label for="comments" id="comments">Comments:</label>
<textarea name="comments" id=""></textarea>
<fieldset>
<input class="btn" type="submit" name="submit" class="submit" value="Send email"/>
<input class="btn" type="reset" value="Reset"/>
</fieldset>
</fieldset>
</form>

Do not rely on submit button as not all browsers send that button as a POST, change to other input:
if(isset($_POST['name'])) {

Related

Send mail form not working

$location ="/contact.php";
if(isset($_POST[$form_names['email']], $_POST[$form_names['subject']], $_POST[$form_names['mess']])){
if($csrf->check_valid('post')){
$email = trim(htmlspecialchars($_POST[$form_names['email']]));
$subject = trim(htmlspecialchars($_POST[$form_names['subject']]));
$mess = trim(htmlspecialchars($_POST[$form_names['mess']]));
if ($email == '' && $subject == '' && $mess == ''){
// $message = "Success";
$headers = "From: {$email}\r\n".
"Content-Type: text/html; charset=ISO-8859-1\r\n";
$current_ip = $_SERVER['REMOTE_ADDR'];
$html_message = nl2br($mess);
$sub = "CONTACT FORM: ".$subject;
//send email
$to = SITE_EMAIL;
$the_mess = "IP: ".$current_ip." <br />
FROM: ".$email."<br />
MESSAGE: <p />"."$html_message";
mail($to, $sub, $the_mess, $headers);
$message = "<div class='alert alert-success'><button type='button' class='close' data-dismiss='alert'>x</button>Thank you, your message has been sent successfully.</div>";
} else {
$message = "<div class='alert alert-danger'><button type='button' class='close' data-dismiss='alert'>x</button>Please complete all required fields.</div>";
}
}
And form:
<form action="<?php echo $location; ?>" method="post" class="form-horizontal" enctype="multipart/form-data">
<label for="subject">Choose Department:</label><br>
<select id="subject" name="<?php echo $form_names['subject']; ?>" class="form-control">
<option value="activation">Account activation</option>
<option value="suspension">Account suspension</option>
<option value="auctions">Auctions</option>
<option value="complaint">Complaints</option>
<option value="others">Others</option>
</select>
<br />
<label for="email">Email Address</label>
<input type="text" class="form-control" name="<?php echo $form_names['email']; ?>" autocomplete="off" value="<?php echo htmlspecialchars($email); ?>" placeholder="Enter your Email Address">
<br />
<label for="email">Please describe your problem</label>
<textarea type="text" class="form-control" style="height:111px;" name="<?php echo $form_names['mess']; ?>" placeholder="Your Message" required="required"><?php echo htmlspecialchars($mess); ?></textarea>
<input class="btn btn-danger" type="submit" name="submit" value="Send Message" />
</form>
When I submit the form, it outputs the second $message:
Please complete all required fields.
I cannot find the issue here. I tried print_r(error_get_last()); and it does not show any errors. Any help would be much appreciated. Thank you very much!
Your condition is if ($email == '' && $subject == '' && $mess == ''){, this means that the mail is sent only if all the fields are empty.
I'm assuming the correct condition should be like if ($email != '' && $subject != '' && $mess != ''){

adding new field to contact.php

I bought a WP theme a few months ago and it works great! I am trying to edit the contact.php / sendmail.php
I have successfully added in a field, it shows up in the body of the email and sends correctly. However, I am have a lot of trouble getting the new field "school(s) of interest" to highlight properly (with hidden text) when the field hasn't been filled. The contact form in question can be found here: http://www.northbrookmontessori.org/school-tours/
sendmail.php
<?php
//validate fields
$errors = '';
//has the form's submit button been pressed?
if( !empty( $_POST['myformsubmit'] ) )
{
// HAS THE SPAM TRAP FIELD BEEN FILLED IN?
if( !empty( $_POST['favouriteColour'] ) )
{
exit;
}
$myemail = $_POST['youremail'];
$thankyou = $_POST['thankyou'];
$formerror = $_POST['formerror'];
if(empty($_POST['name']) ||
empty($_POST['school']) ||
empty($_POST['email']) ||
empty($_POST['message']))
{
$errors .= "\n Error: all fields are required";
}
$name = $_POST['name'];
$school = $_POST['school'];
$email_address = $_POST['email'];
$message = $_POST['message'];
if (!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", $email_address)) { $errors .= "\n Error: Invalid email address"; } //send email
if( empty($errors))
{
$to = $myemail;
$email_subject = "Contact form submission from $name";
$email_body = "$name has sent you this email through the contact form: \n \n".
"Name: $name \n".
"School(s) of interest: $school \n".
"Email: $email_address \nMessage: \n\n$message";
$headers = "From: $email_address";
mail($to,$email_subject,$email_body,$headers);
//redirect to the 'thank you' page
header('Location: ' . $thankyou);
}
else {
header('Location: ' . $formerror);
}
}
?>
contact.php
<!-- ***** CONTACT -->
<div class="block_wrapper <?php if($bb_animation == 'yes'){ echo 'animation' . $anim1_number; ?> animated <?php } ?><?php echo ' '.$custom_width; ?>">
<div class="box_full<?php if($margin_top != ''){echo ' ' . $margin_top;} ?><?php if($margin_bottom != ''){echo ' ' . $margin_bottom;} ?><?php if($custom_classes != NULL){echo ' ' . $custom_classes;} ?>">
<form id="theform" class="form mt35" method="post" action="<?php echo get_template_directory_uri(); ?>/sendmail.php">
<p class="hRow">
<label for="favouriteColour">Favourite colour (do not fill in)</label>
<span><input type="text" name="favouriteColour" id="favouriteColour" value="" /></span>
</p>
<input type="hidden" name="youremail" id="youremail" value="<?php if(!empty($contact_email)){echo $contact_email;} ?>" />
<input type="hidden" name="thankyou" id="thankyou" value="<?php if(!empty($contact_thankyou)){echo $contact_thankyou;} ?>" />
<input type="hidden" name="formerror" id="formerror" value="<?php if(!empty($contact_error)){echo $contact_error;} ?>" />
<p class="name validated">
<label for="name">Name</label>
<span><input type="text" name="name" id="name" /></span>
</p>
<p class="school validated">
<label for="school">School(s) of interest</label>
<span><input type="text" name="school" id="school" /></span>
</p>
<p class="email validated">
<label for="email">E-mail</label>
<span><input type="text" name="email" id="email" /></span>
</p>
<p class="text validated">
<label for="message">Message</label>
<textarea name="message" id="message" rows="50" cols="100"></textarea>
</p>
<p class="submit">
<input type="submit" class="buttonmedium float_r" name="myformsubmit" value="Send Email" />
</p>
<p id="error">* There were errors on the form, please re-check the fields.</p>
</form>
</div>
<div class="clear"></div>
</div><!-- ***** END CONTACT -->
you need to go to the file:
http://www.northbrookmontessori.org/wp-content/themes/modular/js/settings.js?ver=4.2.2
and edit the top where it says:
// Place ID's of all required fields here.
add in school

PHP Mail function didn't work

I have use php contact form for my web site.but it's doesn't work properly.when i fill all the field correctly and submit. it's display error message "Invalid data" .
<?php
$action=$_REQUEST['action'];
if ($action=="")
{
?>
<form action="" method="POST" enctype="multipart/form-data">
<input type="hidden" name="action" value="submit">
Your name:<br>
<input name="name" type="text" value="" size="30" required/><br>
Your email:<br>
<input name="email" type="email" value="" size="30" required /><br>
Your message:<br>
<textarea name="message" rows="7" cols="30" required></textarea><br> <br>
<input type="submit" value="Send email" class="topbarbtn"/>
</form>
<?php
$subject=$_REQUEST['name'];
$email=$_REQUEST['email'];
$body=$_REQUEST['message'];
}else{
$to = "abc#abc.com";
$subject = $subject;
$from = $email;
$message = $body;
if (($from=="")||($subject=="")||($message==""))
{
echo '<script type="text/javascript">alert("Invalid Details");</script>' ;
} else{
$headers = "From: " . $from . "\r\n";
$body .= $message;
mail($to, $subject, $body, $headers);
}
?>
Like this it works:
<?php
$action=$_REQUEST['action'];
if ($action=="") {?>
<form action="" method="POST" enctype="multipart/form-data">
<input type="hidden" name="action" value="submit">
Your name:<br>
<input name="name" type="text" value="" size="30" required/><br>
Your email:<br>
<input name="email" type="email" value="" size="30" required /><br>
Your message:<br>
<textarea name="message" rows="7" cols="30" required></textarea><br> <br>
<input type="submit" value="Send email" class="topbarbtn"/>
</form>
<?php
}
else {
$to = "...mail address...";
$subject = $_REQUEST['name'];
$from = $_REQUEST['email'];
$message = $_REQUEST['message'];
if (($from=="")||($subject=="")||($message=="")) {
echo '<script type="text/javascript">alert("Invalid Details");</script>' ;
}
else{
$headers = "From: " . $from . "\r\n";
$body .= $message;
mail($to, $subject, $body, $headers);
}
}
?>
Noticed a flaw in your logics - you try to set $from as an $email, while you don't get a variable $email
in else statement add this lines:
$from = $_REQUEST['email'];
Plus if mail still is not sending you should considerthat some mail servers, such as qmail, will reject your message if it uses \r\n.
So you should try to use just \n or \n\n as a linebreak in a header.

PHP Sending Form Script

I am having a bit trouble getting the PHP script to work. I am making a very basic form just collecting name and email
<form action"email.php" method="POST" id="signup-form">
<p><input type="text" id="name" name="name" placeholder="name" /></p>
<p><input type="email" id="email" name="email" placeholder="email address" /></p>
<p class="form-note"><em>* we will only email you when our store is up</em></p>
<p><input type="submit" value="Get Notified" /></p>
</form>
My PHP script is
<?php
$error = false;
$sent = false;
if(isset($_POST['submit'])) {
if(empty($_POST['name']) || empty($_POST['email'])) {
$error = true;
}
else {
$to = "my.name#gmail.com";
$name = trim($_POST['name']);
$email = trim($_POST['email']);
$subject = "New Subscriber";
$message = "Name: $name \r\n Email: $email";
$headers = "From:" . $name;
$mailsent = mail($to, $subject, $message, $headers);
if($mailsent) {
$sent = true;
}
}
}
?>
I'm using a Linux hosting company, net registry. I tried to get the PHP errors turned on, but couldn't see how in my cpanel. The mail is not sent, but I have no way of seeing the error preventing it.
You have an error in your html. Try:
<form action="email.php"
Change your HTML to this and try
<form action="email.php" method="POST" id="signup-form">
<p><input type="text" id="name" name="name" placeholder="name" /></p>
<p><input type="email" id="email" name="email" placeholder="email address" /></p>
<p class="form-note"><em>* we will only email you when our store is up</em></p>
<p><input type="submit" value="Get Notified" /></p>
</form>

wordpress hard coded contact form failing

I decided to hard code a contact form for my Wordpress blog. I created a template and it is as follows. I just can't see the issue with it despite taking some time to look. Any help would be greatly appreciated. Also, I haven't forgotten to sanitise input. I'll add that function when i see the issue here. Thanks in advance...
Incidentally, I get this message: Parse error: syntax error, unexpected 'else' (T_ELSE) in F:\xampp\htdocs\new_theme\wp-content\themes\twentytwelve_child\contact.php on line 54
<?php /* Template Name: contact */?>
<?php get_header(); ?>
<?php
if(isset($_POST["submitted"])){
if($_POST["contactName"] === ""){
$nameError = "Please enter your name.";
$hasError = true;
}
else $name = $_POST['contactName'];
if($_POST["email"] === ""){
$emailError = "Please enter your email address.";
$hasError = true;
}
else if (!preg_match("/^[[:alnum:]][a-z0-9_.-]*#[a-z0-9.-]+\.[a-z]{2,4}$/i", $_POST['email'])) {
$emailError = "You entered an invalid email address.";
$hasError = true;
}
else $email = $_POST['email'];
if($_POST['comments'] === ""){
$commentError = "Please enter a message.";
$hasError = true;
}
else $comments = $_POST["comments"];
}
if(!isset($hasError)) {
$emailTo = "mathornley#gmail.com";
$subject = "From Android Scoop - From: $name";
$body = "Name: $name \n Email: $email \n Comments: $comments";
$headers = "From: $name <$emailTo> \n Reply-To: $email";
wp_mail($emailTo, $subject, $body, $headers);
$emailSent = true;
}
?>
<?php if(isset($emailSent) && $emailSent == true){ ?>
<div class="thanks">
<p>Thanks, your email was sent successfully.</p>
</div>
<?php } ?>
<?php else{?>
<?php if(isset($hasError){?> <p class="error">Sorry, an error occured.<p>
<div class="form_left">
<form action="<?php the_permalink();?>" method="post">
<label><span class="requiring">*</span>Name:</label>
<input type="text" name="contactName"/>
<?php if($nameError)?> <span class="error"><?=$nameError;?></span>
<label><span class="requiring">*</span>Email</label>
<input type="text" name="email"/>
<?php if($emailError)?><span class="error"><?=$emailError;?></span>
<label>Phone</label><input type="text" name="phone"/>
<input type="submit" value="Send your message"/>
<input type="hidden" name="submitted" value="true" />
</div>
<div class="form_right">
<label><span class="requiring">*</span>Message:</label>
<textarea name="comments" rows="20" cols="30" class="required requiredField"></textarea>
<?php if($commentError)?><span class="error"><?=$commentError;?></span>
</form>
</div>
<?php }} ?>
There are two syntax errors:
<?php }else{
if(isset($hasError)){?>
1,
<?php } ?>
<?php else{?>
You can't close the PHP tags, then open it again starting with else when using brackets. If you use alternative if/then statements, then this is possible
2,
if(isset($hasError)
You're missing the closing parenthesis on the if statement
Full:
<?php
if(isset($emailSent) && $emailSent == true){
echo '<div class="thanks"><p>Thanks, your email was sent successfully.</p></div>';
}else{
if(isset($hasError)){?>
<p class="error">Sorry, an error occured.<p>
<div class="form_left">
<form action="<?php the_permalink();?>" method="post">
<label><span class="requiring">*</span>Name:</label>
<input type="text" name="contactName"/>
<?php if($nameError)?> <span class="error"><?=$nameError;?></span>
<label><span class="requiring">*</span>Email</label>
<input type="text" name="email"/>
<?php if($emailError)?><span class="error"><?=$emailError;?></span>
<label>Phone</label><input type="text" name="phone"/>
<input type="submit" value="Send your message"/>
<input type="hidden" name="submitted" value="true" />
</div>
<div class="form_right">
<label><span class="requiring">*</span>Message:</label>
<textarea name="comments" rows="20" cols="30" class="required requiredField"></textarea>
<?php if($commentError)?><span class="error"><?=$commentError;?></span>
</form>
</div>
<?php
}
} ?>

Categories