Link to home
Start Free TrialLog in
Avatar of pepps11976
pepps11976

asked on

Contact Form

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
ASKER CERTIFIED SOLUTION
Avatar of Kiran Sonawane
Kiran Sonawane
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of pepps11976
pepps11976

ASKER

Ok when i replace with what you said and click submit i get the following

 User generated image
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">
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
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.
https://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

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
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.