Home » Php » php – Passing search entry to a div on the page through a WordPress plugin

php – Passing search entry to a div on the page through a WordPress plugin

Posted by: admin February 25, 2020 Leave a comment

Questions:

We’re building a WordPress plugin that uses shortcodes to generate page content. On those pages we have a search field and need to pass the submission from that search box to a div later in the page.

Image of search box

Our developer is attempting to use AJAX but can’t manage to pass the submission to AJAX.

Here’s our code so far:

    function search_filter_datalist($atts) {

    wp_register_script( 'search_filter_dropdown', PRDB_ROOT_URL . 'scripts/form_submist_ajax.js', array( 'jquery' ), 1.0, false );
    wp_enqueue_script( 'search_filter_dropdown' );

    global $examplecedb;

    $person_list = $examplecedb->get_results($examplecedb->prepare('select entity_id, person_name from v_person_name_role order by person_name'));

    $Return_string = '<h1>';
    $Return_string .= '<form class="wordpress-ajax-form" id="person-selection" method="post" action="<?php echo admin_url(\'admin-ajax.php\'); ?>">';
    $Return_string .= '<label for="person-choice">Choose a person:</label>';
    $Return_string .= '<input type="text" placeholder="Search.." list="person-list" id="person-choice" name="person-choice" />';
    $Return_string .= '<datalist id="person-list">';

    foreach ($person_list as $person) {

        $Return_string .= '<option><data value="';
        $Return_string .= $person->entity_id;
        $Return_string .= '">';
        $Return_string .= $person->person_name;
        $Return_string .= '</data></option>';

    }

    $Return_string .= '</datalist>';
    $Return_string .= '<input type="hidden" name="action" value="submitpersonId">';
    $Return_string .= '<?php wp_nonce_field( \'custom_action_nonce\', \'name_of_nonce_field\' ); ?>';
    $Return_string .= '<button>Send</button>';
    $Return_string .= '</form>';
    $Return_string .= '</h1>';

    $Return_string .= '<br>';

    $Return_string .= '<div id="person-data-results">';
    $Return_string .= '</div>';


    return $Return_string;

}

We know this function is failing on the line with admin_url(\’admin-ajax.php\’);

Any ideas on how to pass the search submission to a div on the page?

How to&Answers: