PHP / HTML Contact form - blank fields - php

I've made a custom contact form with PHP working with HTML but I am getting some blank fields when sending an email.
Actually, I have made a table of 4 including Name, Email, Subject, and Message but the fields including Subject & Message are being sent empty.
I would appreciate any help given.
Thank you.
Html Code:
<form action="mail.php" method="post">
<div class="form-block clearfix">
<input type="text" placeholder="name*" id="name" />
<input type="text" placeholder="email*" id="email" />
</div>
<div class="form-block clearfix">
<input type="text" placeholder="subject*" id="sub" />
</div>
<div class="form-block">
<textarea cols="1" rows="1" placeholder="Message*" id="message" ></textarea>
</div>
<div class="submit-btn">
<input type="button" id="submit" value="submit" class="detail-submit"/>
</div>
</form>
PHP:
<?php
$to = "My email";
$from = "";
$cc = "";
$subject = "Contact us form";
$errmasg = "";
$name = htmlentities(trim($_POST['name']));
$email = htmlentities(trim($_POST['email']));
$sub = htmlentities(trim($_POST['sub']));
$message = htmlentities(trim(nl2br($_POST['message'])));
if($email){
$message = "<table border='0' cellpadding='2' cellspacing='2' width='600'>
<tr><td>Name: ".$name." </td></tr>
<tr><td>Email: ".$email."</td></tr>
<tr><td>Subject: ".$sub."</td></tr>
<tr><td>Message:".$message."</td></tr>
</table>";
}else{
$errmasg = "No Data";
}
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From:'.$from . "\r\n";
$headers .= 'Cc:'.$cc . "\r\n";
if($errmasg == ""){
if(mail($to,$subject,$message,$headers)){
echo 1;
}else{
echo 'Error occurred while sending email';
}
}else{
echo $errmasg;
}
?>

You need to add an element NAME in the subject and message field.
Just replace your form code to below code:
<form action="mail.php" method="post">
<div class="form-block clearfix">
<input type="text" placeholder="name*" id="name" />
<input type="text" placeholder="email*" id="email" />
</div>
<div class="form-block clearfix">
<input type="text" name="sub" placeholder="subject*" id="sub" />
</div>
<div class="form-block">
<textarea cols="1" rows="1" name="message" placeholder="Message*" id="message" ></textarea>
</div>
<div class="submit-btn">
<input type="button" id="submit" value="submit" class="detail-submit"/>
</div>
</form>

Each form element that you wish to appear in the POST array data when the form is submitted ( and thus to be available using $_POST['fieldname'] ) needs a name attribute. The ID attribute is optional but of limited use in many situations - certainly not required in a traditional form submission such as this..
The input button submit will NOT submit the form unless you do so with Javascript. It might be better to use a submit button as below.
<form action="mail.php" method="post">
<div class="form-block clearfix">
<input type="text" placeholder="name*" name="name" />
<input type="text" placeholder="email*" name="email" />
</div>
<div class="form-block clearfix">
<input type="text" placeholder="subject*" name="sub" />
</div>
<div class="form-block">
<textarea cols="100" rows="1" placeholder="Message*" name="message" ></textarea>
</div>
<div class="submit-btn">
<input type="submit" name="submit" value="Submit" class="detail-submit"/>
</div>
</form>
Not sure why you are having issues ~ perhaps the following will offer enlightenment. It is tested to the point of failing to send an email ( no local mailserver on dev machine at present ) and is an "all in one page" demo where the PHP emulates the original form action mail.php
<?php
/* this emulates mail.php */
error_reporting( E_ALL );
/* use a session variable */
session_start();
/* for testing single page demo */
$singlepage=true;
if( $_SERVER['REQUEST_METHOD']=='POST' ){
$to = "My email";
$from = $cc = '';
$subject = "Contact us form";
$errmasg = '';
/* filter POST data */
$args=array(
'name' => FILTER_SANITIZE_STRING,
'email' => FILTER_SANITIZE_EMAIL,
'sub' => FILTER_SANITIZE_STRING,
'message' => FILTER_SANITIZE_STRING
);
$_POST=filter_input_array( INPUT_POST, $args );
/* assign as variables */
extract( $_POST );
$name = htmlentities(trim($name));
$email = htmlentities(trim($email));
$sub = htmlentities(trim($sub));
$message = htmlentities(trim(nl2br($message)));
if( $email ){
$message = "<table border='0' cellpadding='2' cellspacing='2' width='600'>
<tr><td>Name: ".$name." </td></tr>
<tr><td>Email: ".$email."</td></tr>
<tr><td>Subject: ".$sub."</td></tr>
<tr><td>Message:".$message."</td></tr>
</table>";
}
# REMOVE THIS LINE or COMMENT IT OUT FOR REAL USAGE
#exit( sprintf("<pre>%s\n%s</pre>",$message, print_r( $_POST,true ) ) );
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From:'.$from . "\r\n";
$headers .= 'Cc:'.$cc . "\r\n";
if($errmasg == ""){
if( mail( $to, $subject, $message, $headers ) ){
$_SESSION['mailsent']=1;
}else{
$_SESSION['mailsent']=2;
}
}else{
$_SESSION['mailsent']=3;
}
/*
If you are using mail.php then use a `header` to redirect the user
back to the contact page - assumed to be called `contact.php`
*/
if( !$singlepage ) header( 'Location: contact.php' );
}
?>
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='utf-8' />
<title>POST to email</title>
</head>
<body>
<!-- removed attribute action as this works on same page here -->
<form method="post">
<?php
if( !empty( $_SESSION['mailsent'] ) ){
switch( $_SESSION['mailsent'] ){
case 1:$message='Your message was sent successfully';break;
case 2:$message='Sorry - we had a problem sending your email';break;
case 3:$message='Bogus - no data';break;
}
printf( '<h1>%s</h1>', $message );
unset( $_SESSION['mailsent'] );
}
?>
<div class="form-block clearfix">
<input type="text" placeholder="name*" name="name" /><!-- element has a NAME -->
<input type="text" placeholder="email*" name="email" />
</div>
<div class="form-block clearfix">
<input type="text" placeholder="subject*" name="sub" />
</div>
<div class="form-block">
<textarea cols="100" rows="1" placeholder="Message*" name="message" ></textarea>
</div>
<div class="submit-btn">
<input type="submit" class="detail-submit" /><!-- a SUBMIT button -->
</div>
</form>
</body>
</html>
Typical output for debugging
Array
(
[name] => fred flintstone
[email] => fred#bedrock.com
[sub] => betty had better bake a cake
[message] => hey betty
)

