1

i an new to wordpress theme development, i need to send an email with ajax/PHP in wordpress

var dataString =    'name=' + name + 
        '&email=' + email + 
        '&contact=' + contact + 
        '&technology=' + technology +
        '&budget=' + budget +
        '&details=' + details;          

   alert(dataString);


   $.ajax({  
   type: "POST",  
   url: "<?php bloginfo('template_url'); ?>/qoute-sent.php",  
   data: {name:'anas', Email: '[email protected]'},
   datatype: "html",  
   success: function() {  
   alert(sent);
   } 

all the code work till alert(dataString); but after that ajax does not works....

this is my php file code

    $name=$_POST['name'];
    $email=$_POST['email'];
   // $contact=$_POST['contact'];
   //$technology=$_POST['technology'];
  // $budget=$_POST['budget'];
  //$details=$_POST['details'];

   //-------------for email setup----------------------------
   $to = "[email protected]";
   //------------------------------------------
   $message = " \n " .

   "Name ".$name." \n " .
   "Email : ".$email." \n " ;

    //---------------------------------- 
    $subject = "MWM Qoute Request"; 
    $headers =  'From: '.$email . "\r\n" .
    'Reply-To: '.$to . "\r\n" .
    'X-Mailer: PHP/' . phpversion();
    //echo "TO : ".$to."<br>";
    //echo "FROM : ".$email."<br>";
    //echo "<br>".$message;
    mail($to, $subject,$message,$headers);
2
  • Not sure datatype: "html" works, can you use $_POST to retrieve data posted in this way? Anyone? Also datatype should be dataType i think. Aug 1, 2013 at 12:43
  • This is a possible solution and while it doesn't use any "code" it does offer a lot of flexibility. I would use a plugin called Contact Form 7. I know this doesn't really answer your question and you may just want to learn to program within php which I totally understand but if you ever need a quick contact form I would really recommend Contact Form 7 Aug 1, 2013 at 12:54

2 Answers 2

0

WordPress provides functionality and hooks to develop with AJAX to make it really easy. See the Codex.

You should also check out wp_mail for sending emails.

Remember that WordPress loads jQuery in noconflict mode, so your $ selector's should be 'jQuery'.

To prevent abuse you should consider adding a nonce to your script. WordPress handles that too :) See wp_create_nonce.

I think this is a good reference article once you have the bones in places

0
url: "<?php bloginfo('template_url'); ?>/qoute-sent.php", 

Should the URL be "quote-sent.php"? Just checking.

Also, have you set up the AJAX actions for WP? It won't work without them. I'd generally do something like:

<?php
    function my_send_email(){
        doStuff(); // etc..
        echo 'sent!';
        die();
    }
    add_action('wp_ajax_my_send_email', 'my_send_email');
    add_action('wp_ajax_nopriv_my_send_email', 'my_send_email');
?>

Like Mark said, I'd also recommend using wp_mail.

1
  • Good! Was worried it was a typo. But the 2nd part... do you have AJAX actions in place? Aug 2, 2013 at 13:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.