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'];
}
?>