Web Analytics Made Easy -
StatCounter PHP Forms + Submission Problem - CodingForum

Announcement

Collapse
No announcement yet.

PHP Forms + Submission Problem

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • PHP Forms + Submission Problem

    Hey Everyone!

    To preface my question I'll begin by saying I'm a complete PHP and coding newbie. If you need more information to help solve my problem then let me know -- I'll be more than happy to supply it!

    --- I've two main problems ---
    1. When a user fills out the form at the bottom of my website, the information isn't actually sent to my email. I'll receive an email -- but the contents will basically be empty. (The fields names will be there -- but nothing the user actually input)

    2. When a user submits -- it will jump to a new page and say "success". I just want a simple modal or pop up to say success -- not jump to a completely new page!

    Here's my code:

    PHP Code:
    <?php
    $name 
    $_POST["name"];
    $email $_POST["email"];
    $subject $_POST["subject"];
    $message $_POST["message"];

    $EmailTo "[email protected]";
    $Subject "New Message Received";

    // prepare email body text
    $Body .= "Name: ";
    $Body .= $name;
    $Body .= "\n";

    $Body .= "Email: ";
    $Body .= $email;
    $Body .= "\n";

    $Body .="Subject: ";
    $Body .= $subject;
    $Body .= "\n";

    $Body .= "Message: ";
    $Body .= $message;
    $Body .= "\n";

    // send email
    $success mail($EmailTo$Subject$Body"From:".$email);

    // redirect to success page
    if ($success){
       echo 
    "success";
    }else{
        echo 
    "invalid";
    }

    ?>
    Any suggestions? If I can get these two problems fixed then at least I'll have a semi-functional website up and running!

    Thanks for your time!

  • #2
    You are not showing us your actual form.

    In the form, did you spell 'message' correctly?
    <input type="text" name="message" ...

    The other part will require JQuery/AJAX to process the form and display a message without page refresh.
    You can find a lot of script examples via Google: PHP AJAX contact form example

    Comment


    • #3
      Do not create security holes by using code like $name = $_POST["name"]; - either just use the $_POST fields throughout your code so it is obvious that the values need not be valid OR validate the content of the S_POST fields before copying them to a new field name so you know that the value in the new field is valid whenever it is referenced.
      Stephen
      Learn Modern JavaScript - http://javascriptexample.net/
      Helping others to solve their computer problem at http://www.felgall.com/

      Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

      Comment


      • #4
        Sorry, I just don't know what code to give you all. Below is the HTML from the form and the validation part.

        <form role="form" id="contactform" form action="contact.php" method="post">
        <div class="form-group">
        <label for="name">Your Name</label>
        <input type="text" class="form-control" id="name">
        </div>
        <div class="form-group">
        <label for="email">Email address</label>
        <input type="email" class="form-control" id="email">
        </div>
        <div class="form-group">
        <label for="subject">Subject</label>
        <input type="subject" class="form-control" id="subject">
        </div>
        <div class="form-group">
        <label for="message">Message</label>
        <textarea class="form-control" id="message" rows="3"></textarea>
        </div>
        <button type="submit" class="btn">Submit</button>
        <div id="msgSubmit" class="h3 text-center hidden">Message Submitted!</div>
        </form>
        $("#contactForm").submit(function(event){
        // cancels the form submission
        event.preventDefault();
        submitForm();
        });

        $("#contactForm").validator().on("submit", function (event) {
        if (event.isDefaultPrevented()) {
        // handle the invalid form...
        formError();
        submitMSG(false, "Did you fill in the form properly?");
        } else {
        // everything looks good!
        event.preventDefault();
        submitForm();
        }
        });


        function submitForm(){
        // Initiate Variables With Form Content
        var name = $("#name").val();
        var email = $("#email").val();
        var subject = $("#subject").val();
        var message = $("#message").val();


        $.ajax({
        type: "POST",
        url: "php/contact.php",
        data: "name=" + name + "&email=" + email + "&msg_subject=" + msg_subject + "&message=" + message,
        success : function(text){
        if (text == "success"){
        formSuccess();
        } else {
        formError();
        submitMSG(false,text);
        }
        }
        });
        }

        function formSuccess(){
        $("#contactForm")[].reset();
        submitMSG(true, "Message Submitted!")
        }

        function formError(){
        $("#contactForm").removeClass().addClass('shake animated').one('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend', function(){
        $(this).removeClass();
        });
        }

        function submitMSG(valid, msg){
        if(valid){
        var msgClasses = "h3 text-center tada animated text-success";
        } else {
        var msgClasses = "h3 text-center text-danger";
        }
        $("#msgSubmit").removeClass().addClass(msgClasses).text(msg);
        }

        Comment


        • #5
          The reason the form is submitting directly to the .php page, rather than using the javascript, is because there are some errors in the javascript.

          Starting with, the id of the form doesn't match the jquery selector being used. id="contactform" (used in the form) is not the same as "#contactForm" (used in the jquery.) The letter case of these must match.

          For the rest of the errors, I recommend you use the developer tools - console in your browser to find each of them.
          Finding out HOW to do something is called research, i.e. keep searching until you find the answer. After you attempt to do something and cannot solve a problem with it yourself, would be when you ask others for help.

          Comment


          • #6
            EDIT EDIT EDIT:

            I just realised that this form is being sent via ajax which is composing the form data into data in kay=value pairs. I've left this post here though to help others with similar problems who might find this via google.


            Originally posted by whr2206 View Post
            1. When a user fills out the form at the bottom of my website, the information isn't actually sent to my email. I'll receive an email -- but the contents will basically be empty. (The fields names will be there -- but nothing the user actually input)

            Here's my code:

            PHP Code:
            $name $_POST["name"];
            $email $_POST["email"];
            $subject $_POST["subject"];
            $message $_POST["message"]; 
            Originally posted by whr2206 View Post
            Code:
            <form role="form" id="contactform" form action="contact.php" method="post">
            <div class="form-group">
            <label for="name">Your Name</label>
            <input type="text" class="form-control" id="name">
            </div>
            <div class="form-group">
            <label for="email">Email address</label>
            <input type="email" class="form-control" id="email">
            </div>
            <div class="form-group">
            <label for="subject">Subject</label>
            <input type="subject" class="form-control" id="subject">
            </div>
            <div class="form-group">
            <label for="message">Message</label>
            <textarea class="form-control" id="message" rows="3"></textarea>
            </div>
            <button type="submit" class="btn">Submit</button>
            <div id="msgSubmit" class="h3 text-center hidden">Message Submitted!</div>
            </form>
            Where is the name specified in any of those fields? It isn't. You've given all the fields an id but not a name. EG:

            <input type="email" class="form-control" id="email">

            should actually be:

            <input type="email" class="form-control" id="email" name="email">


            Change every field and give it a name - which will match the id. THEN when you will find that your $_POST array actually contains data with matching names that you can access. At that point, the variables you're assigning from that array and using in your email will contain data... though do note felgalls advice.
            Last edited by tangoforce; Sep 17, 2016, 09:33 AM.
            "Tango says double quotes with a single ( ' ) quote in the middle"
            '$Name says single quotes with a double ( " ) quote in the middle'
            "Tango says double quotes ( \" ) must escape a double quote"
            '$Name single quotes ( \' ) must escape a single quote'

            Comment

            Working...
            X