0

I'm propably going to need to use some form of Ajax here. But i'm pretty new to the Ajax usage withing Wordpress.

It is about the following. I have a Wordpress contact form that uses Bootstrap Validator and some Javascript to have a nice feel to it. I used the contact from from codepen and i made a Fork with my adjustments :

Since i want to share the whole code around this i link the Fork with the functions and everything :

My Fork on the contact form

The thing is that the contact form works fine but i can not figure out how to use wp_mail() in this to send a mail to the administrator since i use javascript and not PHP.

Hope you guys can help me out with this one.

UPDATE

The ajax works! Thanks for the documentation. Now i seem to have another problem though. So i have certain input fields 'first_name' etc etc. But those are not reflecting in my email. Its all blank.

    function my_user_mailsend() {
    $message= $_POST['comment'];
    $contactname= $_POST['first_name'];
    $contactlastname= $_POST['last_name'];
    $contactemail = $_POST['email'];
    $headers = 'From: ' . $contactname . ' ' . $contactlastname . ' <' .$contactemail . '>' ;
    $to = get_option( 'admin_email' );

wp_mail( $to, $message, $headers );
    wp_die();
}

function my_nonuser_mailsend() {

    $contactname= $_POST['first_name'];
    $contactlastname= $_POST['last_name'];
    $contactemail = $_POST['email'];

    $headers = 'From: ' . $contactname . ' ' . $contactlastname . ' <' .$contactemail . '>' ;
    $to = get_option( 'admin_email' );
    $message= $_POST['comment'];

    wp_mail( $to, $message, $headers );
    wp_die();
}

closed as off-topic by Howdy_McGee Apr 20 '17 at 14:26

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

1

Without php you can't send an email. So as per your reference you have to use ajax and php.

In wordpress ajax is little bit different as compared to normal ajax. You can check the link for how to use ajax in wordpress.

https://www.smashingmagazine.com/2011/10/how-to-use-ajax-in-wordpress/

https://premium.wpmudev.org/blog/using-ajax-with-wordpress/?utm_expid=3606929-101.TxEXoCfwS1KxJG1JVvj_5Q.0&utm_referrer=https%3A%2F%2Fwww.google.co.in%2F

https://www.sitepoint.com/how-to-use-ajax-in-wordpress-a-real-world-example/

Once you call the action inside ajax, in that action you can write your wp_mail.

Let me know if still you are not getting this.

  • I'm going to read all of this and try to figure it out :D Thanks for the documentation already. – CompactCode Apr 20 '17 at 12:50
  • I did all the research and i got it to work. Thanks! Now i seem to have another problem as my $_post are not working – CompactCode Apr 20 '17 at 14:23
  • regarding that you can ask question in another post – MagniGeeks Technologies Apr 21 '17 at 13:30
  • Got that also to work. I didn't parse the data trough ajax. Only the question to fire the function. – CompactCode Apr 21 '17 at 20:28

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