Contact Form Not Working Properly

3 replies · opened Oct 11, 2016

Mmscoates29Oct 11, 2016

Hi, I have used a contact form with some custom inputs, however whenever I submit the form I get the reply

{ "alert": "error", "message": "Please Fill up all the Fields and Try Again." }

Despite all fields being complete. I came across a user on this forum who had multiple custom fields in his form so I used the "sendmail-multi.php" file that you made to help him in the hope that it would work the same for me - could you please tell me where I might be going wrong please - the form is hosted at
www.trogoodwood2017tender.com/creativebrief.html

Thanks in advance - I am really enjoying using Canvas by the way!

Mmscoates29Oct 12, 2016

Hi, my problem still hasn't been resolved - here is the code for the sendmail-multi.php file that the form is running from if you are able to help me - thanks:

<?php

require_once('phpmailer/PHPMailerAutoload.php');

$toemails = array();

$toemails[] = array(
'email' => 'xxxxx', // Your Email Address
'name' => 'xxxxx' // Your Name
);

// Form Processing Messages
$message_success = 'We have successfully received your brief and it will now be processed. Thank you.';

// Add this only if you use reCaptcha with your Contact Forms
$recaptcha_secret = ''; // Your reCaptcha Secret

$mail = new PHPMailer();

// If you intend you use SMTP, add your SMTP Code after this Line

$mail->IsSMTP();
$mail->Host = "smtp.live.com";
$mail->SMTPDebug = 2;
$mail->SMTPAuth = true;
$mail->Port = 25;
$mail->Username = "xxxxx";
$mail->Password = "xxxxx";

if( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
if( $_POST['contactform-requestedby'] != '' ) {

		$jobnumber = isset( $_POST['template-contactform-jobnumber'] ) ? $_POST['template-contactform-jobnumber'] : '';
	$client = isset( $_POST['template-contactform-client'] ) ? $_POST['template-contactform-client'] : '';
	$requestedby = isset( $_POST['template-contactform-requestedby'] ) ? $_POST['template-contactform-requestedby'] : '';
	$projecttitle = isset( $_POST['template-contactform-projecttitle'] ) ? $_POST['template-contactform-projecttitle'] : '';
	$projecttype = isset( $_POST['template-contactform-projecttype'] ) ? $_POST['template-contactform-projecttype'] : '';

	$subject = isset($subject) ? $subject : 'New Message From Contact Form';

	$botcheck = $_POST['template-contactform-botcheck'];

	if( $botcheck == '' ) {

		$mail-&gt;SetFrom( $email , $name );
		$mail-&gt;AddReplyTo( $email , $name );
		foreach( $toemails as $toemail ) {
			$mail-&gt;AddAddress( $toemail['email'] , $toemail['name'] );
		}
		$mail-&gt;Subject = $subject;

		$name = isset($name) ? "Name: $name&lt;br&gt;&lt;br&gt;" : '';
		$email = isset($email) ? "Email: $email&lt;br&gt;&lt;br&gt;" : '';
		$phone = isset($phone) ? "Phone: $phone&lt;br&gt;&lt;br&gt;" : '';
		$service = isset($service) ? "Service: " . ( is_array($service) ? implode( ', ', $service ) : $service ) . "&lt;br&gt;&lt;br&gt;" : '';
		$message = isset($message) ? "Message: $message&lt;br&gt;&lt;br&gt;" : '';

		$referrer = $_SERVER['HTTP_REFERER'] ? '&lt;br&gt;&lt;br&gt;&lt;br&gt;This Form was submitted from: ' . $_SERVER['HTTP_REFERER'] : '';

			$body = " $jobnumber $client $requestedby $projecttitle $projecttype ";

		if( isset( $_FILES['template-contactform-file'] ) AND is_array( $_FILES['template-contactform-file'] ) ) {
			$countfiles = count( $_FILES['template-contactform-file'] );
			for( $i=0; $i &lt; $countfiles; $i++ ) {
				if ( isset( $_FILES['template-contactform-file']['tmp_name'][$i] ) ) {
					$mail-&gt;IsHTML(true);
					$mail-&gt;AddAttachment( $_FILES['template-contactform-file']['tmp_name'][$i], $_FILES['template-contactform-file']['name'][$i] );
				}
			}
		}

		// Runs only when reCaptcha is present in the Contact Form
		if( isset( $_POST['g-recaptcha-response'] ) ) {
			$recaptcha_response = $_POST['g-recaptcha-response'];
			$response = file_get_contents( "https://www.google.com/recaptcha/api/siteverify?secret=" . $recaptcha_secret . "&response=" . $recaptcha_response );

			$g_response = json_decode( $response );

			if ( $g_response-&gt;success !== true ) {
				echo '{ "alert": "error", "message": "Captcha not Validated! Please Try Again." }';
				die;
			}
		}

		$mail-&gt;MsgHTML( $body );
		$sendEmail = $mail-&gt;Send();

		if( $sendEmail == true ):
			echo '{ "alert": "success", "message": "' . $message_success . '" }';
		else:
			echo '{ "alert": "error", "message": "Email **could not** be sent due to some Unexpected Error. Please Try Again later.&lt;br /&gt;&lt;br /&gt;**Reason:**&lt;br /&gt;' . $mail-&gt;ErrorInfo . '" }';
		endif;
	} else {
		echo '{ "alert": "error", "message": "Bot **Detected**.! Clean yourself Botster.!" }';
	}
} else {
	echo '{ "alert": "error", "message": "Please **Fill up** all the Fields and Try Again." }';
}

} else {
echo '{ "alert": "error", "message": "An unexpected error occured. Please Try Again later." }';
}

