Renzson Ballesteros Renzson Ballesteros - 3 years ago 43
HTML Question

AJAX - Cant Display the parameter given by ajax using PHP in the same page

Can't echo the parameter given by ajax on the same page.

<?php echo $_GET['category_id']?>
$('.select_item_category').click(function(){
var category_id = $(this).attr("category-id");
$.ajax({
url: window.location.href,
data: {
category_id : category_id
},
type: "GET"
});

Answer Source

Please note ajax is client side, so you can't use php the way you are expecting result, what you can do is as follows

$('.select_item_category').click(function(){  
var category_id = $(this).attr("category-id"); 
   $.ajax({
          url: window.location.href,

          data: {
                category_id : category_id
          },

         type: "GET",

         /* Here is response you receive from server */
         success: function(response){
             $('#some_output_div').html(response);

             /* See contents received */
             alert(response);
         } 
     }) 
});

and on Server Side

<?php
       if(isset($_GET['category_id']))
       {
                /* This is what you sent via ajax */
                $category_value = $_GET['category_id'];

               /* do your stuffs query whatever */

               $some_response = 'This is what you send to browser';

               echo $some_response;
               die();
       }
?>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download