0

I am trying to convert a javascript variable to a PHP variable with ajax. On paper, ajax tells me there aren't any errors, but php doesn't recognize the REQUEST['name_here'], and every time I try to console.log the data, it prints lines of whitespace, and a big chunk of my code as text, along with a 0. Has anyone ever come accross an issue like this before?

// ajax action
add_action( 'wp_ajax_Print_Roster123_function', 'Print_Roster123_function' );

code here:

<script src="https://code.jquery.com/jquery-3.6.4.js" integrity="sha256-a9jBBRygX1Bh5lt8GZjXDzyOB+bWve9EiO7tROUtj/E=" crossorigin="anonymous"></script>
<?php

function Print_Roster123_function() {
    global $wpdb;
  ?>
  
 <script>
 

        jQuery(document).ready(function($) {
           $("#Change1").change(function() {
               // This toggles the select tag
        var Class_Initial = $(this).find("option:selected").text();
        
        
        $.ajax({
           url: ajaxurl,
           // action is the name of the function in final_cart
           data: { 
                 'Class_Initial' : Class_Initial,
                'action' : 'Print_Roster123_function'

           },
           success: function(data) {
               // prints out many lines of whitespace, along with all of my code and 0
               console.log(data);
           },
        error: function(errorThrown){
            console.log(errorThrown);
        }
        });

    });
});
</script>
  
  <?php
    
    if ( isset($_REQUEST) ) {
                // Php doesn't recognize 'Class_Initial'. If I try echoing out $New_Var, nothing shows up
                $New_Var = $_REQUEST['Class_Initial'];
    }
        ?>
1
  • Please check carefully my earlier answer on your question on your Ajax calls instead of repeating the same or almost same Ajax issue again and again. You are not getting correct response on the call, until you carefully learn how to make an Ajax call properly in WP: wordpress.stackexchange.com/a/415701/231952
    – Zollie
    May 1 at 17:54

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.