简体   繁体   English

ECHO在PHP中发布来自AJAX的数据

[英]ECHO post data from AJAX in PHP

I want to echo the post data i get using AJAX-JQUERY. 我想回应使用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. 现在我选择frmo下拉列表的项目作为POST发送到我的控制器。

If i open my network tab in browser, i can see the POST data i get using my AJAX script. 如果我在浏览器中打开我的网络选项卡,我可以看到我使用我的AJAX脚本获得的POST数据。

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: 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 PHP脚本

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. 这是因为您使用Ajax发出请求而不是仅在主窗口中加载它。

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. 如果必须使用JavaScript,则可以使用DOM方法生成表单,将其添加到页面,然后调用其submit()方法。

(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: (也就是说,你似乎试图模拟链接,这会使一个GET请求比POST请求更合适,并允许你只使用常规链接(或设置location.href = ...如果你必须使用JS,你真的不需要这样做。)如果你想使用Ajax,那么你需要改变这个:

 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). ...以便您实际使用data的价值(例如将其添加到当前页面的DOM)。

Please send data in this form in ajax call 请在ajax调用中以此表单发送数据

data : {"site" : site}; 

key should be the string and then it's value in js variable. key应该是字符串,然后是js变量中的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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