Monday, October 31, 2022
HomeWordPress Developmentwpdb - Translating wordpress foreach to ajax

wpdb – Translating wordpress foreach to ajax


Im having problem translating a foreach loop in wordpress to ajax.

The foreach loop in wordpress (working appropriately):

 $question = $wpdb->get_results("SELECT first_name, last_name FROM college students");

            
            foreach($question as $pupil)
            {
                echo '<tr>';
                    echo '<td>';
                    echo '<enter kind="checkbox" identify="progress" id="progress1" worth="1" tabIndex="1" onClick="ckChange(this)">';
                                echo '<td>' . $student->first_name . '</td>';
                                echo '<td>' . $student->last_name . '</td>';
                echo '</tr>';
                
            }
        echo '</div>';

The fetchData perform:

perform fetchData(){
  world $wpdb;
    $first_name = $_POST['first_name']; // worth from the ajax
    $last_name = $_POST['last_name'];

        // Now we wish to JSON encode these values to ship them to $.ajax success.
        $outcome = $wpdb->get_results("SELECT first_name, last_name FROM college students");
          echo "<possibility worth="".$first_name."" >".$first_name."</possibility>"; //This line is returned in ajax. it is a check code
         foreach($question as $pupil)
            {
                echo '<tr>';
                    echo '<td>';
                    echo '<enter kind="checkbox" identify="progress" id="progress1" worth="1" tabIndex="1" onClick="ckChange(this)">';
                                echo '<td>' . $student->first_name . '</td>';
                                echo '<td>' . $student->last_name . '</td>';
                echo '</tr>';
            }


        die();
    }

add_action('wp_ajax_fetchData', 'fetchData');
add_action('wp_ajax_nopriv_fetchData', 'fetchData');

The ajax name:

echo '<script kind="textual content/javascript">
jQuery.ajax({
    kind: "POST",
    url: "/wp-admin/admin-ajax.php",
    information: {
         "first_name" : first_name,
         "last_name" : last_name,
         "motion": fetch_Data
    },
    success : perform (information) {
             console.log(information);
             jQuery("#outcomes").html(information);
            }
  });
  
</script>';

The < div > after I name the every little thing

echo '<div id="outcomes" worth="fetchData"></div>';

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments