Is my PHP contact page corret

8 posts Page 1 of 1
Contributors
User avatar
jtlusco
VIP - Donator
VIP - Donator
Posts: 138
Joined: Mon Jul 12, 2010 11:28 pm

Is my PHP contact page corret
jtlusco
I am working on a site with a contact us section. it should email to an email when you click the send button but it is not. (Email removed for security reasons)

PHP
Code: Select all
<?php
/*
* Contact Form Class
*/


header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-type: application/json');

$admin_email = 'email@email'; // Your Email
$message_min_length = 5; // Min Message Length


class Contact_Form{
	function __construct($details, $email_admin, $message_min_length){
		
		$this->name = stripslashes($details['name']);
		$this->email = trim($details['email']);
		$this->subject = 'Contact from Your Website'; // Subject 
		$this->message = stripslashes($details['message']);
	
		$this->email_admin = $email_admin;
		$this->message_min_length = $message_min_length;
		
		$this->response_status = 1;
		$this->response_html = '';
	}


	private function validateEmail(){
		$regex = '/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i';
	
		if($this->email == '') { 
			return false;
		} else {
			$string = preg_replace($regex, '', $this->email);
		}
	
		return empty($string) ? true : false;
	}


	private function validateFields(){
		// Check name
		if(!$this->name)
		{
			$this->response_html .= '<p>Please enter your name</p>';
			$this->response_status = 0;
		}

		// Check email
		if(!$this->email)
		{
			$this->response_html .= '<p>Please enter an e-mail address</p>';
			$this->response_status = 0;
		}
		
		// Check valid email
		if($this->email && !$this->validateEmail())
		{
			$this->response_html .= '<p>Please enter a valid e-mail address</p>';
			$this->response_status = 0;
		}
		
		// Check message length
		if(!$this->message || strlen($this->message) < $this->message_min_length)
		{
			$this->response_html .= '<p>Please enter your message. It should have at least '.$this->message_min_length.' characters</p>';
			$this->response_status = 0;
		}
	}


	private function sendEmail(){
		$mail = mail($this->email_admin, $this->subject, $this->message,
			 "From: ".$this->name." <".$this->email.">\r\n"
			."Reply-To: ".$this->email."\r\n"
		."X-Mailer: PHP/" . phpversion());
	
		if($mail)
		{
			$this->response_status = 1;
			$this->response_html = '<p>Thank You!</p>';
		}
	}


	function sendRequest(){
		$this->validateFields();
		if($this->response_status)
		{
			$this->sendEmail();
		}

		$response = array();
		$response['status'] = $this->response_status;	
		$response['html'] = $this->response_html;
		
		echo json_encode($response);
	}
}


$contact_form = new Contact_Form($_POST, $admin_email, $message_min_length);
$contact_form->sendRequest();

?>
HTML form
Code: Select all
<form id="contact-form" class="contact-form" action="_include/php/contact.php" method="post">
            	<p class="contact-name">
            		<input id="contact_name" type="text" placeholder="Full Name" value="" name="name" />
                </p>
                <p class="contact-email">
                	<input id="contact_email" type="text" placeholder="Email Address" value="" name="email" />
                </p>
                <p class="contact-message">
                	<textarea id="contact_message" placeholder="Description of your issue" name="message" rows="15" cols="40"></textarea>
                </p>
                <p class="contact-submit">
                	<a id="contact-submit" class="submit" href="#">Send Your Email</a>
                </p>
                
                <div id="response">
                
                </div>
            </form>
Any help would be great I am still new to PHP
Have you been scripted today
Image
User avatar
mandai
Coding God
Coding God
Posts: 2585
Joined: Mon Apr 26, 2010 6:51 pm

Re: Is my PHP contact page corret
mandai
It seems the form does not submit. You can update the HTML form to resolve this:
Code: Select all
                   <a id="contact-submit" class="submit" onclick="document.getElementById('contact-form').submit();" href="#">Send Your Email</a>
User avatar
jtlusco
VIP - Donator
VIP - Donator
Posts: 138
Joined: Mon Jul 12, 2010 11:28 pm

Re: Is my PHP contact page corret
jtlusco
That makes it show the PHP code in the browser so it does something now any idea why it would not send but show the code? Is my PHP not setup correctly? I used the version.php and it shows up like it should so I thought it was setup correctly I have never used or setup PHP before.

Oh and thanks for the help
Have you been scripted today
Image
User avatar
mandai
Coding God
Coding God
Posts: 2585
Joined: Mon Apr 26, 2010 6:51 pm

Re: Is my PHP contact page corret
mandai
It should give the output instead of showing the code, are you able to test a different PHP script in the same folder?
User avatar
jtlusco
VIP - Donator
VIP - Donator
Posts: 138
Joined: Mon Jul 12, 2010 11:28 pm

Re: Is my PHP contact page corret
jtlusco
The hello world php works. Do you have an php page that i should use for testing.?
Have you been scripted today
Image
User avatar
mandai
Coding God
Coding God
Posts: 2585
Joined: Mon Apr 26, 2010 6:51 pm

Re: Is my PHP contact page corret
mandai
You could use this as a simple test:
Code: Select all
<?php
echo "test";
?>
Do you know how PHP was installed on the server initially?
User avatar
jtlusco
VIP - Donator
VIP - Donator
Posts: 138
Joined: Mon Jul 12, 2010 11:28 pm

Re: Is my PHP contact page corret
jtlusco
I used the Web platform installer from microsoft to install php.
the test php page I used was
<?php
echo "Hello World"
?>

and that works
Have you been scripted today
Image
User avatar
jtlusco
VIP - Donator
VIP - Donator
Posts: 138
Joined: Mon Jul 12, 2010 11:28 pm

Re: Is my PHP contact page corret
jtlusco
Here is the contact form section of the main.js file if that will help any.
Code: Select all
contactForm = function(){
	$("#contact-submit").on('click',function() {
		$contact_form = $('#contact-form');
		
		var fields = $contact_form.serialize();
		
		$.ajax({
			type: "POST",
			url: "_include/php/contact.php",
			data: fields,
			dataType: 'json',
			success: function(response) {
				
				if(response.status){
					$('#contact-form input').val('');
					$('#contact-form textarea').val('');
				}
				
				$('#response').empty().html(response.html);
			}
		});
		return false;
	});
}
This is driving me nuts why the php code shows after you click send and it never sends
Have you been scripted today
Image
8 posts Page 1 of 1
Return to “Tutorials”