Jump to content

[SOLVED] Contact form help


DeathStarDarth

Recommended Posts

Hello all! This is probably not the place to be asking for help since there isn't any PHP in this post, but here goes.

 

I have a contact form. Styled and ready to go. The problem is that I don't know a lick of PHP. If someone could help by either pointing me in the right direction or working with me on the code, I would be extremely grateful.

 

Cheers All. And thanks in advance.

 

<form id="contact-form" action="#contact-form" method="post">
        
        
        <fieldset>
            <h2>Your info</h2>
            <div class="field">
                <div class="col col-left">
                    <label for="name">Your name</label><br>
                    <input name="name" id="name" class="input-text" type="text">
                </div>
                <div class="col col-right">
                    <label for="company">Company or organization</label><br>
                    <input name="company" id="company" class="input-text" type="text">
                </div>
            </div>
            <div class="field last-field">
                <div class="col col-left">
                    <label for="email">E-mail</label><br>
                    <input name="email" id="email" class="input-text" type="text">
                </div>
                <div class="col col-right">
                    <label for="phone">Phone or Skype</label><br>
                    <input name="phone" id="phone" class="input-text" type="text">
                </div>
            </div>
      	</fieldset>

        <fieldset>
            
            <h2>Additional Info / Message</h2>            
                        
            <div class="field">
                <label for="project-desc">Please add a short message:</label><br>
                <textarea name="project-desc" id="project-desc" cols="50" rows="5"></textarea>
            </div>
            
            <div class="field last-field">
                    <p class="smallnote">Individuals, please include any injuries or preexisting physical conditions.
                     <br><br>
                    Companies, please include number of employees and days requested.</p>
                </div>
            

            
        </fieldset>    
        
        <fieldset id="button-fieldset">
            <label for="cc" class="label-check">
                <input class="input-check" name="cc" id="cc" value="1" checked="checked" type="checkbox"> Receive a copy of this message?
            </label>
            <input class="input-hidden" name="submited" value="1" type="hidden">        
            <input class="input-image" src="img/send-message.gif" alt=" [ send message ] " onmouseover="this.src='img/send-message-hover.gif';" onmouseout="this.src='img/send-message.gif';" onmousedown="this.src='img/send-message-active.gif';" type="image">
        </fieldset>
        
    </form>

Link to comment
Share on other sites

ACTION in the form must be a valid URL (path to the file where you will process the data sent from the form). Now when you use POST as a method which is good. You will be able to process further the user inputted values via $_POST super global array in php. Say you have test.php file where you want to process the form input. You could add there <?php print_r($_POST); to see all the contents that was sent from your form. Or say you wanted name & company.

<?php
$name = $_POST['name'];
$company = $_POST['company'];
// Do somethng with your values.. e.g save in to database.

 

If saving to db, don't forget to validate and sanitize the user inputted values before inserting them into db.

Link to comment
Share on other sites

i've spent about a month learning illustrator, photoshop as well as how to code valid html and css. just when i thought i was at the end of it, here comes php and mysql. i'm about ready to freak out. the site is for my small business. i don't expect more than 10 replies a week. to start, i'll probably just keep track of everything in a db of my own.

 

to get this emailed to myself, when someone fills out the form, i've got this:

 

<?php

$where_form_is="http://".$_SERVER['SERVER_NAME'].strrev(strstr(strrev($_SERVER['PHP_SELF']),"/"));

