• Resolved Bloke

    (@bloke)


    I need help with this Ajax. I am trying to gather the ID from a select drop down and post it to use on another select.

    My major issue was sending the ID. Becuase I was using WordPress it didn’t know how to send it. Now i know how to do that part. I made a test function to get the Ajax URL working. All it does now is show an alert “Hello World”. I want to remove the line to “Hello World” and replace it with posting ID.

    ajaxurl = '<?php echo admin_url( 'admin-ajax.php'); ?>';

    <script type="text/javascript">
    jQuery(document).ready(function() {
     var id = $("select#category option:selected").attr('value');
         $("select#category").change(function(){
            jQuery.ajax({
                type: 'POST',
                url: ajaxurl,
                data: {"action": "hello"},//Just shows the result of hello function
                success: function(data){alert(data);}//Need to remove
    
            //jQuery.post( ajaxurl ,  { action:"my_action_name",id:"True" } ,function(data){
    //This is what I found I need to work into my script. But not sure of the format.     
    
            });
            return false;
        });
    
    });
    </script>
Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter Bloke

    (@bloke)

    Got this part to work. It sends the post with the ID but I can’t receive it and process it.

    <script type="text/javascript">
    jQuery(document).ready(function()     {
            $("select#category").change(function(){
                  $("select#type").attr("disabled","disabled");
                      $("select#type").html("<option>wait...</option>");
                       jQuery.ajax({
                        type: 'POST',
                        url: ajaxurl,
                        data: {id: $(this).val() },
                        success:function(data){
                               $("select#type").removeAttr("disabled").html(data);
                        }
                    });
            });
      });
    
    </script>

    Thread Starter Bloke

    (@bloke)

    Finally got it working. At least this part of my project. The missing part was the “action”. It was posting the ID but didn’t know where to send it. Then I saw this simple-ajax-example tutorial sending a variable via Ajax and then printing it. I compared what was being done. Viewed the result in Firebug. Then inside a PHP function “example_ajax_request” I retrieved the ID and ran the query inside this function. All I needed was a simple example to figure how it works.

    $("select#category").change(function(){
                  $("select#type").attr("disabled","disabled");
                      $("select#type").html("<option>wait...</option>");
    
                       jQuery.ajax({
                       type: 'POST',
                       url: ajaxurl,
                       data: {action:"example_ajax_request",id: $(this).val() },
                        success:function(data){
                                   $("select#type").removeAttr("disabled");
                                   $("select#type").html(data);
    
                        }
                    });
            });
      });

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Using Ajax to send ID’ is closed to new replies.