Page 1 of 1

Contact Form

Posted: Mon Oct 08, 2012 8:43 am
by Nickos
Hi to all in this nice forum,

I have a problem with my contact form, and as I am very new in php
I ask help from everybody.
Contact form works nice, but when I click the send button nothing happens.
Some problem must be in the contact.php.
I would be greatful to everybody who helps me...
Thanks a lot
Here is the code:

***** Please use the PHP Code tag *****

Code: Select all

<form id="form-main2" action="contact.php" enctype="multipart/form-data" method="post">
                    <fieldset>
                        <div class="wrapper">
                            <div class="column-1">
                           
		<span style="display:inline;position:relative;top:20px;right:-2px;color:red;">*</span> 
		<input class="text" type="text" name="name" id="name" value="Name:" onBlur="if(this.value=='') this.value='Name:'" onFocus="if(this.value =='Name:' ) this.value=''">
                <span style="display:inline;position:relative;top:20px;right:-2px;color:red;">*</span> 
        
	 	<input class="text" type="text" name="email"  id="email" value="Email:" onBlur="if(this.value=='') this.value='Email:'" onFocus="if(this.value =='Email:' ) this.value=''">
	        <span style="display:inline;position:relative;top:20px;right:-2px;color:red;">*</span> 	
	        <input class="text" type="text" name="telephone"  id="telephone"  value="Telephone:" onBlur="if(this.value=='') this.value='Telephone:'" onFocus="if(this.value =='Telephone:' ) this.value=''">
                </div>
                            
                            
                            <div class="column-2">
                            
		<span>&nbsp;</span>
		<input class="text" style="width:255px" type="text" name="subject" id="subject" value="Subject:" onBlur="if(this.value=='') this.value='Subject:'" onFocus="if(this.value =='Subject:' ) this.value=''">
                <span style="display:inline;position:relative;top:20px;right:-2px;color:red;">*</span>   
		<textarea class="text" name="message" id="message" value="Message:'" onBlur="if(this.value=='') this.value='Message:'" onFocus="if(this.value =='Message:' ) this.value=''">Message:</textarea>
		 
		
                                <div class="captcha">
                                <center>
                       Insert Security Code<br />          
        <img src="Captcha/CaptchaSecurityImages.php?width=100&height=40&characters=5" />
        <p></p>
       
		
		<input id="security_code" name="security_code" type="text" />
        </center>
                                              
                                             
		                        <div class="buttons">
                                                  
                                   <a class="button" href="#" onClick="document.getElementById('form-main2').reset()">Clear</a>
                                   <a class="button" href="#" onClick="document.getElementById('form-main2').submit()">Send</a>
                                    
                                              </div>
                          </div>
                        </div>
                    </fieldset>                   
                </form>  

**************************
And here is contact.php

<?php 
session_start();
$name = @$_POST['name'];
$email = @$_POST['email'];
$message = @$_POST['message'];
if( isset($_POST['submit'])) {
   if (! ereg('[A-Za-z0-9_-]+\@[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+', $email))
   {
      header("Location: _contact_error.html");
      exit;
   }
if (!$name or !$message){
	header("Location: _contact_error.html");
	exit;
}
   if( $_SESSION['security_code'] == $_POST['security_code'] && !empty($_SESSION['security_code'] ) ) {
		// Insert you code for processing the form here, e.g emailing the submission, entering it into a database. 
		$your_email = "my@male.com";

		$headers= "From: ".$_POST['name']." <".$_POST['email'].">\r\n";
		$headers.='Content-type: text/html; charset=utf-8';
		mail($your_email, $_POST['subject'],  "
<html>
<head>
 <title>Contact Message</title>
</head>
<body>
	Contact Message<br><br>
	Name : ".$_POST['name']."<br>
	Email : ".$_POST['email']."<br>
	Message : <br>".$_POST['message']."<br>
</body>
</html>" , $headers);
		header("Location: _contact_message.htm");
		unset($_SESSION['security_code']);
   } else {
		// Insert your code for showing an error message here
		//echo 'Sorry, you have provided an invalid security code';
		header("Location: _contact_wrong.html");
   }
}
?>

Re: Contact Form

Posted: Mon Oct 08, 2012 9:53 am
by Christopher
There are lots of things wrong with this code. It looks old. You should not use error suppresssion, but instead check $_POST values with isset(). Browsers can submit with return and IE will not show submit as passed -- check if the HTTP method is post. And, ereg is depricated -- use preg. And better structured programming rather than all those exit()'s. Perhaps set an error value/message so you can know what went wrong or send the email.

What part of this code is run when you submit?

Re: Contact Form

Posted: Mon Oct 08, 2012 10:28 am
by Nickos
Hi Christopher,
Thanks a lot for your kindly answer,
Unfortunatelly I am not programmer, so
I dont understand a lot about the php code.
I found this contact.php in internet and
I tried to fit it in the contact form it inluded in the
template I bought.
If I use insted of
<a class="button" href="#" onClick="document.getElementById('form-main2').submit()">Send</a>

the following submit button it works, but I dont like the format of the the button Send, it is not the same with the format of the button Clear.
<a href="#"> <input style="background:none;border:none;position:relative;top:10px;left:-20px;font-family:Verdana, Arial, Helvetica, sans-serif;font-size:15px;font-weight:bold" type="submit" value="Send" name="submit"></a>

Anyway, many many thanks for your time...
Friendly
Nickos