mail("me@myemail.com","Form data:

name: " . $_POST['name'] . " 
company: " . $_POST['company'] . " 
email: " . $_POST['email'] . " 
phone: " . $_POST['phone'] . " 
message: " . $_POST['message'] . " 



");



?>

 

does that look like a good start to have the message emailed to me?

 

thanks so much for the help. i'm tearing my hair out over something that's probably so simple.

 

 

Link to comment
Share on other sites

Hi DeathStarDarth,

 

Give this basic code a go and build on it as required:

 

<?php
//If the form is POSTed, do the following
if($_SERVER['REQUEST_METHOD'] == 'POST') {

	//Get the POSTed data and store in the $ variables
$name     		= $_POST['name'];
$company      	= $_POST['company'];
$email 			= $_POST['email'];
$phone 			= $_POST['phone'];
$projectdesc	= $_POST['project-desc'];

//If the name field is blank, retun the below error
if(!$name) 
{
echo "You must enter a name.";
}

//If the email address field is blank, retun the below error
if(!$email) 
{
echo "You must enter an email address";
}

//If the email address entered appears to be of an incorrect format return the below error
elseif (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email))
{ 
echo "You must enter a valid email address";
}

//If the phone number field is blank, retun the below error
if(!$phone) 
{
echo "You must enter a phone number";
}

if(!$errors)
{
$msg = "Full Name: $name\n\nCompany: $company\n\nEmail Address: $email\n\nPhone: $phone\n\nProject Description: $projectdesc\n\n"; 

mail("youremail@yourdomain.com", "Website Enquiry", $msg, "From: $name  <$email>"); 
}


}
?>
<form id="contact-form" method="post">
        
        
        <fieldset>
            <h2>Your info</h2>
            <div class="field">
                <div class="col col-left">
                    <label for="name">Your name</label><br>
                    <input name="name" id="name" class="input-text" type="text">
                </div>
                <div class="col col-right">
                    <label for="company">Company or organization</label><br>
                    <input name="company" id="company" class="input-text" type="text">
                </div>
            </div>
            <div class="field last-field">
                <div class="col col-left">
                    <label for="email">E-mail</label><br>
                    <input name="email" id="email" class="input-text" type="text">
                </div>
                <div class="col col-right">
                    <label for="phone">Phone or Skype</label><br>
                    <input name="phone" id="phone" class="input-text" type="text">
                </div>
            </div>
      	</fieldset>

        <fieldset>
            
            <h2>Additional Info / Message</h2>            
                        
            <div class="field">
                <label for="project-desc">Please add a short message:</label><br>
                <textarea name="project-desc" id="project-desc" cols="50" rows="5"></textarea>
            </div>
            
            <div class="field last-field">
                    <p class="smallnote">Individuals, please include any injuries or preexisting physical conditions.
                     <br><br>
                    Companies, please include number of employees and days requested.</p>
                </div>
            

            
        </fieldset>    
        
        <fieldset id="button-fieldset">
            <label for="cc" class="label-check">
                <input class="input-check" name="cc" id="cc" value="1" checked="checked" type="checkbox"> Receive a copy of this message?
            </label>
            <input class="input-hidden" name="submited" value="1" type="hidden">        
            <input class="input-image" src="img/send-message.gif" alt=" [ send message ] " onmouseover="this.src='img/send-message-hover.gif';" onmouseout="this.src='img/send-message.gif';" onmousedown="this.src='img/send-message-active.gif';" type="image">
        </fieldset>
        
    </form>

Link to comment
Share on other sites

i link to the page link this:

 

<form id="contact-form"  method="post" action="/scripts/mailer.php">

 

when i do that, and hit the submit button, it sends me to:

 

http://mydomain.com/scripts/mailer.php

 

which just shows up as a blank page.

 

If it's mailer.php and the same page just do action="mailer.php".

I'm not sure why it's a blank page.

 

The action="page" should have all the php code to process the form. If it's a blank page maybe you don't have anything to echo out that it's been sent or not been sent?

Link to comment
Share on other sites

do you mean, a "thank you. your message has been sent." page?

 

how would i modify the code to send a person to that page?

 

thanks, all, for of your patience and help. i'm almost there.

 

Try adding echo "Thanks. Your email has been sent!"; like below and test it out. Once it's been sent then it'll say thanks instead of show a blank page. You then should receive your email.

 

if(!$errors)
{
$msg = "Full Name: $name\n\nCompany: $company\n\nEmail Address: $email\n\nPhone: $phone\n\nProject Description: $projectdesc\n\n"; 

mail("youremail@yourdomain.com", "Website Enquiry", $msg, "From: $name  <$email>"); 

echo "Thanks. Your email has been sent!";
}

Link to comment
Share on other sites

thank you, pneudralics.

 

that, at least, sends them to mailer.php. but displays the message.

 

the reason, i originally had is set up as:

 

