Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
197 views
in Technique[技术] by (71.8m points)

Unable to display error or Success msg in php Contact form code

This is my Code,

i am not getting an error or success msg display after form submission. Rest all code is working fine I am getting mail with attachment, but want to display if the mail sent successfully or fail. as an error on form page...

i am not getting an error or success msg display after form submission. Rest all code is working fine I am getting mail with attachment, but want to display if the mail sent successfully or fail. as an error on form page...

 <p class="statusMsg <?php echo !empty($msgClass)?$msgClass:''; ?>"><?php echo $statusMsg; ?></p>
<?php } ?>

<?php
require 'include/config.php';                                   
$postData = $uploadedFile = $echo = '';
$msgClass = 'errordiv';
if(isset($_POST['submit'])){
 // Get the submitted form data
 $postData = $_POST;
 $email = $_POST['email'];
 $name = $_POST['name'];
 $subject = $_POST['phone2'];
 $message = $_POST['applyfor'];
 
 // Check whether submitted data is not empty
 if(!empty($email) && !empty($name) && !empty($subject) && !empty($message)){
     
     // Validate email
     if(filter_var($email, FILTER_VALIDATE_EMAIL) === false){
         $echo = 'Please enter your valid email.';
     }else{
         $uploadStatus = 1;
         
         // Upload attachment file
         if(!empty($_FILES["attachment"]["name"])){
             
             // File path config
             $targetDir = "resumes/";
             $fileName = basename($_FILES["attachment"]["name"]);
             $targetFilePath = $targetDir . $fileName;
             $fileType = pathinfo($targetFilePath,PATHINFO_EXTENSION);
             
             // Allow certain file formats
             $allowTypes = array('pdf', 'doc', 'docx', 'jpg', 'png', 'jpeg');
             if(in_array($fileType, $allowTypes)){
                 // Upload file to the server
                 if(move_uploaded_file($_FILES["attachment"]["tmp_name"], $targetFilePath)){
                     $uploadedFile = $targetFilePath;
                 }else{
                     $uploadStatus = 0;
                     $statusMsg = 'Sorry, there was an error uploading your file.';
                 }
             }else{
                 $uploadStatus = 0;
                 $statusMsg = 'Sorry, only PDF, DOC, JPG, JPEG, & PNG files are allowed to upload.';
             }
         }
         
         if($uploadStatus == 1){
             
             // Recipient
             $toEmail = $receiverEmail;

             // Sender
             $from = '';
             $fromName = '';
             
             // Subject
             $emailSubject = 'Resume Submitted by '.$name;
             
             // Message 
             $htmlContent ='';

           // Header for sender info
             $headers = "From: $fromName"." <".$from.">";

             if(!empty($uploadedFile) && file_exists($uploadedFile)){
                 
                 // Boundary 
                 $semi_rand = md5(time()); 
                 $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 
                 
                 // Headers for attachment 
                 $headers .= "
MIME-Version: 1.0
" . "Content-Type: multipart/mixed;
" . " boundary="{$mime_boundary}""; 
                 
                 // Multipart boundary 
                 $message = "--{$mime_boundary}
" . "Content-Type: text/html; charset="UTF-8"
" .
                 "Content-Transfer-Encoding: 7bit

" . $htmlContent . "

"; 
                 
                 // Preparing attachment
                 if(is_file($uploadedFile)){
                     $message .= "--{$mime_boundary}
";
                     $fp =    @fopen($uploadedFile,"rb");
                     $data =  @fread($fp,filesize($uploadedFile));
                     @fclose($fp);
                     $data = chunk_split(base64_encode($data));
                     $message .= "Content-Type: application/octet-stream; name="".basename($uploadedFile).""
" . 
                     "Content-Description: ".basename($uploadedFile)."
" .
                     "Content-Disposition: attachment;
" . " filename="".basename($uploadedFile).""; size=".filesize($uploadedFile).";
" . 
                     "Content-Transfer-Encoding: base64

" . $data . "

";
                 }
                 
                 $message .= "--{$mime_boundary}--";
                 $returnpath = "-f" . $email;
                  $secretKey = $secretkey;
 $captcha=$_POST['g-recaptcha-response'];
 $ip = $_SERVER['REMOTE_ADDR'];
  $headers2 .= $company_mail;
 $headers2  .= 'MIME-Version: 1.0' . "
";
 $headers2 .= 'Content-type: text/html; charset=iso-8859-1' . "
";   
     // post request to server
 $url = 'https://www.google.com/recaptcha/api/siteverify?secret=' . urlencode($secretKey) .  '&response=' . urlencode($captcha);
 $response = file_get_contents($url);
 $responseKeys = json_decode($response,true);
     // should return JSON with success as true
     if($responseKeys["success"]) {
                 // Send email
                 $mail = mail($toEmail, $emailSubject, $message, $headers, $returnpath);
             
                 @unlink($uploadedFile);
         
                
                }else{
                    $statusMsg = 'Clear the Captcha';
                }
             }
         }
     }
 }
             // If mail sent
             if($mail){
                 $statusMsg = 'Your contact request has been submitted successfully !';
                 $msgClass = 'succdiv';
                 
                 $postData = '';
             }else{
                 $statusMsg = 'Your contact request submission failed, please try again.';
             }
         }

