?
Solved

Contact Form

Posted on 2011-10-18
7
Medium Priority
?
5,474 Views
Last Modified: 2012-05-12
Hi all i have created a form on my website as below

<form action="bin/MailHandler.php" method="post" name="contact-form" class="main-contacts" id="contact-form">
<fieldset>
															<input type="hidden" name="owner_email" id="owner_email" value="john.pepper@testsite.com" />
															<input type="hidden" name="serverProcessorType" id="serverProcessorType" value="php" />
															<input type="hidden" name="smtpMailServer" id="smtpMailServer" value="localhost" />
															<input type="hidden" name="stripHTML" id="stripHTML" value="true" />
						
										
                  	<div class="rowElem">
						<b>Enter your Name:</b>
						<div class="bg">
							<input type="text"  name="name" id="name">
						</div>
						<label class="error" for="name" id="name_error">*This field is required.</label>
						<label class="error" for="name" id="name_error2">*This is not a valid name.</label>
					</div>
                   	<div class="rowElem">
						<b>Enter your E-mail:</b>
						<div class="bg">
                    		<input type="text"  name="email" id="email" value="">
                   		</div>
						<label class="error" for="email" id="email_error">*This field is required.</label>
						<label class="error" for="email" id="email_error2">*This is not a valid email address.</label>
					</div>
                   <div class="rowElem">
				   		<b>Enter your Phone:</b>
						<div class="bg">
                      		<input type="text" name="phone" id="phone">
                   		</div>
						<label class="error" for="phone" id="phone_error">*This field is required.</label>
						<label class="error" for="phone" id="phone_error2">*This is not a valid phone number.</label>
					
					</div>
                    <div class="textarea-box">
						<b>Enter your Message:</b>
						<textarea rows="1" cols="1" name="message" id="message"></textarea>
						<label class="error" for="message" id="message_error">*This field is required.</label>
						<label class="error" for="message" id="message_error2">*The message is too short.</label>
					</div>
                    <div class="clear"></div>
                    <a href="#" class="link" id="clear">Reset</a> 
					<a href="bin/MailHandler.php" class="link" id="submit">Submit</a>
				</fieldset>
                </form>

Open in new window


and this is the code for the mail handler

 
<?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";
	$messageBody .= '<p>Phone Number: ' . $_POST['phone'] . '</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";
	}
?>

Open in new window


when i click on the submit button nothing happens, i am new to PHP so can any body guide me as to what i am doing wrong, Also do i need to set anything up on the server Side?

John
0
Comment
Question by:pepps11976
7 Comments
 
LVL 17

Accepted Solution

by:
sonawanekiran earned 2000 total points
ID: 36984917
You should use

<input type="submit" name="sub" value="Submit" class="link">
Instead of
<a href="bin/MailHandler.php" class="link" id="submit">Submit</a>
0
 

Author Comment

by:pepps11976
ID: 36984980
Ok when i replace with what you said and click submit i get the following

 Form Error
0
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 36984991
What is the url of this page ?

check whether the file mentioned in action action="bin/MailHandler.php" is present on correct path ??

try chnaging with extra / before bin

action="/bin/MailHandler.php"              in form tage

<form action="/bin/MailHandler.php" method="post" name="contact-form" class="main-contacts" id="contact-form">
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:pepps11976
ID: 36985029
Ok I can confirm it exists i have tried putting a forward slash infront of it i have also tried moving the mailhandler.phph into the root folder but i still get the same error message.

The website is hosted on a win 2003 web server is there anything that needs to be done that end?

john
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 36992231
New to PHP?  You will love this little book.  Very readable with great examples.
http://www.sitepoint.com/books/phpmysql4/

The general design of a form-to-email script is shown in the code snippet.  You might try installing that, and once you get it to run, you can add the other features you want, one-at-a-time, testing each feature as you add it.  This strategy is called Test-Driven Development, and it's a sure way to get good results fast.  I have an article here at EE that describes the thought processes and steps in a TDD activity.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_7830-A-Quick-Tour-of-Test-Driven-Development.html