Related

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

HTML contact form, PHP code

I was trying to send multiple vars from my html form to my email by using PHP. The problem is that I do not know anything about PHP. I found a simple form which worked for me, but it contained only 3 vars, subject, email and message. This time however I have 7.
Here's the code.
<?php
$projectname = htmlentities($_POST['projectname']);
$projectemail = trim(strip_tags($_POST['projectemail']));
$projectphone = htmlentities($_POST['projectphone']);
$projectcompany = htmlentities($_POST['projectcompany']);
$projecttype = trim(strip_tags($_POST['projecttype']));
$projecttimeline = htmlentities($_POST['projecttimeline']);
$aboutproject = htmlentities($_POST['aboutproject']);
$message = "{$projectname}{$projectphone}{$projectcompany}{$projecttimeline}{$aboutproject}";
$subject = $projecttype;
$to = 'myemail#gmail.com';
$body = <<<HTML
$message
HTML;
$headers = "From: $projectemail\r\n";
$headers .= "Content-type: text/html\r\n";
mail($to, $subject, $body, $headers);
header('Location: thanks.html');
?>
And the corresponding HTML
<form id="formproject" action="thank_you_project.php" method="post">
<label for="name">Name</label>
<input type="text" id="projectname" name="name">
<label for="email">Email</label>
<input type="text" id="projectemail" name="email">
<label for="phone">Phone</label>
<input type="text" id="projectphone" name="phone">
<label for="company">Company</label>
<input type="text" id="projectcompany" name="company">
<label for="typeofproject">Type of project</label>
<input type="text" id="projecttype" name="typeofproject">
<label for="timeline">Timeline</label>
<input type="text" id="projecttimeline" name="timeline">
<label for="message">Message</label>
<textarea name="message" id="aboutproject" cols="30" rows="10"></textarea>
<input type="submit" id="projectsend" value="Send"></input>
</form>
</div> <!-- end form -->
Edited the PHP, left out the body and put message in mail instead, still not working.
<?php
$projectname = htmlentities($_POST['projectname']);
$projectemail = trim(strip_tags($_POST['projectemail']));
$projectphone = htmlentities($_POST['projectphone']);
$projectcompany = htmlentities($_POST['projectcompany']);
$projecttype = trim(strip_tags($_POST['projecttype']));
$projecttimeline = htmlentities($_POST['projecttimeline']);
$aboutproject = htmlentities($_POST['aboutproject']);
$message = "{$projectname}{$projectphone}{$projectcompany}{$projecttimeline}{$aboutproject}";
$subject = $projecttype;
$to = 'albermy145#alberttomasiak.be';
$headers = "From: $projectemail\r\n";
$headers .= "Content-type: text/html\r\n";
mail($to, $subject, $message, $headers);
header('Location: thanks.html');
?>
Try this,
<?php
$to = 'myemail#gmail.com';
$projectname = htmlentities($_POST['projectname']);
$projectemail = trim(strip_tags($_POST['projectemail']));
$projectphone = htmlentities($_POST['projectphone']);
$projectcompany = htmlentities($_POST['projectcompany']);
$projecttype = trim(strip_tags($_POST['projecttype']));
$projecttimeline = htmlentities($_POST['projecttimeline']);
$aboutproject = htmlentities($_POST['aboutproject']);
$header = "From: $projectemail \r\n";
$subject = $projecttype;
$message = "
<html>
<head>
<title></title>
</head>
<body>
<p>Your HTML</p>
Project Name : $projectname
<br/>
Project Email : $projectemail
<br/>
Project Phone : $projectphone
...
...
...
...
</body>
</html>
";
$message .= "";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-type: text/html\r\n";
$retval = mail ($to,$subject,$message,$header);
if($retval == true)
{
header('Location: thanks.html');
exit();
}
?>
}
HTML
<form id="formproject" action="thank_you_project.php" method="post">
<label for="name">Name</label>
<input type="text" id="projectname" name="projectname">
<label for="email">Email</label>
<input type="text" id="projectemail" name="projectemail">
<label for="phone">Phone</label>
<input type="text" id="projectphone" name="projectphone">
<label for="company">Company</label>
<input type="text" id="projectcompany" name="projectcompany">
<label for="typeofproject">Type of project</label>
<input type="text" id="projecttype" name="projecttype">
<label for="timeline">Timeline</label>
<input type="text" id="projecttimeline" name="projecttimeline">
<label for="message">Message</label>
<textarea name="aboutproject" id="aboutproject" cols="30" rows="10"></textarea>
<input type="submit" id="projectsend" value="Send"></input>
</form>
</div> <!-- end form -->
Add all variables into $body (which is the third param of mail function).
//$body = <<<HTML
//$message <br>
//$projectname <br>
//$projectcompany<br>
//...
//HTML;
Or the second way is to add these vars into the third mail param directly.
mail($to, $subject, $message, $headers);
// this code is updated as I wrote in my comment below.
the form posted the value of input is using 'name' attribute as identifier of value,
<form id="formproject" action="thank_you_project.php" method="post">
<label for="name">Name</label>
<input type="text" id="projectname" name="name">
<label for="email">Email</label>
<input type="text" id="projectemail" name="email">
<label for="phone">Phone</label>
<input type="text" id="projectphone" name="phone">
<label for="company">Company</label>
<input type="text" id="projectcompany" name="company">
<label for="typeofproject">Type of project</label>
<input type="text" id="projecttype" name="typeofproject">
<label for="timeline">Timeline</label>
<input type="text" id="projecttimeline" name="timeline">
<label for="message">Message</label>
<textarea name="message" id="aboutproject" cols="30" rows="10"></textarea>
<input type="submit" id="projectsend" value="Send"></input>
</form>
<!-- end form -->
<?php
$projectname = htmlentities($_POST['name']);
$projectemail = trim(strip_tags($_POST['email']));
$projectphone = htmlentities($_POST['phone']);
$projectcompany = htmlentities($_POST['company']);
$projecttype = trim(strip_tags($_POST['typeofproject']));
$projecttimeline = htmlentities($_POST['timeline']);
$aboutproject = htmlentities($_POST['message']);
?>

