SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Jun 2012
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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:

    HTML 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

    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
    Last edited by Mittineague; Jun 20, 2012 at 13:38. Reason: reformatting bbcode tags

  2. #2
    It's all Geek to me silver trophybronze trophy
    ralph.m's Avatar
    Join Date
    Mar 2009
    Location
    Melbourne, AU
    Posts
    24,171
    Mentioned
    454 Post(s)
    Tagged
    8 Thread(s)
    Hi barbra. Welcome to the forums.

    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

    Code:
    <form id="ContactForm" method="post" action="formscript.php">
    Does that make any sense?

  3. #3
    SitePoint Member
    Join Date
    Jun 2012
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you for the reply, and the welcome :)

    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 .= "\n Error: all fields are required";
    }

    $name = $_POST['name'];
    $email_address = $_POST['email'];
    $message = $_POST['message'];

    if (!preg_match(
    "/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i",
    $email_address))
    {
    $errors .= "\n 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:\n Name: $name \n Email: $email_address \n Message \n $message";

    $headers = "From: $myemail\n";
    $headers .= "Reply-To: $email_address";

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

  4. #4
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    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.

  5. #5
    Foozle Reducer ServerStorm's Avatar
    Join Date
    Feb 2005
    Location
    Burlington, Canada
    Posts
    2,699
    Mentioned
    89 Post(s)
    Tagged
    6 Thread(s)
    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 Code:
    <?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 
    "\n Error: all fields are require";
                return 
    0;
            }
            
    /*
             * Sanatize and Validate the inputs
             */
            
    if($key != 'email_address'){
                 
    $cleansed filter_var($valueFILTER_SANITIZE_STRING);
            } else {
                
    $cleansed filter_var($valueFILTER_SANITIZE_EMAIL);
            }
            if(
    $cleansed == FALSE){
                echo 
    "\n 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
    ictus==""


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •