I know that there are many possible duplicates but i keep receiving this error each time
I'm currently working on a WordPress contact form, which is created as plugin. I need after submitting the form to redirects to the same page with a new clean form. The problem is that if i put header("location...) it shows me the message
Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\WP\wp-content\themes\group-test\header.php:13) in C:\xampp\htdocs\WP\wp-content\plugins\form-example\form-example.php on line 84
The form is working correctly and it sends email after i press submit button, but the problem is that it keeps the old input values and it resends them again after every reload or submit pressing even the form is empty... The best decision for me would be not to reload the page but only to shows some message after i press "Submit" and to empty the input values from the form as others WP forms functionality(Contact 7 etc.) but i know that Ajax is needed... But firstly i will be glad if someone shows me where i am wrong.
<?php
/*
Plugin Name: Example Contact Form
Plugin URI: http://jd.com
Description: Contact Form
Version: 1.0
Author: JD
Author URI: http://jd.com
*/
function html_form_code() {
?>
<form action="<?php esc_url( $_SERVER['REQUEST_URI'] )?>" method="post" class="contact-form" id="newsletterform">
<div class=header-contact>
<p><h2>Contact Form</h2></p>
<hr>
</div>
<div class=input-containers>
<input type="text" id="name" name="cf-name" pattern="[a-zA-Z0-9 ]+" value="" size="40" placeholder="Име и фамилия"/>
</div>
<div class=input-containers>
<input type="email" name="cf-email" value="" size="40" placeholder="Поща"/>
</div>
<div class=input-containers>
<input type="text" name="cf-subject" pattern="[a-zA-Z ]+" value="" size="40" placeholder="Относно"/>
</div>
<div class=input-containers>
<textarea rows="10" cols="35" name="cf-message" placeholder="Текст"></textarea>
</div>
<div class=input-containers>
<input type="submit" name="cf-submitted" value="Send" >
</div>
</form>
<?php
}
//function to generate response
function deliver_mail() {
require_once "wp-includes/class-phpmailer.php";
$verificationMessages = array();
// if the submit button is clicked, send the email
if ( isset( $_POST['cf-submitted'] ) ) {
// sanitize form values
$name = sanitize_text_field( $_POST["cf-name"] );
$email = sanitize_email( $_POST["cf-email"] );
$subject = sanitize_text_field( $_POST["cf-subject"] );
$message = esc_textarea( $_POST["cf-message"] );
$headers = "From: $name <$email>" . "\r\n";
// Localhost
$mail = new PHPMailer(true);
$mail->IsSMTP(); // telling the class to use SMTP
$mail->CharSet = 'UTF-8';
$mail->SMTPDebug = 0; // enables SMTP debug information (for testing)
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "ssl"; // sets the prefix to the servier
$mail->Host = "mail.gmx.com"; // sets GMX as the SMTP server for example: mail.gmx.com
$mail->Port = 465; // set the SMTP port for the GMX server
$mail->Username = $email;
$mail->Password = 'pass';
$mail->SetFrom($email, $name);
$mail->AddAddress($email);
$mail->Subject = $subject;
$mail->MsgHTML($message);
$headers .= "Content-Type: text/html; charset=utf-8";
$headers .= "Content-Transfer-Encoding: 8bit";
try {
$mail->send();
$msg = "An email has been sent for verfication.";
$msgType = "success";
array_push($verificationMessages, "Check your email to activate your account");
header("location: http://localhost/wp/");
} catch (Exception $ex) {
$msg = $ex->getMessage();
$msgType = "warning";
array_push($verificationMessages, "Wrong");
header("location: http://localhost/wp/");
}
}
}
function cf_shortcode() {
ob_start();
deliver_mail();
html_form_code();
ob_end_flush();
//return ob_get_clean();
}
add_shortcode( 'sitepoint_contact_form', 'cf_shortcode' );
header
before any output, the moment PHP sees a single character even a space, it outputs a set of headers telling the browser to expect a webpage. Once that's done, the header part of the page is done, you can't change it, hence the warning