Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End html contact form not working

  • This topic is empty.
Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #249565
    chauhanheena
    Participant

    Hi
    I’m trying to create a simple html/php contact form. below is the code:

    html

    <div class="col-sm-8">
                                <div id="message"></div>
                                <form method="post" action="html_form_send.php" name="contactform" id="contactform">
                                    <fieldset>
                                        <input name="name" type="text" id="name" placeholder="Your Name*"/> 
                                        <input name="email" type="text" id="email" placeholder="Your Email*"/>
                                    </fieldset>
                                    <fieldset>
                                        <p class="antispam">Leave this empty:<input name="url" /></p>
                                        <textarea name="comments" cols="40" rows="3" id="comments" placeholder="Your Message*"></textarea>
                                    </fieldset>
                                    <input type="submit" class="submit" id="submit" value="Send Message" />
                                </form>
                            </div>
    

    php

    <?php
    if(isset($_POST['url']) && $_POST['url'] == ''){
    
        // CHANGE THE TWO LINES BELOW
        $email_to = "[email protected]";
        $email_subject = "Contact form on abc Website";
    
    
    
        function died($error) {
            // your error code can go here
            echo "We are very sorry, but there were error(s) found with the form you submitted. ";
            echo "These errors appear below.<br /><br />";
            echo $error."<br /><br />";
            echo "Please go back and fix these errors.<br /><br />";
            die();
        }
    
        // validation expected data exists
        if(!isset($_POST['name']) ||
            !isset($_POST['email']) ||
            !isset($_POST['comments'])) {
            died('We are sorry, but there appears to be a problem with the form you submitted.');       
        }
    
        $name = $_POST['name']; // required
        $email_from = $_POST['email']; // required
        $comments = $_POST['comments']; // required
    
    
        $email_message = "Form details below.\n\n";
    
    
    
        function clean_string($string) {
          $bad = array("content-type","bcc:","to:","cc:","href");
          return str_replace($bad,"",$string);
        }
    
        $email_message .= "Name: ".clean_string($name)."\n";
        $email_message .= "Email: ".clean_string($email_from)."\n";
        $email_message .= "Comments: ".clean_string($comments)."\n";
        $recipient = "[email protected]";    
    
    // create email headers
    $headers = 'From: '.$recipient."\r\n".
    'Reply-To: '.$recipient."\r\n" .
    'X-Mailer: PHP/' . phpversion();
    @mail($email_to, $email_subject, $email_message, $headers); 
    header("Location:http://www.abc.com/");  
    ?>
    
    <!-- place your own success html below -->
    
    header("Location:http://www.abc.com/");  
    
    <?php
    }
    die();
    ?>
    
    #249567
    damcel
    Participant

    What exactly is not working?

    Please post your client side validation and JS as well, could be that too, we never know:-)

    #249568
    chauhanheena
    Participant

    this is all the code that i have for the contact form

    the email is sent to the specified email id but validation is not done.
    Also after submit, it is directed to http://www.abc.com

    All i want is if the fields are empty, the an error message should display in the ‘message’ div

    and if the fields are successfully filled, the the submit should be successful and a success message should display in the message div

    #249572
    damcel
    Participant

    Here you go, try and submit the form without entering data:-)

    You have the client side validation JS and under that, there is the php(commented ouit).

    http://codepen.io/damianocel/pen/ygBwRY

Viewing 4 posts - 1 through 4 (of 4 total)
  • The forum ‘Back End’ is closed to new topics and replies.