I have read through a lot of responses on this site and am unable to find the issue with my code.

The form is :

<?php while (have_posts()) : the_post()?>
        <form  method='post'  id="dav-international" >
            <label for="dav-address">Address:</label><input type="text" name="dav-address" id="dav-address"/>
            <button type="submit" id="submit">Submit</button>
<?php endwhile;?>

The functions are in function.php ( home-page is already enqueued ):

    add_action('wp_enqueue_scripts', 'dav_script');
    function dav_script(){
        if (is_front_page()){
            $dav_nonce = wp_create_nonce('contact_form');
            wp_localize_script('home-page', 'ajaxObj', array(
                    'ajaxurl' => admin_url('admin-ajax.php'),
                    'nonce' => $dav_nonce,

    add_action('wp_ajax_nopriv_dav_validation', 'dav_form_validation');
    add_action('wp_ajax_dav_validation', 'dav_form_validation');

function dav_form_validation(){
    global $wpdb;
    if ($_SERVER["REQUEST_METHOD"] == 'POST'){
    $dav_address = sanitize_text_field($_POST['info']);

    $dav_ajax_data = array();
        $dav_ajax_data['dav-address'] = $dav_address;

    echo json_encode($dav_ajax_data);

and the jQuery is :

var $j = jQuery;
                        var $j = jQuery;
                        if ($j('#dav-address').val().length > 0){
                            var formInfo = $j('form').serialize();
                            var formdata = {
                                action : 'dav_validation',
                                  _ajax_nonce: ajaxObj.nonce,
                                  info : formInfo
                                url: ajaxObj.ajaxurl,
                                data: formdata,
                                success: function(data){

I believe I followed the instructions on WordPress codex properly but the console continues to output 0. I am stuck on this and cannot seem to figure it out.

2 Answers 2


What makes you think this will ever be true?

if (isset($_POST['submit'])){

In function dav_form_validation the die() is in 2 conditionals. It should be called regardless of whether the conditionals are satisfied. Add 2 else clauses and return error messages.

Usually a response of '0', means the ajax hooks are not being found. But your code seems okay in that regard.

  • Hi, thanks for the response. I added the isset ($_POST['submit']) because I was trying things I saw in other posts. When I took it out and moved the die() to the end of the function I am getting an empty array. Would you know why this would occur? Thanks Aug 2, 2015 at 0:51
  • Remove this: if (isset($dav_address)){
    – shanebp
    Aug 2, 2015 at 1:11
  • I did and the console is returning: Aug 2, 2015 at 1:22
  • <b>Notice</b>: Undefined index: dav-address in <b>C:\xampp\htdocs\wordpress\wp-content\themes\Chevalier Transport copy\functions.php</b> on line <b>112</b><br /> {"dav-address":""} Aug 2, 2015 at 1:22
  • I solved it, thanks a lot. I thought something was wrong with my code, but it was the way PHP and ajax worked. Aug 2, 2015 at 13:21

Solved the problem thanks to the guidance of @shanebp: The issue was how the data was being called through PHP and ajax. Instead of using the name attribute in the input field, I used var_dump to see what $_POST was outputting and found that the array created (formdata) in jQuery was what $_POST was retrieving. Instead of using the name attribute, I had to return the info key passed through the formdata array in my ajax. I am guessing this is how wordpress uses ajax and php.

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.