PHP Form not working at root level - JS Validation issue, maybe

时间:2015-10-30 23:06:20

标签: php forms validation email root

I am totally stumped by this one. My PHP form works just fine when within a folder on my server see it here http://getnice.co.uk/formTest/

but the same code at root level does not work. Yes everything is pointing to the right places, so it is not that. http://getnice.co.uk/form.html

Below is the code.. However I think it is a server issue? correct me if I am wrong.

<!-- Contact start -->

<section id="contact" class="pfblock pfblock-gray">
    <div class="container">
        <div class="row">

            <div class="col-sm-8 col-sm-offset-2">

                <div class="pfblock-header">
                    <h2 class="pfblock-title">Contact us</h2>
                    <div class="pfblock-line"></div>
                    <div class="pfblock-subtitle">
                     Tell us about your business and what your needs are:
                    </div>
                </div>

            </div>

        </div><!-- .row -->

        <div class="row">

            <div class="col-sm-8 col-sm-offset-2">

                <form id="contact-form" role="form" method="post" action="assets/php/contact.php">
                    <!-- <div class="contact.php"> -->
                        <div class="form-group wow fadeInUp">
                            <label class="sr-only" for="c_name">Name</label>
                            <input type="text" id="c_name" class="form-control" name="c_name" placeholder="Name">
                        </div>

                        <div class="form-group wow fadeInUp" data-wow-delay=".1s">
                            <label class="sr-only" for="c_email">Email</label>
                            <input type="email" id="c_email" class="form-control" name="c_email" placeholder="E-mail">
                        </div>

                        <div class="form-group wow fadeInUp" data-wow-delay=".1s">
                            <label class="sr-only" for="c_design">Design you like</label>
                            <select  id="c_design" class="form-control" name="c_design" placeholder="Name of the Design you like">
                                <option value="" disabled selected>Select the design you like</option>
                                <option value="Office">Office Based Business</option>
                                <option value="Cafe or shop">Cafe or Shop</option>
                                 <option value="Portfolio">Portfolio style</option>
                                <option value="Other small">Other Small Business</option>
                                <option value="Music">Musician Band style</option>
                                <option value="Agency">Agency style</option>
                                <option value="custom">Custom design required</option>
                            </select>
                        </div>

                        <div class="form-group wow fadeInUp" data-wow-delay=".2s">
                            <textarea class="form-control" id="c_message" name="c_message" rows="7" placeholder="Message"></textarea>
                        </div>

                        <button type="submit" class="btn btn-lg btn-block wow fadeInUp" data-wow-delay=".3s">Send Message</button>
                    </div>
                    <div class="ajax-response"></div>
                </form> 

        </div><!-- .row -->
    </div><!-- .container -->
</section>

<!-- Contact end -->



<!-- Javascript files -->

<script src="assets/js/jquery-1.11.1.min.js"></script>
<script src="assets/bootstrap/js/bootstrap.min.js"></script>
<script src="assets/js/jquery.parallax-1.1.3.js"></script>
<script src="assets/js/imagesloaded.pkgd.js"></script>
<script src="assets/js/jquery.sticky.js"></script>
<script src="assets/js/smoothscroll.js"></script>
<script src="assets/js/wow.min.js"></script>
<script src="assets/js/waypoints.min.js"></script>
<script src="assets/js/jquery.cbpQTRotator.js"></script>
<script src="assets/js/custom.js"></script>

PHP

<?php
if(isset($_POST['c_message'])){

$name = $_POST['c_name'];
$email = $_POST['c_email'];
$varDropdown = $_POST['c_design'];
$message = $_POST['c_message'];
$to      = 'me@gmail.com';
$subject = 'Get Nice Contact Form';
$mailcontent2="<html>
<head>
</head>
<body>
<table width='800' border='0' cellspacing='2' cellpadding='0'>
  <tr>
    <td align='left'>
        <p>Hi,</p>
              <strong style='color:#C59800'>New request has been sent with the below details.</strong><br /><br />
              <strong>Name: </strong>".stripslashes($_REQUEST['c_name'])."<br />
              <strong>Email: </strong>".stripslashes($_REQUEST['c_email'])."<br />
              <strong>Design: </strong>".stripslashes($_REQUEST['c_design'])."<br />
              <strong>Message: </strong>".nl2br(stripslashes($_REQUEST['c_message']))."<br /><br />
        <p>Thanks</p>
    </td>
  </tr>
</table>
</body>
</html>
"; 
//echo $mailcontent2;
$headers2  = "MIME-Version: 1.0" . "\r\n";
$headers2 .= "Content-type: text/html; charset=iso-8859-1" . "\r\n";
$headers2 .= "From: ".$email." <".$email.">" . "\r\n";  
$status = mail($to,$subject,$mailcontent2,$headers2);


if($status == TRUE){    
    $res['sendstatus'] = 'done';

    //Edit your message here
    $res['message'] = 'Thank you your mail has been sent';
}
else{
    $res['message'] = 'Failed to send mail. Please email me at hello@getnice.co.uk';
}


echo json_encode($res);

}

JS

/* ---------------------------------------------- /*
     * E-mail validation
    /* ---------------------------------------------- */

    function isValidEmailAddress(emailAddress) {
        var pattern = new RegExp(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i);
        return pattern.test(emailAddress);
    };

    /* ---------------------------------------------- /*
     * Contact form ajax
    /* ---------------------------------------------- */

    $('#contact-form').submit(function(e) {

        e.preventDefault();

        var c_name = $('#c_name').val();
        var c_email = $('#c_email').val();
        var c_message = $('#c_message ').val();
        var c_design = $('#c_design ').val();
        var response = $('#contact-form .ajax-response');

        var formData = {
            'c_name'       : c_name,
            'c_email'      : c_email,
            'c_design'     : c_design,
            'c_message'    : c_message
        };

        if (( c_name== '' || c_email == '' || c_message == '') || (!isValidEmailAddress(c_email) )) {
            response.fadeIn(500);
            response.html('<i class="fa fa-warning"></i> Please fix the errors and try again.');
        }

        else {
                 $.ajax({
                        type        : 'POST', // define the type of HTTP verb we want to use (POST for our form)
                        url         : 'assets/php/contact.php', // the url where we want to POST
                        data        : formData, // our data object
                        dataType    : 'json', // what type of data do we expect back from the server
                        encode      : true,
                        success     : function(res){
                                        var ret = $.parseJSON(JSON.stringify(res));
                                        response.html(ret.message).fadeIn(500);
                        }
                    });
            }           
            return false;
        });

});

})(jQuery);

1 个答案:

答案 0 :(得分:1)

move the e.preventDefault(); towards the end in custom.js