<form id="contact-form" action="#contact-form" method="post">

 

was that i want to display the information on a div on the same page. right under the beginning of the contact form.

 

i thought i was doing it right to do that. but then, how would i link to the php script?

 

here's what i want to have happen when someone hits the submit button:

 

<form id="contact-form" action="#contact-form" method="post">
        
        <div class="error-message">
<p>The message has <em>not been sent</em>. Could you please check the following:</p>
<ul>
<li>Your name is missing</li>
<li>Your company/organization name is missing</li>
<li>Your email is missing</li>

<li>Please provide phone number or Skype username</li>
<li>Please select services required for the project</li>
<li>Please provide short message</li>
</ul>
</div>

 

or

<div class="section">
    
<div class="thankyou-message"><p>NAME HERE, thanks for your message!<br>I will get back to you soon.</p></div>    
</div>	    
    </div>

 

does that make sense? i want the message, on the same page. that's why i made the action #contact-form. to go with the supplied thank you or error messages.

 

i could take this in a different direction and send them to another html page. I'm simply trying to keep the entire site looking the same across the board as i've spend a lot of time on it and don't want to just have the sender sent to a generic page that looks nothing like the rest of the site. w/o even a back button.

 

to do that, would i just:

 

   if(!$errors)
   {
   $msg = "Full Name: $name\n\nCompany: $company\n\nEmail Address: $email\n\nPhone: $phone\n\nProject Description: $projectdesc\n\n";
   
   mail("youremail@yourdomain.com", "Website Enquiry", $msg, "From: $name  <$email>");
   
   echo "/linktothankyoupage.html";
   }

 

 

thanks for the help. as i said, i'm very grateful.

 

cheers.

 

 

Link to comment
Share on other sites

ok. i see that echo only sends them to mailer.php and gives the user a thank you message. is there a way to use echo, or another function to either redirect them to a separate html page or trigger hidden divs on my main contact page?

 

don't worry, i'm reading php tutorials while working on this. it's just that my brain is so full of information, at this point, that your help is what's keeping me sane.  ;)

 

thanks.

Link to comment
Share on other sites

i added:

 

if(!$errors)
   {
   $msg = "Full Name: $name\n\nCompany: $company\n\nEmail Address: $email\n\nPhone: $phone\n\nProject Description: $projectdesc\n\n";
   
   mail("youremail@yourdomain.com", "Website Enquiry", $msg, "From: $name  <$email>");
   
   include ("http://mysite.com/confirm.html");
}

 

in place of:

 

echo "Thanks. Your email has been sent!"

 

this gives me all of the html that is on confirm.html but css is not included. how would i do a straight redirect to confirm.html?

 

cheers!

 

 

 

 

Link to comment
Share on other sites

i added:

 

if(!$errors)
   {
   $msg = "Full Name: $name\n\nCompany: $company\n\nEmail Address: $email\n\nPhone: $phone\n\nProject Description: $projectdesc\n\n";
   
   mail("youremail@yourdomain.com", "Website Enquiry", $msg, "From: $name  <$email>");
   
   include ("http://mysite.com/confirm.html");
}

 

in place of:

 

echo "Thanks. Your email has been sent!"

 

this gives me all of the html that is on confirm.html but css is not included. how would i do a straight redirect to confirm.html?

 

cheers!

 

 

 

 

 


if(!$errors)
   {
   $msg = "Full Name: $name\n\nCompany: $company\n\nEmail Address: $email\n\nPhone: $phone\n\nProject Description: $projectdesc\n\n";
   
   mail("youremail@yourdomain.com", "Website Enquiry", $msg, "From: $name  <$email>");
   
   header('Location: http://www.ThePageYouWantToDirectTo.com/');//HTML Cannot be displayed prior using header
//If you absolutely have to you can use ob_start(); and ob_flush(); 
}


Make sure your action is point to your php page. Header will redirect it to the page you want after it's been submitted.

Link to comment
Share on other sites

so here's what i have, so far.

 

not going to post the entire code, but here's my action:

 

<form id="contact-form"  method="post" action="/scripts/mailer2.php">

 

and the php:

 

