HTML Contact Form with PHP code- HELP

Discussion in 'HTML & Website Design' started by Darwin Leon, Apr 9, 2013.

  1. #1
    Hi am starting with the web programming and learning its basics. Practicing with some files in the web, I have a template of contact in which the user can send an email to the creator. File contact is made in html and the Manager is in php. This template is made at Template Monster and I have reviewed several but does not correspond or supports provided or other references on the Internet.

    I try to make the form work, but by simply adding my domain email isn't enough I edit some lines more but can not find them.
    Can you help me?






    HTML CONTENT

    <form id="contact-form" method="post">
     
                                  <fieldset>
     
                                    <label class="name">
     
                                        <input type="text" value="Nombre" onfocus="if(this.value=='Your Name'){this.value=''}" onblur="if(this.value==''){this.value='Your Name'}">
     
                                    </label>
     
                                    <label class="phone">
     
                                        <input type="text" value="Teléfono" onfocus="if(this.value=='Telephone'){this.value=''}" onblur="if(this.value==''){this.value='Telephone'}">
     
                                    </label>
     
                                    <label class="email">
     
                                      <input type="email" value="Email" onfocus="if(this.value=='Email'){this.value=''}" onblur="if(this.value==''){this.value='Email'}">
     
                                    </label>
     
                                    <label class="message">
     
                                      <textarea onfocus="if(this.value=='Message'){this.value=''}" onblur="if(this.value==''){this.value='Message'}">Mensaje</textarea>
     
                                    </label>
     
                                    <div class="btns">
     
                                        <a class="button" onClick="document.getElementById('contact-form').reset()">Borrar</a>
     
                                        <a class="button" onClick="document.getElementById('contact-form').submit()">Enviar</a>
     
                                    </div>
     
                                  </fieldset>
     
                                </form>
    HTML:

    PHP MAIL HANDLER
    <?php
        $owner_email = $_POST["lansss@lanslatin.com"];
        $headers = 'From:' . $_POST["email"];
        $subject = 'A message from your site visitor ' . $_POST["name"];
        $messageBody = "";
       
        if($_POST['name']!='nope'){
            $messageBody .= '<p>Visitor: ' . $_POST["name"] . '</p>' . "\n";
            $messageBody .= '<br>' . "\n";
        }
        if($_POST['email']!='nope'){
            $messageBody .= '<p>Email Address: ' . $_POST['email'] . '</p>' . "\n";
            $messageBody .= '<br>' . "\n";
        }else{
            $headers = '';
        }
        if($_POST['phone']!='nope'){       
            $messageBody .= '<p>Phone Number: ' . $_POST['phone'] . '</p>' . "\n";
            $messageBody .= '<br>' . "\n";
        }   
        if($_POST['message']!='nope'){
            $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";
        }
    ?>
    PHP:
    These codes are free templates.I have tried to change some lines of the code to load it into the hostings erver but the portal does not send the mail. Even if I change the line 'owner_email' this generates an error to me and throws me the mail failure page.What can I do?
     
    Last edited: Apr 9, 2013
    Darwin Leon, Apr 9, 2013 IP
  2. dafuqjoe

    dafuqjoe Member

    Messages:
    34
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #2
    First of all: Your code is so ugly with all this CSS
    [COLOR=#000000][SIZE=12px][FONT=Arial]
    Code (markup):
    written like that. I had to think twice if I even want to take a look at it.

    Edit
    $owner_email = $_POST["lansss@lanslatin.com"];
    Code (markup):
    to
    $owner_email = "lansss@lanslatin.com";
    Code (markup):
    and try again.

    You could also add
    $headers .= 'X-Mailer: PHP/' . phpversion() . "\r\n";
    $headers .= "Content-type: text/html; charset=UTF-8\r\n";
    Code (markup):
    to your code.

    If it still doesn't work, make sure your server / hosting provider does support emails.

    Edit: If you want to learn the web programming and its basics, don't start with templates.
    Write this script on your own, only then you understand what it does.
     
    dafuqjoe, Apr 9, 2013 IP
  3. Darwin Leon

    Darwin Leon Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #3
    Thank you, those lines of code CSS appeared to correct my publication, however they already do not appear. My server supports the sent mail in a form. HadalreadymadethischangeI will tryitagainby addingthelinesthat yourecommend.

    Thank you very much.
     
    Darwin Leon, Apr 9, 2013 IP
  4. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #4
    You have label tags when you don't have actual text for the labels... pull stupid javascript nonsense not just for the 'false simplicity' placeholder texts but worse, pissing away accessibility using anchors instead of input[reset] and input[submit]... and I'm not even seeing an ACTION attribute for that 'submit' to even WORK. You haven't set anything for it to submit TO! Much less there are no NAME attributes, so those inputs can't actually send anything to the server in the first place!!!

    I've no clue why you are checking for the value "nope" as that's just bizarro-land gibberish, the multiple if statements for no real reason, having the 'stripHTML' being set from client side (which is something that should NEVER be passed via client-side), and really tag stripping and character escaping should be done on ALL $_POST values if your gonna feed them into mail().

    That mail form is gibberish, the server-side code is WORSE. One sec, lemme do a rewrite... let's see... first step is to clean up the markup and strip out all the "gee ain't it neat" scripted crap that destroys it's functionality, while plugging in all the things it's missing.

    <form id="contactForm" method="post" action="mailer.php">
    
    	<fieldset>
    
    		<label for="contactName">Your Name:</label>
    		<input type="text" name="name" id="contactName" />
    		<br />
    
    		<label for="contactPhone">Phone Number:</label>
    		<input type="text" name="phone" id="contactPhone" />
    		<br />
    
    		<label for="contactEMail">Contact E-Mail:</label>
    		<input type="text" name="eMail" id="contactEMail" />
    		<br />
    
    		<label for="contactMessage">Message:</label>
    		<textarea name="message" id="contactMessage"></textarea>
    
    	</fieldset>
    
    	<div class="submitsAndHiddens">
    		<input type="reset" value="Reset" id="contactReset" />
    		<input type="submit" value="Submit" id="contactSubmit" />
    	</div>
    
    </form>
    Code (markup):
    Excuse me for swapping it all back to English, it was confusing it bouncing back and forth between it and... I don't even know what.

    I'm on the way out the door, I'll tackle the PHP later on... pretty much what you have there is just plain nonsense. Needs to be thrown out completely as that's just not how this stuff should be done.
     
    deathshadow, Apr 9, 2013 IP
  5. dafuqjoe

    dafuqjoe Member

    Messages:
    34
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #5
    lol, didn't look at the html code... Dude check out SelfHTML to learn HTML.
    For your JS code you can simply use
    placeholder="phone" 
    Code (markup):
    . Does the same.

    A very easy php for deathshadow's html would be

    <?php
        $owner_email = "lansss@lanslatin.com";
       
        $subject = $_POST["name"]." contacted you, dear owner!";
        $message = "His phone number is: ".$_POST["phone"]."<br \> and his contact mail is: ".$_POST["eMail"];
        $message.= "<br \><br \> He wanted to tell you: <br \>".$_POST["message"];
        $header = "From: ".$owner_email . "\r\n";
        $header .= "X-Mailer: PHP/" . phpversion() . "\r\n";
        $header .= "Content-type: text/html; charset=UTF-8\r\n";
               
        if (@mail($owner_email, $subject, $message, $header) === true) {
                        //mail sent
        }
        else{
                echo "ERROR!!!";
        }
       
    ?>
    
    Code (markup):
    But I would really recommend to check the submitted data and use some proper code. I just coded this as easy as possible for you to understand.
     
    dafuqjoe, Apr 10, 2013 IP
  6. Darwin Leon

    Darwin Leon Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #6
    Hello guys... Thanks so much by your comments...


    This template was sent to me for review by the problem with sending this form. Little handling web programming, I was not the designer of the code to the contact form. I'm just trying to understand what created according to that page and also according to what you want this person. As I wrote this template was purchased in Template Monster and modified it from there, however only support on the gallery but no contact form.

    I've tried using both recommendations and hopefully positive results. e have ayudaod to understand a little more like codes work to submit a form which I see is not easy and less to start.


    He page is in Spanish so the code to publish there words you do not understand.

    DeathShadow ( Can i use your example code to edit the Html code, but whats happend with the PHP mail handler. I want to know how this code work Or you recommend I do my own code based on what you guys are showing. If so, what should I take into consideration??).
     
    Darwin Leon, Apr 13, 2013 IP
  7. Milo Odin

    Milo Odin Greenhorn

    Messages:
    57
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #7
    Save this code as submit.php and upload to your host and than place that link in submit button of your contact form.

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <META HTTP-EQUIV="refresh" content="0;URL=thankyou.html">
    <title>Email Form</title>
    </head>
     
    <body>
    <?php
      $name=addslashes($_POST['name']);
      $email=addslashes($_POST['email']);
      $comments=addslashes($_POST['message']);
     
    // you can specify which email you want your contact form to be emailed to here
     
      $toemail = "abc@YOURSITE.com";
      $subject = "from YOUR SITE";
     
      $headers = "MIME-Version: 1.0\n"
                ."From: \"".$name."\" <".$email.">\n"
                ."Content-type: text/html; charset=iso-8859-1\n";
     
      $body = "Name: ".$name."<br>\n"
                ."Email: ".$email."<br>\n"
                ."Comments:<br>\n"
                .$comments;
     
      if (!ereg("^[a-zA-Z0-9_]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$", $email))
      {
        echo "That is not a valid email address.  Please return to the"
              ." previous page and try again.";
        exit;
      }
     
        mail($toemail, $subject, $body, $headers);
        echo "Thanks for submitting your comments";
    ?>
    </body>
    </html>
    PHP:
     
    Milo Odin, Apr 14, 2013 IP
  8. nav0413

    nav0413 Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #8
    Heheh, I also looking for this help, thanks for the help :)
     
    nav0413, Apr 16, 2013 IP
  9. rypher21

    rypher21 Active Member

    Messages:
    914
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    58
    #9
    So that is how to do it? Thank you so much for the comments. We have the same problem. Now I know what to do. :)
     
    rypher21, Apr 23, 2013 IP
  10. Ricardo2016

    Ricardo2016 Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #10
    Hello , I need MailHandler.php send using SMTP authentication . How would the code ?
     
    Ricardo2016, Apr 27, 2016 IP