Rajan
Rajan

Reputation: 2425

How to get data sent from AJAX POST in PHP?

I am trying to get POST data using AJAX & JQUERY

I have a bootstrap dropdown in which i show some values from database. I want to pass the item i selected as POST to my PHP.

My dropdown is like this:

<li class="dropdown">
  <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Your Sites <span class="caret"></span></a>
  <ul class="dropdown-menu">
    <?php
      foreach($sites as $site)
      {
        echo "<li class='specialLink' id='".$site->site_key."'><a href='#'>".$site->site_key."</a></li>";
      }
    ?>
  </ul>
</li>

Now the my problem is when i echo the POST Values in PHP i get nothing, while if i alert the data from ajax then it shows me the correct value.

Also if i open my browser console in can see the values i select in Network->XHR tab.

<script type="text/javascript">

$( ".specialLink" ).click(function() {
    var site = this.id;
    console.log(site);
    var url= "<?php echo base_url('customer/dashboard/index') ?>"; 

     //get value for throw to controller

    $.ajax({ 
        type: "POST", //send with post 
        url: "<?php echo base_url('customer/dashboard/index') ?>", 
        data: {site:site}, 
        success:function(data){ 

        },

    });
});

Trying to get Values like this in PHP:

 if(!empty($_POST))
    {
       //$site = $_POST['site'];

        echo $this->input->post('site');
       //$this->session->set_userdata('site', $site);
    }

In Console i can see the item selected like this: image

Request URL:http://127.0.0.1/bizrtc/customer/dashboard/index
Request Method:POST
Status Code:200 OK
Form Data
view source
view URL encoded
site:HT45-YT6T

Upvotes: 1

Views: 4758

Answers (2)

danielpm
danielpm

Reputation: 125

Try create a simple div:

<div id="test"></div>

Do a echo json_encode

if($_REQUEST['function'] == 'function_site')
    {
       $site = $_POST['site'];
       //$this->session->set_userdata('site', $site);
       $response['site'] = $site;

       echo json_encode($response);

    }

and append your ajax result to him:

$.ajax({ 
        type: "POST", //send with post 
        url: "<?php echo base_url('customer/dashboard/index') ?>", 
        data: {site:site, function:function_site},
        dataType: 'json',
        success:function(data){ 
           $("#test").html(data.site);
        },

    });

I dont have an enviroment to test for you right now, but You should see what you are echoing now.

Upvotes: 1

Ketan Solanki
Ketan Solanki

Reputation: 291

Try to change your drop down code like this ,see if it works:

<li class="dropdown">
  <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Your Sites <span class="caret"></span></a>
  <ul class="dropdown-menu">
    <?php
      foreach($sites as $site)
      {
        echo "<li class='specialLink' data-id='".$site->site_key."' id='".$site->site_key."'><a href='#'>".$site->site_key."</a></li>";
      }
    ?>
  </ul>
</li>

And Your JS code to :

<script type="text/javascript">

    $( ".specialLink" ).click(function() {
//        var site = this.id;
          var site = $(this).attr('data-id').val; 
        console.log(site);
        var url= "<?php echo base_url('customer/dashboard/index') ?>"; 

         //get value for throw to controller

        $.ajax({ 
            type: "POST", //send with post 
            url: "<?php echo base_url('customer/dashboard/index') ?>", 
            data: {site:site}, 
            success:function(data){ 

            },

        });
    });

Upvotes: 2

Related Questions