<?php
//If the form is POSTed, do the following
if($_SERVER['REQUEST_METHOD'] == 'POST') {
    
    //Get the POSTed data and store in the $ variables
   $name           = $_POST['name'];
   $company         = $_POST['company'];
   $email          = $_POST['email'];
   $phone          = $_POST['phone'];
   $message   = $_POST['message'];
   
   //If the name field is blank, retun the below error
   if(!$name) 
   {
   echo "You must enter a name.";
   }

   //If the email address field is blank, retun the below error
   if(!$email) 
   {
   echo "You must enter an email address";
   }

   //If the email address entered appears to be of an incorrect format return the below error
   elseif (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email))
   { 
   echo "You must enter a valid email address";
   }

   //If the phone number field is blank, retun the below error
   if(!$phone) 
   {
   echo "You must enter a phone number";
   }

if(!$errors)
   {
   $msg = "Full Name: $name\n\nCompany: $company\n\nEmail Address: $email\n\nPhone: $phone\n\nMessage: $message\n\n";
   
   mail("youremail@yourdomain.com", "Website Enquiry", $msg, "From: $name  <$email>");
   
   header('Location: http://mysite.com/confirm.html/');
   }

?>

 

here's what i'm getting:

 

Parse error: syntax error, unexpected $end in mydirectory/scripts/mailer2.php on line 45

 

 

Link to comment
Share on other sites

oh! this rocks.  :D

 

form works! thanks, everyone, for all of the help.

 

last thing, i think. instead of having:

 

   //If the name field is blank, retun the below error
   if(!$name) 
   {
   echo "You must enter a name.";
   }

   //If the email address field is blank, retun the below error
   if(!$email) 
   {
   echo "You must enter an email address";
   }

   //If the email address entered appears to be of an incorrect format return the below error
   elseif (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email))
   { 
   echo "You must enter a valid email address";
   }

   //If the phone number field is blank, retun the below error
   if(!$phone) 
   {
   echo "You must enter a phone number";
   }

 

can i send them to another html page similar to the one i'm sending them to with the header command, but for errors?

Link to comment
Share on other sites

You can define as meny redirects as you want and make the actual redirect based on actions. E.g

 

<?php
if (form successful)
{
    header('Location: succesful.php');
}
else if (some other condition)
{
    header('Location: other.php');
}
else
{
    header('Location: error.php');
}

Link to comment
Share on other sites

but there can only be one header per page, right?

 

what i want to do is have an error page "ohno.html" and a thank you page "thanks.html"

 

i simply want to maintain the styling throughout the site instead of having a generic white page that says whatever the error is.

 

is there a way to do this?

 

You can just style the php page instead of having all the redirects. It'll only be one direct depending on the action. Like Tendolla's example.

Link to comment
Share on other sites

i was looking for something more simple. (for me)

 

i found this:

 

http://www.quackit.com/html/codes/html_form_to_email.cfm

 

here's the php::

 

<?php
// This function checks for email injection. Specifically, it checks for carriage returns - typically used by spammers to inject a CC list.
function isInjected($str) {
$injections = array('(\n+)',
'(\r+)',
'(\t+)',
'(%0A+)',
'(%0D+)',
'(%08+)',
'(%09+)'
);
$inject = join('|', $injections);
$inject = "/$inject/i";
if(preg_match($inject,$str)) {
	return true;
}
else {
	return false;
}
}

// Load form field data into variables.
$email_address = $_REQUEST['email_address'] ;
$comments = $_REQUEST['comments'] ;

// If the user tries to access this script directly, redirect them to feedback form,
if (!isset($_REQUEST['email_address'])) {
header( "Location: feedback_form.html" );
}

// If the form fields are empty, redirect to the error page.
elseif (empty($email_address) || empty($comments)) {
header( "Location: error_message.html" );
}

// If email injection is detected, redirect to the error page.
elseif ( isInjected($email_address) ) {
header( "Location: error_message.html" );
}

// If we passed all previous tests, send the email!
else {
mail( "name@example.com", "Feedback Form Results",
  $comments, "From: $email_address" );
header( "Location: thank_you.html" );
}
?>

 

can this be done to my code? and if yes, how? thank you all.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.