1

I figured out how to successfully pass along a variable in Ajax. I can send responses and output text on the fly. However, I am having a little difficulty with passing along the Ajax data and storing them into my php variable. I am trying to use XML, but do not know how to reference which file I am using, as I am using Wordpress's Cpanel.

Here is my ajax code:

 $.ajax({
           url: ajaxurl,
           // action is the name of the function in final_cart
           data: { 
                'test': test,
                 'Class_Initial' : Class_Initial,
                "action" : "new_ajax_request",

           },
// THIS IS WHERE I NEED HELP
           success: function(data) {
               $("#game1").html(Class_Initial);
               
               var ajax = new XMLHttpRequest();

               // Do I use the url ajaxurl I listed? Or is there some file in my file manager on cpanel I need to use?
               ajax.open("POST", ajaxurl);
               ajax.send("Class_Initial="+Class_Initial);
           },
        error: function(errorThrown){
            console.log(errorThrown);
        }
        });

On my PHP I would then use

<?php
$Ajax_Result = $_POST['Class_Initial'];
echo $Ajax_Result;
?>
7
  • Why use XMLHttpRequest when you're already using a wrapper that's easier to use for AJAX ( $.ajax ), XMLHttpRequest isn't specific to XML, it's just the name of an old API. Modern JS uses functions such as fetch. On top of that it's happening inside the success function of a jQuery AJAX request, this doesn't make sense as it would create a second AJAX request that does the same thing as the first one. As I mentioned in your previous question, you can't set a PHP variable in one AJAX call, then use it in another, PHP doesn't remember variables from previous requests
    – Tom J Nowell
    May 3, 2023 at 19:02
  • @TomJNowell, is there any way at all to take a selection from a dropdown and asynchronously take the value in javascript and convert it to a php variable, and output it on the screen? I am able to see the results in the Network section when I Inspect the page
    – John Lyons
    May 3, 2023 at 19:13
  • If its impossible to take the values inside of the jQuery Ajax request, is there any way to at least take the values from the Network page and output the values to a php variable?
    – John Lyons
    May 3, 2023 at 19:15
  • that doesn't make sense either, PHP runs on the server before the JS, they're separate languages that run in separate places. What are you trying to build with drop downs? If you explain that, then people can help tell you how that would work. Also would it be easier to take the value from the AJAX response ( data ) and use it to modify the page with javascript?
    – Tom J Nowell
    May 4, 2023 at 8:07
  • I am trying to take the value of the dropdown selection, and insert it into a query whose results are equal to the selection. Ie: SELECT a FROM b WHERE a = 'Selected dropdown value'
    – John Lyons
    May 4, 2023 at 15:27

0

Your Answer

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

Browse other questions tagged or ask your own question.