POST from jQuery to PHP

I have a form that passes field values to jQuery, which validates the fields and creates the variables being sent in data: {} in the snippet below.

$.ajax({
    type: "POST",
    url: "<?php bloginfo('stylesheet_directory'); ?>/process.php",
    dataType: "json",
    data: {
        "title":title,
        "goals":goals,
        "progress":progress,
        "categories":categories,
        "tags":tags,
        "video":video,
        "audio":audio,
    },
    success: function() {
        //get permalink for post from php and go to it
    }
});

process.php is not receiving any data, or it’s not posting the data.

Read More

I’m not sure why.

Below is my process.php file:

<?php

    $user_submitted_title = sanitize_text_field($_POST['user_submitted_title']);
    $user_submitted_progress = $_POST['user_submitted_progress'];
    $user_submitted_goals = $_POST['user_submitted_goals'];
    $user_submitted_categories = $_POST['user_submitted_categories'];
    $user_submitted_tags = sanitize_text_field($_POST['user_submitted_tags']);
    $user_submitted_video = sanitize_text_field($_POST['user_submitted_video']);
    $user_submitted_audio = sanitize_text_field($_POST['user_submitted_audio']);

    $user_post = array(
        'comment_status' => 'open',
        'post_author'    => $user_ID,
        'post_category'  => array($user_submitted_categories),
        'post_content'   => '<h2>Project Progress</h2>' . $user_submitted_progress . '<h2>Project Goals</h2>' . $user_submitted_goals,
        'post_status'    => 'publish',
        'post_title'     => $user_submitted_title,
        'post_type'      => 'post',
        'tags_input'     => $user_submitted_tags
    );

    $user_post_id = wp_insert_post($user_post);
    add_post_meta($user_post_id, 'wpcf-video', $user_submitted_video);
    add_post_meta($user_post_id, 'wpcf-audio', $user_submitted_audio);

    $user_post_redirect = get_permalink($user_post_id);

?>

I need to figure out why nothing posts when I use the form and how to send $user_post_redirect back to jQuery so I can redirect to it.

How can I achieve that?

Related posts

1 comment

  1. When you load your process.php file directly, it’s not within the context of the WordPress environment, so no WordPress functions are available. WordPress has a native AJAX API that should be used for this sort of thing.

    First, enqueue your javascript file, then use wp_localize_script to pass the location of admin-ajax.php, which will be processing the requests:

    function wpa_scripts() {
        wp_enqueue_script(
            'wpa_script',
            get_template_directory_uri() . '/js/script.js',
            array('jquery'),
            null,
            true
        );
        $script_data = array(
            'admin_ajax' => admin_url( 'admin-ajax.php' )
        );
        wp_localize_script(
            'wpa_script',
            'wpa_data',
            $script_data
        );
    }
    add_action( 'wp_enqueue_scripts', 'wpa_scripts' );
    

    Next, add an action to your ajax data, which will map to the function hooked to process the request:

    $.ajax({
        type: "POST",
        url: wpa_data.admin_ajax,
        dataType: "json",
        data: {
            "action":"some_action",
        },
        success: function() {
            //get permalink for post from php and go to it
        }
    });
    

    Last, add the action in php and hook it to the function which will receive and process it, then move the contents of process.php inside this function

    add_action( 'wp_ajax_some_action', 'your_process_function' );
    add_action( 'wp_ajax_nopriv_some_action', 'your_process_function' );
    

Comments are closed.