Contact form not working while click on submit button

I was working with an HTML contact form, its action is a mail sending php script. But it is not working when i am clicking on the send button. I couldn't find any possible here. Please someone help me to fix this.
HTML form
<div class="contact-form">
<form action="sendmail.php" method="post">
<div class="control-group"><label class="nameLabel" for="name">Name</label> <input id="name" name="name" type="text" /></div>
<div class="control-group"><label class="emailLabel" for="email">Email</label> <input id="email" name="email" type="text" /></div>
<div class="control-group"><label class="messageLabel" for="message">Message</label><textarea id="message" name="message"></textarea></div>
<div class="control-group"><button type="submit">Send</button></div>
</form>
<h1 class="status-message-contact"></h1>
</div>
PHP code
<?php
if(isset($_POST['submit']))
{
$name = $_POST['name'];
$email = $_POST['email'];
$query = $_POST['message'];
$email_from = $name.'<'.$email.'>';
$to="sarath.sarigama#gmail.com";
$subject="WEB Enquiry!";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: ".$email_from."\r\n";
$message="
Name:
$name
<br>
Email-Id:
$email
<br>
Message:
$query
";
if(mail($to,$subject,$message,$headers))
header("Location:../index.php?msg=Successful Submission! Thankyou for contacting us.");
else
header("Location:../index.php?msg=Error To send Email !");
//contact:-your-email#your-domain.com
}
?>
you dont have submit button name ,change name of the button
<button name='submit' type="submit">Send</button>
or use input tag
<input name='submit' type='submit'>
Replace
<button type="submit">Send</button>
with
<input type="submit" value="Send" />
And maybe add
<input type="hidden" name="submit" value="1" />
For the isset( $_POST['submit'] )

