• Hi,

    I’m trying to send an email true a ajax contact form, but something seems to be wrong. I see the ajax load the right file (mail.php) but still it doesn’t send the email.

    I have 4 different files for this action:
    – form
    – default.js
    – mail.php
    – functions.php

    form:

    <form id="contact_form" action="" method="POST">
                <input id="name" type="text" name="name">
                <input id="email" type="email" name="email">
                 <select id="location" name="location">
                	<?php
    				$args = array('post_type' => 'locatie');
    				$loop = new WP_Query( $args ); 
    
                     while ( $loop->have_posts() ) : $loop->the_post();
    
                 		 echo '<option value="'.get_the_title().'">'.get_the_title().'</option>';
    
                     endwhile;
    				 ?>
                </select>
                 <select id="type" name="type">
                  <option value="huren">Huren</option>
                  <option value="adverteren">Adverteren</option>
                </select>
                <textarea id="message" name="message">Opmerkingen en/of vragen</textarea>
    
                <input id="submit" type="submit" value="Verzenden">
    </form>

    default.js

    jQuery('#contact_form').submit(ajaxSubmit);
    
    	function ajaxSubmit(){
    		jQuery.ajax({
    			type:"POST",
    			cache: false,
    			url: "/wp-content/themes/test/mail.php",
    			data: $('#contact_form').serialize(),
    			dataType: "html",
    			success:function(data){
    				alert(data);
    			}
    		});
    		return false;
    	}

    mail.php
    <?php contact_send_message(); ?>

    functions.php

    function contact_send_message() {
        $contact_errors = false;
    
        $name = $_POST["name"];
        $email = $_POST["email"];
        $message = $_POST["message"];
        $type = $_POST["type"];
        $location = $_POST["location"];
    
        $header .= "From: ". $name ." <".$email.">";
    
        $message .= "Naam: $vname" . "\r\n";
        $message .= "E-mail: $email" . "\r\n";
        $message .= "Type: $type" . "\r\n";
        $message .= "Locatie: $location" . "\r\n";
        $message .= "Vraag en/of opmerking: $message";
    
        $subject = "e-mail via test.be";
        $subject = "=?utf-8?B?" . base64_encode($subject) . "?=";
    
        $to = "[email protected]";
    
    	wp_mail($to, $subject, $message, $header);
    
        if( !wp_mail($to, $subject, $message, $header) ) {
            $contact_errors = true;
        }
    }
    add_action('contact_send_message', 'contact_send_message');

    Any idea what’s wrong?
    Thanks

Viewing 4 replies - 1 through 4 (of 4 total)
  • Is your webserver correctly configured to send emails ? Not all hosting setups enable email by default.
    Does your website send emails when a new user is registered ?
    If email is not enabled, then seek support from your hosting company.

    Suggest that you install a browser inspector such as firebug, it will give you a javascript console, enable you to see javascript errors and ajax traffic.

    Thread Starter TA003

    (@ta003)

    My webserver is configured to send emails, and I have installed firebug. Firebug shows no errors en the ajax succes is shown, it just doesn’t send the email or outputs any data I put on the mail.php file.

    Can’t figure out where it is going wrong.
    Thanks for the reply.

    How about adding some debug output. Have the mail.php echo back to the js code the result of the wp_mail call, have it say either “all good” or “nyet”, whatever. At the moment I can’t tell if wp_mail is being called or not. Add a failure result function too, we have to gather more information.

    Thread Starter TA003

    (@ta003)

    The problem seems to be solved.
    I have re-written the code, sometime the best solution apparently.

    I still don’t know the mistake, but I’m glad it works.
    Thanks for help.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘ajax form’ is closed to new replies.