简体   繁体   中英

ECHO post data from AJAX in PHP

I want to echo the post data i get using AJAX-JQUERY.

I have a dropdown in that dropdown i show some values from database. Now the item i select frmo dropdown is sent as POST to my controller.

If i open my network tab in browser, i can see the POST data i get using my AJAX script.

Like this: 响应

But if i try to echo it shows me nothing on browser.

I am using this script:

<script type="text/javascript">

$( ".specialLink" ).click(function() {
      var site = this.id;
      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){ 

          },

      });
  });

The DROPDOWN:

<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>

THE PHP SCRIPT

var_dump($_REQUEST);
var_dump($_POST);

But if i try to echo it shows me nothing on browser

That is because you are making the request using Ajax instead of just loading it in the main window.

If you want to load the content in the main window, then just submit a regular form. If you must use JavaScript, then you can generate the form using DOM methods, add it to the page, then call its submit() method.

(That said, you seem to be trying to simulate links, which would make a GET request more appropriate than a POST request and allow you to just use a regular link (or set location.href = ... if you must use JS, which you really shouldn't need to for this). If you want to use Ajax then you need to change this:

 success:function(data){ }, 

… so that you actually do something with the value of data (such as add it to the DOM of the current page).

Please send data in this form in ajax call

data : {"site" : site}; 

key should be the string and then it's value in js variable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM