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
)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.