?>

Mmscoates29Oct 12, 2016

sorry here it is again:

`<?php

require_once('phpmailer/PHPMailerAutoload.php');

$toemails = array();

$toemails[] = array(
'email' => 'mscoates29@hotmail.com ', // Your Email Address
'name' => 'Martyn Scoates' // Your Name
);

// Form Processing Messages
$message_success = 'We have successfully received your brief and it will now be processed. Thank you.';

// Add this only if you use reCaptcha with your Contact Forms
$recaptcha_secret = ''; // Your reCaptcha Secret

$mail = new PHPMailer();

// If you intend you use SMTP, add your SMTP Code after this Line

$mail->IsSMTP();
$mail->Host = "smtp.live.com";
$mail->SMTPDebug = 2;
$mail->SMTPAuth = true;
$mail->Port = 25;
$mail->Username = "mscoates29@hotmail.com";
$mail->Password = "Ms4623559";

if( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
if( $_POST['contactform-requestedby'] != '' ) {

		$jobnumber = isset( $_POST['template-contactform-jobnumber'] ) ? $_POST['template-contactform-jobnumber'] : '';
	$client = isset( $_POST['template-contactform-client'] ) ? $_POST['template-contactform-client'] : '';
	$requestedby = isset( $_POST['template-contactform-requestedby'] ) ? $_POST['template-contactform-requestedby'] : '';
	$projecttitle = isset( $_POST['template-contactform-projecttitle'] ) ? $_POST['template-contactform-projecttitle'] : '';
	$projecttype = isset( $_POST['template-contactform-projecttype'] ) ? $_POST['template-contactform-projecttype'] : '';

	$subject = isset($subject) ? $subject : 'New Message From Contact Form';

	$botcheck = $_POST['template-contactform-botcheck'];

	if( $botcheck == '' ) {

		$mail-&gt;SetFrom( $email , $name );
		$mail-&gt;AddReplyTo( $email , $name );
		foreach( $toemails as $toemail ) {
			$mail-&gt;AddAddress( $toemail['email'] , $toemail['name'] );
		}
		$mail-&gt;Subject = $subject;

		$name = isset($name) ? "Name: $name&lt;br&gt;&lt;br&gt;" : '';
		$email = isset($email) ? "Email: $email&lt;br&gt;&lt;br&gt;" : '';
		$phone = isset($phone) ? "Phone: $phone&lt;br&gt;&lt;br&gt;" : '';
		$service = isset($service) ? "Service: " . ( is_array($service) ? implode( ', ', $service ) : $service ) . "&lt;br&gt;&lt;br&gt;" : '';
		$message = isset($message) ? "Message: $message&lt;br&gt;&lt;br&gt;" : '';

		$referrer = $_SERVER['HTTP_REFERER'] ? '&lt;br&gt;&lt;br&gt;&lt;br&gt;This Form was submitted from: ' . $_SERVER['HTTP_REFERER'] : '';

			$body = " $jobnumber $client $requestedby $projecttitle $projecttype ";

		if( isset( $_FILES['template-contactform-file'] ) AND is_array( $_FILES['template-contactform-file'] ) ) {
			$countfiles = count( $_FILES['template-contactform-file'] );
			for( $i=0; $i &lt; $countfiles; $i++ ) {
				if ( isset( $_FILES['template-contactform-file']['tmp_name'][$i] ) ) {
					$mail-&gt;IsHTML(true);
					$mail-&gt;AddAttachment( $_FILES['template-contactform-file']['tmp_name'][$i], $_FILES['template-contactform-file']['name'][$i] );
				}
			}
		}

		// Runs only when reCaptcha is present in the Contact Form
		if( isset( $_POST['g-recaptcha-response'] ) ) {
			$recaptcha_response = $_POST['g-recaptcha-response'];
			$response = file_get_contents( "https://www.google.com/recaptcha/api/siteverify?secret=" . $recaptcha_secret . "&response=" . $recaptcha_response );

			$g_response = json_decode( $response );

			if ( $g_response-&gt;success !== true ) {
				echo '{ "alert": "error", "message": "Captcha not Validated! Please Try Again." }';
				die;
			}
		}

		$mail-&gt;MsgHTML( $body );
		$sendEmail = $mail-&gt;Send();

		if( $sendEmail == true ):
			echo '{ "alert": "success", "message": "' . $message_success . '" }';
		else:
			echo '{ "alert": "error", "message": "Email **could not** be sent due to some Unexpected Error. Please Try Again later.&lt;br /&gt;&lt;br /&gt;**Reason:**&lt;br /&gt;' . $mail-&gt;ErrorInfo . '" }';
		endif;
	} else {
		echo '{ "alert": "error", "message": "Bot **Detected**.! Clean yourself Botster.!" }';
	}
} else {
	echo '{ "alert": "error", "message": "Please **Fill up** all the Fields and Try Again." }';
}

} else {
echo '{ "alert": "error", "message": "An unexpected error occured. Please Try Again later." }';
}

?>

SSemicolon WebSTAFFOct 12, 2016

Hello,

We Really Apologize about the Inconveniences! The Codes appears to be fine, however when we try to check out your Website for the Issue, it returns a 404 Error. Can you please provide us with a Valid Live URL so that we can check out the exact issue and provide you with a more accurate solution for this. Thanks for your Patience.

Additionally, according to your issue it seems that you are using an Email for your Form so we guess this fix should work. Please follow the Steps below:

  1. Please find the following code in the include/sendemail.php File:
$mail = new PHPMailer();

and replace it with:

$mail = new PHPMailer();
$email = 'default@your-website.com';
  1. Now, find the following code a:
if( $_POST['template-contactform-email'] != '' ) {

and replace it with:

if( $email != '' ) {
  1. Then, find and delete the following code:
$email = isset( $_POST['template-contactform-email'] ) ? $_POST['template-contactform-email'] : '';

This should definitely fix the issue.

Meanwhile, do let us know if we can help you with anything else or if you find any further issues with Canvas.

Have the same question, or something new?

Sign in to the Canvas dashboard to reply or open your own topic. Canvas owners get direct help from the SemiColonWeb team.

Reply on the dashboard