-1

Once done validating a form with ajax, I use php within my functions.php to actually send the form.

<?php
$name = $_POST['fname'];
$lname = $_POST['lname'];
$subject_line = $_POST['subject'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$captcha = $_POST['captcha'];


if (!empty($name) && !empty($lname) && !empty($email) && !empty($phone) && !empty($subject_line) && !empty($message) ) {

    $to = 'email@domain.com';
    $from = $subject_line;
    $subject = $subject_line;
    $body1 = "First Name: " . $name .
        "\nLast name: " . $lname .
        "\nEmail: " . $email .
        "\nPhone Number: " . $phone .
        "\nMessage: " . $message;
    if (mail($to, $subject, $body1, 'From: ' . $email)) {
        print "Thank you, we will get back to you shortly.";
    }
}

When I add in the php to the functions.php, I get the fatal error Call to undefined add_action() in the theme's functions.php. Any suggestions on what I'm missing?

  • 1
    are you calling functions.php directly or are you using the AJAX API? – Milo Aug 26 '13 at 0:08
  • Calling it directly. – ColoursB Aug 26 '13 at 0:16
  • 1
    Then fix that and use the API. – fuxia Aug 26 '13 at 0:18
  • This answer should help explain the problem with accessing functions.php directly. – s_ha_dum Aug 26 '13 at 0:50
  • Thank you guys, but I'm still unsure of how to implement the AJAX API. What do I type where? – ColoursB Aug 26 '13 at 1:16
0

register an ajax action for submitting email:

Note handle_form is the name of your action in this example

add_action('wp_ajax_handle_form','handle_form_submit_111605' );

also add this in order to make it work for non logged in users

add_action('wp_ajax_nopriv_handle_form','handle_form_submit_111605' );

do validation and mail sending job in the callback function handle_form_submit_111605

In functions.php

function handle_form_submit_111605() {

}

User simply jquey ajax to submit the form through ajax In you js file

<script type="text/jaascript">

var submitdata =  myForm.serialize();
jQuery.ajax({
  url: 'http://domain/wp-admin/admin-ajax.php?action=handle_form',
      //action parameter is the same one you used while registering the ajax
  type: 'POST',
  data: submitData,
  success: function(data, textStatus, xhr) {
     if(data.status){
         // ...
     }
   }
 });
</script>

See more here

  • Hi Dipesh, I'm assuming that I should be adding both add_action() to functions.php, but how does all of that communicate with validating the form? My jQuery is as follows $(document).ready(function(){ $("#loading").hide(); $("#myform").validate({ debug: false, rules: { input: "required" }, messages: { input: "error message" }, submitHandler: function(form) { $("#loading").show(); $.post('<?php echo $our_url . '/functions.php'; ?>', $("#myform").serialize(), function(data) { $('#results').html(data); $("#loading").hide(); }); } }); }); – ColoursB Aug 27 '13 at 2:52
  • Your code says you are making front-end validation, add_action has nothing to do with client side validation, but yes, you can do server-side validation with php in the callback function registered with your ajax action.Besides, the whole idea behind using ajax was to actually submit the form without complete page reload – Dipesh KC Aug 27 '13 at 9:49
  • Can you explain it to me like I'm 5? I don't understand what you're suggesting sorry – ColoursB Aug 28 '13 at 0:57
  • Sure, please tell me what things have you done so far. I guess your form is ready and you have already validated before submitting the form, right?These are front-end or client-side stuffs( because these code run on the users browser). Next you need to submit the form to the server so that the message or whatever reaches to its target receiver. and this is called serverside stuff( because this code runs on the server). You may again need to make sure you have enough data like email and message to prevent errors,so you might need to validate serverside as well. – Dipesh KC Aug 28 '13 at 4:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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