1

I am trying to understand how to make a form submit button to work... I took template from bootstrap and hosted it on Github. I know Github doesn't allow anything rather than a static website, so I hosted the PHP code in another server.

On this project https://github.com/samu101108/02PortfolioPage_FCC02, there is a folder 'mail', that has 'contact_me.php', which is now placed in other server. I did the changes the template says to do but it is not working.

I've see that on index.html file, the tag form doesn't have 'method' and 'action' attributes (weird...I've never seen), so I saw there is a 'js' folder with 'contact_me.js'...over there on line 24 there was //url: "././mail/contact_me.php", and I substituted for url: "http://samuel.net46.net/toExternal/contact_me.php", thinking that could solve the issue, but not.

I would like to know what to do and why there are no 'action' and 'method' attributes on form. Thanks in advance!

index.html 

    <!-- Contact Section -->
    <section id="contact">
        <div class="container">
            <div class="row">
                <div class="col-lg-12 text-center">
                    <h2>Contact Me</h2>
                    <hr class="star-primary">
                </div>
            </div>
            <div class="row">
                <div class="col-lg-8 col-lg-offset-2">
                    <!-- To configure the contact form email address, go to mail/contact_me.php and update the email address in the PHP file on line 19. -->
                    <!-- The form should work on most web servers, but if the form is not working you may need to configure your web server differently. -->
                     <form name="sentMessage" id="contactForm" novalidate>
                    <!-- Line test<form name="sentMessage" id="contactForm" method="post" action="http://samuel.net46.net/toExternal/contact_me.php"> -->
                        <div class="row control-group">
                            <div class="form-group col-xs-12 floating-label-form-group controls">
                                <label>Name</label>
                                <input type="text" class="form-control" placeholder="Name" id="name" required data-validation-required-message="Please enter your name.">
                                <p class="help-block text-danger"></p>
                            </div>
                        </div>
                        <div class="row control-group">
                            <div class="form-group col-xs-12 floating-label-form-group controls">
                                <label>Email Address</label>
                                <input type="email" class="form-control" placeholder="Email Address" id="email" required data-validation-required-message="Please enter your email address.">
                                <p class="help-block text-danger"></p>
                            </div>
                        </div>
                        <div class="row control-group">
                            <div class="form-group col-xs-12 floating-label-form-group controls">
                                <label>Phone Number</label>
                                <input type="tel" class="form-control" placeholder="Phone Number" id="phone" required data-validation-required-message="Please enter your phone number.">
                                <p class="help-block text-danger"></p>
                            </div>
                        </div>
                        <div class="row control-group">
                            <div class="form-group col-xs-12 floating-label-form-group controls">
                                <label>Message</label>
                                <textarea rows="5" class="form-control" placeholder="Message" id="message" required data-validation-required-message="Please enter a message."></textarea>
                                <p class="help-block text-danger"></p>
                            </div>
                        </div>
                        <br>
                        <div id="success"></div>
                        <div class="row">
                            <div class="form-group col-xs-12">
                                <button type="submit" class="btn btn-success btn-lg">Send</button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </section>
contact_me.js

$(function() {

    $("#contactForm input,#contactForm textarea").jqBootstrapValidation({
        preventSubmit: true,
        submitError: function($form, event, errors) {
            // additional error messages or events
        },
        submitSuccess: function($form, event) {
            // Prevent spam click and default submit behaviour
            $("#btnSubmit").attr("disabled", true);
            event.preventDefault();

            // get values from FORM
            var name = $("input#name").val();
            var email = $("input#email").val();
            var phone = $("input#phone").val();
            var message = $("textarea#message").val();
            var firstName = name; // For Success/Failure Message
            // Check for white space in name for Success/Fail message
            if (firstName.indexOf(' ') >= 0) {
                firstName = name.split(' ').slice(0, -1).join(' ');
            }
            $.ajax({
                url: "http://samuel.net46.net/toExternal/contact_me.php",
                //url: "././mail/contact_me.php",
                // samuel.net46.net/toExternal/contact_me.php
                type: "POST",
                data: {
                    name: name,
                    phone: phone,
                    email: email,
                    message: message
                },
                cache: false,
                success: function() {
                    // Enable button & show success message
                    $("#btnSubmit").attr("disabled", false);
                    $('#success').html("<div class='alert alert-success'>");
                    $('#success > .alert-success').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;")
                        .append("</button>");
                    $('#success > .alert-success')
                        .append("<strong>Your message has been sent. </strong>");
                    $('#success > .alert-success')
                        .append('</div>');

                    //clear all fields
                    $('#contactForm').trigger("reset");
                },
                error: function() {
                    // Fail message
                    $('#success').html("<div class='alert alert-danger'>");
                    $('#success > .alert-danger').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;")
                        .append("</button>");
                    $('#success > .alert-danger').append("<strong>Sorry " + firstName + ", it seems that my mail server is not responding. Please send your message right to my email samu101108@gmail.com");
                    $('#success > .alert-danger').append('</div>');
                    //clear all fields
                    $('#contactForm').trigger("reset");
                },
            });
        },
        filter: function() {
            return $(this).is(":visible");
        },
    });

    $("a[data-toggle=\"tab\"]").click(function(e) {
        e.preventDefault();
        $(this).tab("show");
    });
});

// When clicking on Full hide fail/success boxes
$('#name').focus(function() {
    $('#success').html('');
});
concact_me.php

<?php
// Check for empty fields
if(empty($_POST['name'])    ||
   empty($_POST['email'])   ||
   empty($_POST['phone'])   ||
   empty($_POST['message']) ||
   !filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
   {
 echo "No arguments Provided!";
 return false;
   }

$name = strip_tags(htmlspecialchars($_POST['name']));
$email_address = strip_tags(htmlspecialchars($_POST['email']));
$phone = strip_tags(htmlspecialchars($_POST['phone']));
$message = strip_tags(htmlspecialchars($_POST['message']));

// Create the email and send the message
$to = 'samu101108@gmail.com'; // Add your email address inbetween the '' replacing
//yourname@yourdomain.com - This is where the form will send a message to.
$email_subject = "Website Contact Form:  $name";
$email_body = "You have received a new message from your website contact form.\n\n"."Here are the details:\n\nName: $name\n\nEmail: $email_address\n\nPhone: $phone\n\nMessage:\n$message";
$headers = "From: samu101108@gmail.com\n";//This is the email address the generated
//message will be from. We recommend using something like noreply@yourdomain.com.
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
return true;
?>
samu101108
  • 675
  • 7
  • 23
  • 1
    Instead of attaching links add the code blocks please – neophyte Jan 25 '17 at 00:28
  • I accept pull requests! ;) – samu101108 Jan 25 '17 at 00:29
  • I meant if you post atleast a portion of your code it will help us to understand the problem. its difficult to follow links and understand what's wrong. – neophyte Jan 25 '17 at 00:30
  • ok...editing...to make it easy...one moment please. – samu101108 Jan 25 '17 at 00:31
  • 1
    "and why there are no 'action' and 'method' attributes on form" - because javascript, more precise: ajax request handles that (check $.ajax part in js code)... And, regarding the rest: http://stackoverflow.com/questions/298745/how-do-i-send-a-cross-domain-post-request-via-javascript – sinisake Jan 25 '17 at 00:54

1 Answers1

0

I found a workaround to solve the problem... Well...I understood the issue was ssl certification. Github uses it and when I put the PHP part on an insecure server, the connection is refused. I could see it on cosole:enter image description here

So I moved all the project to a server with no https and as a result it works and I received the message! enter image description here

samu101108
  • 675
  • 7
  • 23