-1

I've created an AJAX request to send data to my plugin which will then submit a job application. When submitting the form the page that it is returning is blank other than a 0 and the console is showing a 400 error for admin-ajax.php.

This code has now been updated from Plamen's suggestions, I realise the $data variable now may not be doing anything

The action of my form is below:

<form method="POST" id="job-application-form" action="<?php echo admin_url( 'admin-ajax.php' ) ?>" class="flex lg:flex-row flex-col flex-wrap">

This is the code on my form page:

<script type="text/javascript">
    jQuery(function($) {
        $('#job-application-form').on('submit', function(e) {
            var form = $(this);
            var data = form.serialize();
            
            $.post(form.attr('action'), data, function(response) {
                console.log(form.attr('action'));
                alert('Got this from the server: ' + response);
            });
            
            e.preventDefault();

            return false;
        });
    });
</script>

And this is the code on my plugin function:

<?php

    add_action( 'wp_ajax_submit_post', 'submit_post' );
    add_action( 'wp_ajax_nopriv_submit_post', 'submit_post' );

    function submit_post() { 

        $actual_link = parse_url('http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']);
        $parts = explode('/', $actual_link['path']);
        $job_id = $parts[count($parts)-1];

        $curl = curl_init();

        $firstName = $_POST['firstName'];
        $lastName = $_POST['lastName'];
        $email = $_POST['email'];
        $phone = $_POST['phone'];
        $postcode = $_POST['postcode'];
        // $info = $_GET['info'];

        $data = array(
            'firstName' => $firstName,
            'lastName'  => $lastName,
            'email'     => $email,
            'phone'     => $phone,
            'address'   => $postcode,
        );
        $payload = json_encode($data);

        var_dump($job_id);
        var_dump($payload);

        $token_url = "/home/.token";
        $file = fopen($token_url, "r") or die("Unable to open job ads file!");
        $access_token_new = fread($file,filesize($token_url));

        curl_setopt_array($curl, array(
            CURLOPT_URL => The API URL . $job_id .'/applications',
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'POST',
            CURLOPT_POSTFIELDS => $payload,
            CURLOPT_HTTPHEADER => array(
                'Authorization: Bearer ' . $access_token_new,
                'Content-Type: application/json',
            ),
        ));

        fclose($file);

        $response = curl_exec($curl);

        curl_close($curl);

        var_dump($response);

        wp_die();
    }

?>

From what I've gathered my ajax request cannot find my plugin function but I'm not sure if I've messed up the naming or what's going on.

2

1 Answer 1

1

You need to pass the payload of the HTTP POST request in a compatible format.

The simplest would be to change your code to the following:

Add a hidden field named "action" with as follows:

<form method="POST" id="job-application-form" action="<?php echo admin_url( 'admin-ajax.php' ) ?>" class="flex lg:flex-row flex-col flex-wrap">
  ...
  <input type="hidden" name="action" value="submit_post">
</form>

Then change your JavaScript implementation like this, no need to get the Ajax URL with PHP, you have it in the <form> action attribute:

jQuery(function($) {
  $('#job-application-form').on('submit', function(e) {
    var form = $(this);
    var data = form.serialize();
      
    $.post(form.attr('action'), data, function(response) {
       console.log(form.attr('action'));
       alert('Got this from the server: ' + response);
    });
    
    e.preventDefault();

    return false;
  });
});
11
  • Hi Plamen, thanks for this but it's still giving me the same error message, I can see the payload is fine and it was being encoded in my php function but still getting the same 400 error Commented Oct 27, 2022 at 16:32
  • 1
    Can you update your question with details how is your PHP end-point function being resisted, in what file, and what hook. Commented Oct 27, 2022 at 17:08
  • 1
    But how is this PHP code being included in your WordPress setup? Commented Oct 27, 2022 at 17:24
  • 1
    You need to add the add_action( 'wp_ajax_submit_post', 'submit_post' ); and add_action( 'wp_ajax_nopriv_submit_post', 'submit_post' ); code to either your plugin init or theme functions.php script. Commented Oct 27, 2022 at 18:12
  • 1
    It seems that the 500 error may be coming from the CURL request... so I guess this would be a new question. Commented Oct 28, 2022 at 8:48

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