HTML/CSS/PHP contact form is sending blank fields

I'm by no means a web designer/coder, but I have enough general knowledge to get my website going. Just a simple portfolio website.
I'm having some problems with my contact form though. It's strictly CSS, HTML and PHP but when I put in the information (name, email and message) to test it, it sends me an email with those field headings, but no content. For example, the email displays as;
Name:
Email:
Message:
Even when filled out with information.
I have a feeling it's something small and a stupid error, so any help is greatly appreciated!
HTML
<div id="contact-area">
<form method="post" action="contactengine.php">
<input type="text" name="Name" id="Name" placeholder="Name"/>
<input type="text" name="Email" id="Email" placeholder="Email"/>
</form>
</div>
<div id="contact-area2">
<form method="post" action="contactengine.php">
<textarea name="Message" placeholder="Message" rows="20" cols="20" id="Message"></textarea>
</form>
<div id="submit-button">
<form method="post" action="contactengine.php">
<input type="submit" name="submit" value="SUBMIT" class="submit-button" />
</form>
</div>
</div>
PHP
<?php
$EmailFrom = "contact#brettlair.ca";
$EmailTo = "contact#brettlair.ca";
$Subject = "Contact Form";
$Name = Trim(stripslashes($_POST['Name']));
$Email = Trim(stripslashes($_POST['Email']));
$Message = Trim(stripslashes($_POST['Message']));
// validation
$validationOK=true;
if (!$validationOK) {
print "<meta http-equiv=\"refresh\" content=\"0;URL=index.htm\">";
exit;
}
// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $Message;
$Body .= "\n";
// send email
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
// redirect to success page
if ($success){
print "<meta http-equiv=\"refresh\" content=\"0;URL=index.html\">";
}
else{
print "<meta http-equiv=\"refresh\" content=\"0;URL=index.htm\">";
}
?>
You have your form fields in one form, and the submit button in another. They need to be in the same form.
<form method="post" action="contactengine.php">
<input type="text" name="Name" id="Name" placeholder="Name"/>
<input type="text" name="Email" id="Email" placeholder="Email"/>
</div>
<div id="contact-area2">
<textarea name="Message" placeholder="Message" rows="20" cols="20" id="Message"></textarea>
<div id="submit-button">
<input type="submit" name="submit" value="SUBMIT" class="submit-button" />
</form>
$Name = Trim(stripslashes($_POST['Name']));
There is no Trim function, should be trim (lowercase).

PHP Sendmail Form Returns no Results

I'm extremely new to php, roughly 2 days experience.
My conundrum is this:
My html is:
<div class="form">
<form action="php/sendmail.php" method="post" id="contactwidget">
<div class="inp_r"><input type="text" name="wname" id="wname" value="Name" size="22" tabindex="11" alt="Name" /></div>
</div>
<div class="inp_r"><input type="text" name="wemail" id="wemail" value="Email" size="22" tabindex="12" alt="Email" /></div>
</div>
<table>
<tr>
<td class="text_m"><textarea name="wmessage" id="wmessage" cols="28" rows="6" tabindex="13" title="Quick Message">Quick Message</textarea></td>
<td class="text_r"></td>
</tr>
</table>
<div class="loading"></div>
<div><input type="hidden" name="wcontactemail" id="wcontactemail" value="info#joshuas.com" /></div>
<div><input type="hidden" name="wcontacturl" id="wcontacturl" value="php/sendmail.php" /></div>
<div><span>Send</span></div>
</form>
</div>
My php is this:
<?php
$email = $_POST['Email'] ;
$name = $_POST['Name'] ;
$message = $_POST['wmessage'] ;
$headers .= 'From: ' . $from . "\r\n";
mail( "info#gmail.com", "Message from Joshua",
$message, "From: $email" );
?>
All that is returning is an email with "Message from Joshua"
Any ideas??
you have to redirect the page in php script.
header( "location=wherever.php" );
IF you want to test the output use this: http://papercut.codeplex.com/

Categories