0

I am trying to pass a javascript over to a PHP variable by using AJAX. However, I am having a little bit of difficulty understanding Ajax, as I am new to it.

Here is my code:

    <form method="post">
    
        <select name="Class_Initial">
        <!-- dropdown data here -->
        </select>

    </form>
    
     <script>
            $(document).ready(function() {
               $("#Change1").change(function() {
                   // This toggles the select tag
            var Class_Initial = $(this).find("option:selected").text();
    
            $.ajax({
                type: "POST",
                url: "my_ajax_obj.ajax_url",
                data: Class_Initial = Class_Initial,
                success: function(data) 
                {
                    alert("Success!");
                },
                data: 
                 { 
                     Class_Initial : Class_Initial 
                 }
            });
        });
    });
    </script>
            
    <?php
    // How to tie var Class_Initial equal to $A
    $A = $_POST['Class_Initial'];
    ?>

As a final note, I want to take the value of var Class_Initial and have it equal to a key in a query.

Ie:

$wpdb->query("SELECT X from A Where X = 'Javascript_Value'");
1
  • it's probably not an AJAX issue and more basic variable usage, see this: url: "my_ajax_obj.ajax_url", instead of saying you want the URL to be the value of my_ajax_obj.ajax_url you've told it you want the URL to be literally the name of the variable, not its value: "my_ajax_obj.ajax_url"
    – Tom J Nowell
    Apr 28, 2023 at 17:00

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.