?>```
question from:https://stackoverflow.com/questions/65949312/unable-to-display-error-or-success-msg-in-php-contact-form-code

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
require 'include/config.php';                                   
$postData = $uploadedFile = $echo = '';
$msgClass = 'errordiv';
if(isset($_POST['submit'])){
 // Get the submitted form data
 $postData = $_POST;
 $email = $_POST['email'];
 $name = $_POST['name'];
 $subject = $_POST['phone2'];
 $message = $_POST['applyfor'];
 
 // Check whether submitted data is not empty
 if(!empty($email) && !empty($name) && !empty($subject) && !empty($message)){
     
     // Validate email
     if(filter_var($email, FILTER_VALIDATE_EMAIL) === false){
         $echo = 'Please enter your valid email.';
     }else{
         $uploadStatus = 1;
         
         // Upload attachment file
         if(!empty($_FILES["attachment"]["name"])){
             
             // File path config
             $targetDir = "resumes/";
             $fileName = basename($_FILES["attachment"]["name"]);
             $targetFilePath = $targetDir . $fileName;
             $fileType = pathinfo($targetFilePath,PATHINFO_EXTENSION);
             
             // Allow certain file formats
             $allowTypes = array('pdf', 'doc', 'docx', 'jpg', 'png', 'jpeg');
             if(in_array($fileType, $allowTypes)){
                 // Upload file to the server
                 if(move_uploaded_file($_FILES["attachment"]["tmp_name"], $targetFilePath)){
                     $uploadedFile = $targetFilePath;
                 }else{
                     $uploadStatus = 0;
                     $statusMsg = 'Sorry, there was an error uploading your file.';
                 }
             }else{
                 $uploadStatus = 0;
                 $statusMsg = 'Sorry, only PDF, DOC, JPG, JPEG, & PNG files are allowed to upload.';
             }
         }
         
         if($uploadStatus == 1){
             
             // Recipient
             $toEmail = $receiverEmail;

             // Sender
             $from = '';
             $fromName = '';
             
             // Subject
             $emailSubject = 'Resume Submitted by '.$name;
             
             // Message 
             $htmlContent ='';

           // Header for sender info
             $headers = "From: $fromName"." <".$from.">";

             if(!empty($uploadedFile) && file_exists($uploadedFile)){
                 
                 // Boundary 
                 $semi_rand = md5(time()); 
                 $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 
                 
                 // Headers for attachment 
                 $headers .= "
MIME-Version: 1.0
" . "Content-Type: multipart/mixed;
" . " boundary="{$mime_boundary}""; 
                 
                 // Multipart boundary 
                 $message = "--{$mime_boundary}
" . "Content-Type: text/html; charset="UTF-8"
" .
                 "Content-Transfer-Encoding: 7bit

" . $htmlContent . "

"; 
                 
                 // Preparing attachment
                 if(is_file($uploadedFile)){
                     $message .= "--{$mime_boundary}
";
                     $fp =    @fopen($uploadedFile,"rb");
                     $data =  @fread($fp,filesize($uploadedFile));
                     @fclose($fp);
                     $data = chunk_split(base64_encode($data));
                     $message .= "Content-Type: application/octet-stream; name="".basename($uploadedFile).""
" . 
                     "Content-Description: ".basename($uploadedFile)."
" .
                     "Content-Disposition: attachment;
" . " filename="".basename($uploadedFile).""; size=".filesize($uploadedFile).";
" . 
                     "Content-Transfer-Encoding: base64

" . $data . "

";
                 }
                 
                 $message .= "--{$mime_boundary}--";
                 $returnpath = "-f" . $email;
                  $secretKey = $secretkey;
 $captcha=$_POST['g-recaptcha-response'];
 $ip = $_SERVER['REMOTE_ADDR'];
  $headers2 .= $company_mail;
 $headers2  .= 'MIME-Version: 1.0' . "
";
 $headers2 .= 'Content-type: text/html; charset=iso-8859-1' . "
";   
     // post request to server
 $url = 'https://www.google.com/recaptcha/api/siteverify?secret=' . urlencode($secretKey) .  '&response=' . urlencode($captcha);
 $response = file_get_contents($url);
 $responseKeys = json_decode($response,true);
     // should return JSON with success as true
     if($responseKeys["success"]) {
                 // Send email
                 $mail = mail($toEmail, $emailSubject, $message, $headers, $returnpath);
             
                 @unlink($uploadedFile);
         
                
                }else{
                    $statusMsg = 'Clear the Captcha';
                }
             }
         }
     }
 }
             // If mail sent
             if($mail){
                 $statusMsg = 'Your contact request has been submitted successfully !';
                 $msgClass = 'succdiv';
                 
                 $postData = '';
             }else{
                 $statusMsg = 'Your contact request submission failed, please try again.';
             }
         }

Write HTML code below PHP code. As $statusMsg variable value set in PHP code.

<p class="statusMsg <?php echo !empty($msgClass)?$msgClass:''; ?>"><?php echo $statusMsg; ?></p>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...