简体   繁体   中英

Why won't my contact form work? the loading.gif is stuck and nothing happens

I have tried using my contact form with XAMPP i have configured .ini files etc. correctly to GMAIL for testing purposes. Can you see where I have made a mistake?

I have even tried WAMP and still nothing works.

I changed the xwamp files as this guy suggested: How to configure XAMPP to send mail from localhost?

HTML

<form method="post" action="contact-form.php" name="contactform" id="contactform">
    <fieldset>
        <input name="name" type="text" id="name" placeholder="Name"/> 
        <input name="email" type="text" id="email" placeholder="Email"/>  
        <input name="subject" type="text" id="subject" placeholder="Subject"/> 
    </fieldset>
    <fieldset> 
        <textarea name="comments" cols="40" rows="3" id="comments" placeholder="Message"></textarea>
    </fieldset>
    <input type="submit" class="submit" id="submit" value="Send Message" />
</form>

JS

 $('#contactform').submit(function(){
    var action = $(this).attr('action');
    $("#message").slideUp(250,function() {
        $('#message').hide();
        $('#submit')
            .after('<img src="img/assets/cbp-loading.gif" class="loader" />')
            .attr('disabled','disabled');
        $.post(action, {
            name: $('#name').val(),
            email: $('#email').val(),
            subject: $('#subject').val(),
            comments: $('#comments').val(),
        },
            function(data){
                document.getElementById('message').innerHTML = data;
                $('#message').slideDown(250);
                $('#contactform img.loader').fadeOut('slow',function(){$(this).remove()});
                $('#submit').removeAttr('disabled');
                if(data.match('success') != null) $('#contactform').slideUp(850, 'easeInOutExpo');
            }
        );
    });
    return false;   

PHP

 <?php
if (isset($_POST["submit"])) {
    $name = $_POST['name'];
    $email = $_POST['email'];
    $subject = 'Message from Contact Demo ';
    $message = $_POST['message'];
    $from = 'Demo Contact Form'; 
    $to = 'myemail@gmail.com'; 
    $body = "From: $name\n E-Mail: $email\n Message:\n $message";
    mail('myemail@gmail.com', $name, $email, $subject, $message);
}
?>

Sendmail code (i have removed the email and password obviously)

[sendmail]

smtp_server=smtp.gmail.com
smtp_port=587
error_logfile=error.log
debug_logfile=debug.log
auth_username=MYEMAIL@gmail.com
auth_password=MY EMAIL PASSWORD
force_sender=MYEMAIL@gmail.com

One of the most underestimated points is to no start XAMPP as administrator. In addition I'm missing some configuartions for smtp and co:

 $smtp = Mail::factory('smtp', array( 'host' => 'ssl://smtp.gmail.com', 'port' => '465', 'auth' => true, 'username' => 'EMAIL', 'password' => 'PASSWORD' )); 

Those are necessary to be allowed to sent to your email adress. If not anyone could spam your email adress.

Sincerely liqSTAR

There's no mail() function in the PHP code.

Would recommend stripping back to the simplest test case to start with. As a first test I would recommend writing a simple PHP file that just utilises the mail() function to send an email to your own address and returns the result (be sure to also check the PHP error log) - this will help to confirm PHP is configured with a working SMTP server.

I'd then try the form submission without the use of JavaScript to ensure that it's submitting okay. Does the $.post action appear in the console okay? Does it show anything as the response or status code?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM