Stumped on basic email form question

I realize that there are about 100000 online sources for this sort of thing, but after several hours of work I am getting nowhere.
I am trying to get the code below tied into a php backend process, but I am just lost. I have tried to cannibalize the code from other forms, but it isn’t working. I don’t know how to get this thing to work. I need my hand held I am afraid. Here is my form code:

<form id="ContactForm" method="post">
		<div>
		  <div  class="wrapper">
		    <strong>Name:</strong>
			<div class="bg"><input type="text" class="input" >
                     </div>
		 </div>
	     <div  class="wrapper">
                <strong>Email:</strong>
		   <div class="bg"><input type="text" class="input" >	
                 </div>							
	     </div>
		<div  class="textarea_box">
		   <strong>Message:</strong>
		       <div class="bg"><textarea name="textarea" cols="1" rows="1"></textarea>		
                  </div>
              </div>
      <a href="#" class="button" onClick="document.getElementById('ContactForm').submit()"><span><span>Send</span></span></a>
      <a href="#" class="button" onClick="document.getElementById('ContactForm').reset()"><span><span>Clear</span></span></a>
   </div>
</form>

I need to go clearly, step by step, through getting an email from this form to me using php.
Thank you so much, in advance, to the patient person who takes this on :slight_smile:

Barbra
P.S. For some reason when I preview this message I see pac-man interspersed throughout my code. I dunno why, it’s actually the opening portion of my tag, just ignore that part and make the mental substitution.

Edit:

You didn’t turn off smilies or wrap the code in bbcode tags - see the “advvanced” editor

Hi barbra. Welcome to the forums. :slight_smile:

The first problem I see with your code is that there’s something critical missing at the top—the action=“” attribute. Without that, the form can’t communicate with the PHP script.

Presumably you have a PHP script somewhere that will process this form. Let’s say it’s in a file called formscript.php, and let’s assume that file is in the same folder as the page that displays the form. In that case, the first line of your form should read

<form id="ContactForm" method="post" [COLOR="#0000CD"]action="formscript.php"[/COLOR]>

Does that make any sense?

Thank you for the reply, and the welcome :slight_smile:

It certainly does make sense, and believe it or not that is the one small piece of info I do know. The problem is that I do not have a backend script, that is what I have been trying to cannibalize from other forms. And as you point out, with the lack of the “action” tag I may not even have a complete front end. I can just go to the internet and download one of those pre-made contact forms and drop the files and code in the appropriate spot, but the form as it currently appears is very nice and suits the website well, so I would prefer to get it tied in with a backend script.

The one I have been trying to get to work is here:

<?php
$errors = ‘’;
$myemail = ‘info@firstvirginiaservices.com’;//<-----Put Your email address here.
if(empty($_POST[‘name’]) ||
empty($_POST[‘email’]) ||
empty($_POST[‘message’]))
{
$errors .= "
Error: all fields are required";
}

$name = $_POST[‘name’];
$email_address = $_POST[‘email’];
$message = $_POST[‘message’];

if (!preg_match(
“/[1]+(\.[_a-z0-9-]+)@[a-z0-9-]+(\.[a-z0-9-]+)(\.[a-z]{2,3})$/i”,
$email_address))
{
$errors .= "
Error: Invalid email address";
}

if( empty($errors))
{
$to = $myemail;
$email_subject = “Contact form submission: $name”;
$email_body = "You have received a new message. “.
" Here are the details:
Name: $name
Email: $email_address
Message
$message”;

$headers = "From: $myemail\

";
$headers .= “Reply-To: $email_address”;

mail($to,$email_subject,$email_body,$headers);
//redirect to the 'thank you' page
header('Location: thanks.html');

}
?>


  1. _a-z0-9- ↩︎

I am sorry to be the one to tell you this.

That HTML form you showed clearly says that you do not have a clue how HTML forms work.

So, that should be your first port of call.

Find a basic html forms tutorial or a book.

Have a play with forms, get them working satisfactorily without PHP - forget Javascript enhancements until you have nailed this basic skill.

This comment:

I can just go to the internet and download one of those pre-made contact forms and drop the files and code in the appropriate spot,

… says that you clearly do not have even an inkling of the kind of damage you could be leaving yourself open to, damage to your server (or god forbid) your potential clients.

The existence of properly secure email form handlers which are fit for all situations, and all levels of user is a total myth.

You need to understand the fundamentals of PHP security before you can make an assessment about what is secure and usable, before you put anything like this on the web.

Buy some books or invest some time in following tutorials, make some efforts then come back here with questions and code you’d like us to comment on.

The best thing you did today was probably to ask your question here, but web development is not as easy as copy and pasting stuff you find on the web.

Sorry to be the one to tell you but at least I took the time to reply honestly to you.

Hi Barbra,

CUPS advice is good, however I was working this up for you prior to CUPS comments so, this is a very traditional way to hook up an email form.


<?php
/*
 * Check for emptyp fields
 */
if($_POST['email_posted']){
    /*
     * Array uses to store cleaned values
     */
    $CLEAN = array();
    foreach($_POST as $key => $value){
        if(empty($value)){
            echo "\
 Error: all fields are require";
            return 0;
        }
        /*
         * Sanatize and Validate the inputs
         */
        if($key != 'email_address'){
             $cleansed = filter_var($value, FILTER_SANITIZE_STRING);
        } else {
            $cleansed = filter_var($value, FILTER_SANITIZE_EMAIL);
        }
        if($cleansed == FALSE){
            echo "\
 Error: Your email failed check your email address and make
            sure you don't type any HTML";
            return 0;
        } else {
            $CLEAN[$key] = $cleansed;
        }
    }


    /*
     * Now pass the values to your email object
     */
     var_dump($CLEAN);
}




?>
<form id="ContactForm" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" >
   <fieldset>
       <label for="users_name"><span>Name:</span>
            <input name='users_name' type="text" value="" />
       </label>
       <label for="email_address"><span>Email:</span>
            <input name='email_address' type="text" value="" />
       </label>
       <label for="message"><span>Message:</span>
            <TEXTAREA name="message"
              rows="20" cols="50"
              >
            </TEXTAREA>
       </label>
   </fieldset>
   <fieldset>
        <ul class='buttons_right'>
            <li><input type="submit"  name="email_posted" value="Send"></li>
        </ul>
  </fieldset>

You can do nicer forms with javascript and libraries like jquery. For example you can have custom buttons (don’t need to use the harder to style <input type=‘submit’ button). You also can submit a post using ajax so the page doesn’t refresh and you don’t have to post the page back to itself like I have shown above. However hopefully this gives you a little better idea.

I would recommend that you look at swiftmailer.org and get the Swiftmailer email library for PHP. It is very well written and well documented so you can read and understand how to implement the actual sending of the email.

Regards,
Steve