Sidebar note: It looks like you have two different <label> tags with the same id attribute.  You might want to check that.

Best regards, ~Ray
<?php // RAY_form_to_email.php
error_reporting(E_ALL);


// SEND MAIL FROM A FORM


// REQUIRED VALUES ARE PREPOPULATED - CHANGE THESE FOR YOUR WORK
$from  = "NoReply@Your.org";
$subj  = "Contact Form";

// THIS IS AN ARRAY OF RECIPIENTS - CHANGE THESE FOR YOUR WORK
$to[]  = "You@Your.org";
$to[]  = "Her@Your.org";
$to[]  = "Him@Your.org";



// IF THE DATA HAS BEEN POSTED
if (!empty($_POST['email']))
{
    // DISABLED ON THE SERVER SIDE
    var_dump($_POST);
    die(' DISABLED');

    // CLEAN UP THE POTENTIALLY BAD AND DANGEROUS DATA
    $email      = clean_string($_POST["email"]);
    $name       = clean_string($_POST["name"]);
    $telephone  = clean_string($_POST["telephone"]);

    // CONSTRUCT THE MESSAGE THROUGH STRING CONCATENATION
    $content    = NULL;
    $content   .= "You have a New Query From $name" . PHP_EOL . PHP_EOL;
    $content   .= "Tel No: $telephone" . PHP_EOL;
    $content   .= "Email: $email" . PHP_EOL;

    // SEND MAIL TO EACH RECIPIENT
    foreach ($to as $recipient)
    {
        if (!mail( $recipient, $subj, $content, "From: $from\r\n"))
        {
            echo "MAIL FAILED FOR $recipient";
        }
        else
        {
            echo "MAIL WORKED FOR $recipient";
        }
    }
}


// A FORM TO TAKE CLIENT INPUT FOR THIS SCRIPT
$form = <<<ENDFORM
<form method="post">
Please enter your contact information
<br/>Email: <input name="email" />
<br/>Phone: <input name="telephone" />
<br/>Name:  <input name="name" />
<br/><input type="submit" />
</form>
ENDFORM;

echo $form;



// A FUNCTION TO CLEAN UP THE DATA - AVOID BECOMING AN OPEN-RELAY FOR SPAM
function clean_string($str)
{
    // IF MAGIC QUOTES IS ON, WE NEED TO REMOVE SLASHES
    $str = stripslashes($str);

    // REMOVE EXCESS WHITESPACE
    $rgx
    = '/'               // REGEX DELIMITER
    . '\s'              // MATCH THE WHITESPACE CHARACTER(S)
    . '\s+'             // MORE THAN ONE CONTIGUOUS INSTANCE OF WHITESPACE
    . '/'               // REGEX DELIMITER
    ;
    $str = preg_replace($rgx, ' ', $str);

    // REMOVE UNWANTED CHARACTERS
    $rgx
    = '/'               // REGEX DELIMITER
    . '['               // START OF A CHARACTER CLASS
    . '^'               // NEGATION - MATCH NONE OF THE CHARACTERS IN THIS CLASS
    . 'A-Z0-9&+:?_.- '  // CHARACTERS WE WANT TO KEEP
    . ']'               // END OF THE CHARACTER CLASS
    . '/'               // REGEX DELIMITER
    . 'i'               // CASE-INSENSITIVE
    ;
    $str = preg_replace($rgx, NULL, $str);

    return $str;
}

Open in new window

0
 

Author Comment

by:pepps11976
ID: 36993818
Thanks for the info

i installed php using the windows installer i also installed fast cgi,

the problem that i am currently experiancing is whenever i browse to a php page my browser asks me to save it rather than view it

any idears???

john
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 36994436
Sounds like your machine does not know that it needs to parse scripts named like *.php or *.phph through the PHP engine.  But that is really a separate question.  You might want to post it in the PHP for Windows zone.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month13 days, 19 hours left to enroll

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question