CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   PHP (http://www.codingforums.com/forumdisplay.php?f=6)
-   -   Contact Form Problem (http://www.codingforums.com/showthread.php?t=285314)

HQWebs 01-05-2013 12:59 PM

Contact Form Problem
 
Hello people ,

First i have to tell you that my english is not perfect , but i hope you will understand me.

I got an problem with A Mail form on my website , I downloaded an template that have e mail form in contact page , but i dont know where to input my e-mail as owner who will recieve e mails that people send me from this form.

And normaly after pressing send there should come an message if it was sucessfully sent or not .

Here are the codes , and tell me how to fix it . How to bring this form working normaly how it should.

______________________________

I got 2 files named " MailHandler.php " and " MailHandler.ashx " . This Files are located in an folder named " bin " .

MailHandler.php code :

PHP Code:

<?php
    $owner_email 
$_POST["owner_email"];
    
$headers 'From:' $_POST["email"];
    
$subject 'A message from your site visitor ' $_POST["name"];
    
$messageBody "";
    
    
$messageBody .= '<p>Visitor: ' $_POST["name"] . '</p>' "\n";
    
$messageBody .= '<br>' "\n";
    
$messageBody .= '<p>Email Address: ' $_POST['email'] . '</p>' "\n";
    
$messageBody .= '<br>' "\n";
    if(
$_POST['state']!='nope'){        
        
$messageBody .= '<p>State: ' $_POST['state'] . '</p>' "\n";
        
$messageBody .= '<br>' "\n";
    }
    if(
$_POST['phone']!='nope'){        
        
$messageBody .= '<p>Phone Number: ' $_POST['phone'] . '</p>' "\n";
        
$messageBody .= '<br>' "\n";
    }    
    if(
$_POST['fax']!='nope'){        
        
$messageBody .= '<p>Fax Number: ' $_POST['fax'] . '</p>' "\n";
        
$messageBody .= '<br>' "\n";
    }
    
$messageBody .= '<p>Message: ' $_POST['message'] . '</p>' "\n";
    
    if(
$_POST["stripHTML"] == 'true'){
        
$messageBody strip_tags($messageBody);
    }

    try{
        if(!
mail($owner_email$subject$messageBody$headers)){
            throw new 
Exception('mail failed');
        }else{
            echo 
'mail sent';
        }
    }catch(
Exception $e){
        echo 
$e->getMessage() ."\n";
    }
?>

MailHandler.ashx code :

Code:

<%@ WebHandler Language="C#" Class="Handler" Debug="true" %>

using System;
using System.Web;
using System.Net.Mail;
using System.Text.RegularExpressions;

public class Handler : IHttpHandler {
        public void ProcessRequest (HttpContext context) {
                SmtpClient mailClient = new SmtpClient(context.Request.Form.Get("smtpMailServer"));
                string owner_email = context.Request.Form.Get("owner_email");
                string subject = "A message from your site visitor " + context.Request.Form.Get("name");
                string email = context.Request.Form.Get("email");
                string messageBody = "";
       
                messageBody += "<p>Visitor: " + context.Request.Form.Get("name") + "</p>\n";
                messageBody += "<br>\n";
                messageBody += "<p>Email Address: " + context.Request.Form.Get("email") + "</p>\n";
                messageBody += "<br>\n";
                messageBody += "<p>Phone Number: " + context.Request.Form.Get("phone") + "</p>\n";
                messageBody += "<br>\n";
                messageBody += "<p>Message: " + context.Request.Form.Get("message") + "</p>\n";
       
                       
                MailMessage message = new MailMessage();
       
                try{
                        message.From = new MailAddress(email.ToString());
                }catch (FormatException e) {
                        context.Response.Write(e.Message);
                }
       
                message.To.Add(owner_email);
                message.Subject = subject;
                if(context.Request.Form.Get("stripHTML") == "true"){
                        message.IsBodyHtml = false;
            messageBody = Regex.Replace(messageBody, "<.*?>", string.Empty);
                }else{
                          message.IsBodyHtml = true;
                }
                message.Body = messageBody;
               
                try{
                        mailClient.Send(message);
                }catch (SmtpException e) {
                        context.Response.Write("mail failed");
                }
                context.Response.Write("mail sent");
        }

        public bool IsReusable {
                get        {
                        return false;
                }
        }
}

And in my contact.html is this form code :

Code:

<form id="contact-form">
                                                                <fieldset>
                                                                        <label>
                                                                                <input type="text" value="Name" onBlur="if(this.value=='') this.value='Name'" onFocus="if(this.value =='Name' ) this.value=''" />
                                                                        </label>
                                                                        <label>
                                                                          <input type="text" value="Email" onBlur="if(this.value=='') this.value='Email'" onFocus="if(this.value =='Email' ) this.value=''" />
                                                                        </label>
                                                                        <label>
                                                                          <input type="text" value="Phone" onBlur="if(this.value=='') this.value='Phone'" onFocus="if(this.value =='Phone' ) this.value=''" />
                                                                        </label>
                                                                        <textarea onBlur="if(this.value=='') this.value='Message'" onFocus="if(this.value =='Message' ) this.value=''">Message</textarea>
                                                                        <div class="buttons-wrapper">
                                                                                <a class="button" onClick="document.getElementById('contact-form').reset()">Clear</a>
                                                                                <a class="button" onClick="document.getElementById('contact-form').submit()">Send</a>
                                                                        </div>
                                                                </fieldset>
                                                        </form>

Thats all , can anyone tell me please how to make it working correctly and where to put my e mail which should reciee all messages sent from this form ?

Thank you very much people!

minkoko 01-05-2013 04:49 PM

You just need to insert the value name the contact form like this name="yorname"

simple mail function with php

PHP Code:

<?php
$to 
"cyberoot1@gmail.com";
$subject $_POST['subject'];
$message $_POST['message'];
$from =$_POST['youremail'];
$headers "From:" $from;
mail($to,$subject,$message,$headers);
echo 
"Mail Sent.";
?>

Quote:

<form id="contact-form">
<fieldset>
<label>
<input type="text" name = "typeyourname" value="Name" onBlur="if(this.value=='') this.value='Name'" onFocus="if(this.value =='Name' ) this.value=''" />
</label>
<label>
<input type="text" name = "typeyourname"value="Email" onBlur="if(this.value=='') this.value='Email'" onFocus="if(this.value =='Email' ) this.value=''" />
</label>
<label>
<input type="text" name = "typeyourname"value="Phone" onBlur="if(this.value=='') this.value='Phone'" onFocus="if(this.value =='Phone' ) this.value=''" />
</label>
<textarea name = "typeyourname"onBlur="if(this.value=='') this.value='Message'" onFocus="if(this.value =='Message' ) this.value=''">Message</textarea>
<div class="buttons-wrapper">
<a class="button" onClick="document.getElementById('contact-form').reset()">Clear</a>
<a class="button" onClick="document.getElementById('contact-form').submit()">Send</a>
</div>
</fieldset>
</form>

HQWebs 01-05-2013 05:00 PM

Aham , Can you tell me where should I add this php code directly ?
Can you give me the right codes ?
And I dont understand what means this red words
Quote:

name = "typeyourname"
So This e-mail should recieve messages from this form > " hqwebs.eu@gmail.com "

Thank you very much

minkoko 01-05-2013 05:24 PM

simple mail function with you contact form

PHP Code:

<form id=" action="" method="post" contact-form">
<
fieldset>
<
label>
<
input type="text" name "name" value="Name" onBlur="if(this.value=='') this.value='Name'" onFocus="if(this.value =='Name' ) this.value=''" />
</
label>
<
label>
<
input type="text" name "email"value="Email" onBlur="if(this.value=='') this.value='Email'" onFocus="if(this.value =='Email' ) this.value=''" />
</
label>
<
label>
<
input type="text" name "phone"value="Phone" onBlur="if(this.value=='') this.value='Phone'" onFocus="if(this.value =='Phone' ) this.value=''" />
</
label>
<
textarea name "message"onBlur="if(this.value=='') this.value='Message'" onFocus="if(this.value =='Message' ) this.value=''">Message</textarea>
<
div class="buttons-wrapper">
<
class="button" onClick="document.getElementById('contact-form').reset()">Clear</a>
<
class="button" onClick="document.getElementById('contact-form').submit()">Send</a>
</
div>
</
fieldset>
</
form


Code:

<?php
$to = "hqwebs.eu@gmail.com";
$name=$_POST['name'];
$phone = $_POST['phone'];
$email = $_POST ['email'];
$message = $_POST['message'];
$headers = "From:" . $email;
mail($to,$name,$message,$headers);

if(mail){
echo "Mail Sent.";
}else{
    echo "Cannot Send your mail for error";
}
?>


HQWebs 01-05-2013 08:00 PM

Okay , I fixed that now , its working fine .
But now i have another problem ...

This Phone field is not working

I become message with e mail , name and text message but there is not phone number ..

How to fix that ?

Thank you very much.

AndrewGSW 01-05-2013 09:46 PM

email injection

The following is not very effective in this regard:

PHP Code:

if($_POST["stripHTML"] == 'true'){ 
        
$messageBody strip_tags($messageBody); 
    } 


HQWebs 01-05-2013 09:57 PM

Quote:

Originally Posted by AndrewGSW (Post 1304311)
email injection

The following is not very effective in this regard:

PHP Code:

if($_POST["stripHTML"] == 'true'){ 
        
$messageBody strip_tags($messageBody); 
    } 


I am not using this code anymore , I am using this code now :

PHP Code:

<?php header("Refresh: 5;url=http://www.hqwebs.eu/contacts.html"); 
$to "hqwebs.eu@gmail.com";
$name=$_POST['name'];
$phone $_POST['phone'];
$email $_POST ['email'];
$message $_POST['message'];
$headers "From:" $email;
mail($to,$name,$message,$headers);


if(
mail){
echo 
"<html>";
echo 
"<head>";
echo 
"<title>HQWebs - Mail Sent</title>";
echo 
"</head>";
echo 
"<body bgcolor='#000000'>";
    echo 
"<center><img src='http://www.hqwebs.eu/images/mailsent.jpg'></center>";
echo 
"</body>";
echo 
"</html>";
}else{
    echo 
"Cannot Send your mail for error";
}
?>

But This Phone field is not working , when you send an message i got , e mail , name , text message from sender , but there is not the phone number , what should i do to fix that too ?

Thank you

minkoko 01-06-2013 01:03 AM

PHP Code:

<?php
$to 
"hqwebs.eu@gmail.com";
$name=$_POST['name'];
$phone $_POST['phone'];
$email $_POST ['email'];
$message $_POST['message'];
$headers "From:" $email "Phone Numbers is :" $phone ;
mail($to,$name,$message,$headers);

if(
mail){
echo 
"Mail Sent.";
}else{
    echo 
"Cannot Send your mail for error";
}
?>


HQWebs 01-06-2013 03:16 AM

minkoko , this is an good solution , but there is an little problem.

Look at this Image :

http://s1239.beta.photobucket.com/us...5a979.jpg.html

Its telling me " Numbers is :0038766336703 <fightingforlove@greenmail.netphone> "
And it should be " Phone Number is : 0038766336703 <fightingforlove@greenmail.net> "

Can you fix this code please ? And thank you very much for spending time on my problem!

minkoko 01-06-2013 08:31 AM

PHP Code:

<?php
if(isset($_POST['send'])){

$to "hqwebs.eu@gmail.com";
$name=$_POST['name'];
$phone $_POST['phone'];
$email $_POST ['email'];
$message $_POST['message']. "\n\n Phone Number is " .$phone;
$headers "From:" $email  ;
mail($to,$name,$message,$headers);

if(
mail){
echo 
"Mail Sent.";
}else{
    echo 
"Cannot Send your mail for error";
}
}
?> 
<form id=" action="email" method="post" contact-form">
<fieldset>
<label>
<input type="text" name = "name" value="Name" onBlur="if(this.value=='') this.value='Name'" onFocus="if(this.value =='Name' ) this.value=''" />
</label>
<label>
<input type="text" name = "email"vvalue="Email" onBlur="if(this.value=='') this.value='Email'" onFocus="if(this.value =='Email' ) this.value=''" />
</label>
<label>
<input type="text" name = "phone" value="Phone" onBlur="if(this.value=='') this.value='Phone'" onFocus="if(this.value =='Phone' ) this.value=''" />
</label>
<textarea name = "message" onBlur="if(this.value=='') this.value='Message'" onFocus="if(this.value =='Message' ) this.value=''">Message</textarea>
<div class="buttons-wrapper">
<a class="button" onClick="document.getElementById('contact-form').reset()">Clear</a>
<input type="submit" name="send" onClick="document.getElementById('contact-form').submit()" value="Send"></a>
</div>
</fieldset>
</form>


HQWebs 01-11-2013 04:33 AM

Okay , now i got what i wanted.
Thank you very much !

HQWebs 01-28-2013 06:55 AM

Hmm , can anyone tell me how to add option "Required" to some fields ?
So when this required field in not filled then the message wont be sent , and he will get this message which is placed in this php " Cannot Send your mail for error " .



And how can I add drop down list option ?

Thank you very much !

ckirkster 01-28-2013 03:37 PM

Try the following code. I've cleaned up yours a bit to make it easier to read.
It's untested but simple enough that it should work.
I've commented it to help you

PHP Code:

<!DOCTYPE html>
<html>
<?php
$fields 
= array('message','email');  // required fields go in here
$errors=false;
if(isset(
$_POST['send']))
    {
        
array_walk($_POST'cleanMe'); // Clean up the input
        
$to "hqwebs.eu@gmail.com";
        
$name=$_POST['name'];
        
$phone $_POST['phone'];
        
$email $_POST ['email'];
        
$message $_POST['message']. "\n\n Phone Number is " .$phone;
        
$headers "From:" $email  ;
      
       
/**
        * Checks fields to see if they are empty
        */
        
foreach($fields as $field
        {
            if( 
strlen($_POST[$field]) == 
            {
                
$errors true;
                break;
            }
        }  
        
        if(!
$errors// If none of the required fields produced an error then try to email
        
{
            
mail($to,$name,$message,$headers);

            if(
mail)
            {
                echo 
"Mail Sent.";
            }
            else
            {
                echo 
"Cannot Send your mail for error";
            }
        }
        else 
// One or more of the required fields wasn't filled in
        
{
            echo 
'Fields marked with a * are required';
        }            
        
        
    }
/**
 * 
 * Clean up input from the user
 * @param type $data
 * @return string
 */
function cleanMe($data)
    {
        
$data trim($data);
        
$data stripslashes($data);
        
$data htmlspecialchars($dataENT_QUOTES);
        return 
$data;
    }        
?> 
    <form id="" method="post" contact-form">
        <fieldset>
            <label>Name</label>
            <input type="text" name = "name" value="Name" onBlur="if(this.value=='') this.value='Name'" onFocus="if(this.value =='Name' ) this.value=''" /><br />
            
            <label>Email</label>
            <input type="text" name = "email"vvalue="Email" onBlur="if(this.value=='') this.value='Email'" onFocus="if(this.value =='Email' ) this.value=''" />*<br />
            
            <label>Phone</label>
            <input type="text" name = "phone" value="Phone" onBlur="if(this.value=='') this.value='Phone'" onFocus="if(this.value =='Phone' ) this.value=''" /><br />
            
            <textarea name = "message" onBlur="if(this.value=='') this.value='Message'" onFocus="if(this.value =='Message' ) this.value=''">Message</textarea>*<br />
            <div class="buttons-wrapper">
                <a class="button" onClick="document.getElementById('contact-form').reset()">Clear</a>
                <input type="submit" name="send" onClick="document.getElementById('contact-form').submit()" value="Send"></a>
            </div>
        </fieldset>
    </form>
</html>

Hope that helps

HQWebs 01-28-2013 08:24 PM

Hey CkirSter , thank you for spending your time on my problem , but this codes are not working correctly .

After filling all fields and press send button , nothing happens , and i dont become the e mail , and there is not coming the message if it was sent sucessfully or not . Its not working :S
Can you take a look on this codes and find where is problem ? Thank you very much.

ckirkster 01-28-2013 09:05 PM

I might be confusing you a little.
I was using minkoko's last code as an example. I also never tested it but can see at second glance the

PHP Code:

mail($to,$name,$message,$headers);
  if(
mail)
 {
     echo 
"Mail Sent.";
 }
 else
 {
     echo 
"Cannot Send your mail for error";
 } 

should read
PHP Code:

  if(mail($to,$name,$message,$headers))
 {
     echo 
"Mail Sent.";
 }
 else
 {
     echo 
"Cannot Send your mail for error";
 } 


The code I added to it is there to give you a simple example of setting errors and stopping the script calling the "mail" function if there are errors.

That should help point you in the right direction


All times are GMT +1. The time now is 05:18 PM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.