我们可以在POST中获得链接吗?

时间:2016-02-23 12:54:40

标签: php codeigniter

我的网站上有一个引导程序导航栏,其中一个下拉列表中填充了数据库中的一些数据。

现在,当我点击该链接时,我想将POST中的数据发送给我的控制器。这可能吗?

这是我的下拉列表:

<ul class="dropdown-menu">
    <li><a href="#">
    <?php
    foreach($sites as $site) {
        echo "<li>".$site->site_key."</li>";
    }
    ?>
    </a></li>
</ul>

更新代码:

 <form id="hiddenForm" method="post"   style="display: none">
          <input name="linkAddress" value="<?php echo $site->site_key; ?>">
          <input name="otherData" value="">
    </form>


<script>
      var specialLinks = document.getElementsByClassName("specialLink");
        var hiddenForm = document.getElementById('hiddenForm');


        Array.prototype.forEach.call(specialLinks, function(link) {
        link.onclick = function(event) {
            event.preventDefault();
            var req = new XMLHttpRequest();

            req.addEventListener('load', function() {
                var response = req.responseText;

                //Do something with response, like change a part of the page
            });

            //Set the page to send the request to
            req.open('POST', 'recieve.php');
            //Specify that we're using URL-encoded coded parameters
            req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
            //Send the URL-encoded POST data.
            req.send('linkAddress=' + link.href + '&otherData=' + someOtherData());
        }

    </script>

Error

3 个答案:

答案 0 :(得分:1)

您可以覆盖链接的常规行为,而不是在点击时发送POST,然后只提供链接的href作为参数。

发送POST的最简单方法可能是设置为POST的隐藏表单,只需以编程方式设置表单的字段,然后在有人点击链接时提交。

实施例

我写了一个示例页面,它使用两种变体来发送请求:

<! DOCTYPE HTML> 
<html>
    <head>
        <title>Post Send Example</title>
    </head>
    <body>
        <!-- Place somewhere on your page if you're using the hidden form method:
        The display needs to be none so the form doesn't show.
        Ideally, this won't use inline styling, obviously.-->
        <form id="hiddenForm" method="post" action="receive.php" style="display: none">
            <input name="linkAddress" value="">
            <input name="otherData" value="">
        </form>
        <!-- You could also programatically create the form using Javascript if you want -->

        <a class="specialLink" href="www.someaddress.com">A Link!</a>
        <a class="specialLink" href="www.someaddress.com/someSubPage">Another Link!</a>

        <script>
            var specialLinks = document.getElementsByClassName("specialLink");
            var hiddenForm = document.getElementById('hiddenForm');

            //If using a form, grab all links, and set the click handlers to fill
            // and submit the hidden form
            Array.prototype.forEach.call(specialLinks, function(link) {
                link.onclick = function(event) {
                    //Prevent the link from causing navigation. 
                    event.preventDefault();
                    //Grab the link and other field
                    var linkAddress = document.querySelector('#hiddenForm [name=linkAddress]');
                    var otherDataField = document.querySelector('#hiddenForm [name=otherData]');

                    //Set the form field to the link's address
                    linkAddress.value = link.href;

                    otherDataField.value = someOtherData();

                    hiddenForm.submit();
                }
            });

            //Or via AJAX, grab all links again, but create a AJAX request instead
            Array.prototype.forEach.call(specialLinks, function(link) {
            link.onclick = function(event) {
                event.preventDefault();
                var req = new XMLHttpRequest();

                req.addEventListener('load', function() {
                    var response = req.responseText;

                    //Do something with response, like change a part of the page
                });

                //Set the page to send the request to
                req.open('POST', 'recieve.php');
                //Specify that we're using URL-encoded coded parameters
                req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
                //Send the URL-encoded POST data.
                req.send('linkAddress=' + link.href + '&otherData=' + someOtherData());
            }
        </script>

    </body>
</html>

请记住,我一个月前才学会了如何使用这个表单,而且这种方式只在几天前使用了AJAX。虽然我知道这有效,但我无法说出最佳做法。另请注意,为简洁起见,我在此处没有进行任何错误处理。实际上,您应该检查AJAX请求的响应代码,以确保它正确完成。

答案 1 :(得分:1)

经过长时间的讨论,我得到了他想要的东西

  1. 他想通过点击(我使用Jquery)
  2. 将变量发送到控制器

    所以这是根据您的问题我的demo

    请注意,在该演示中,您需要最新的jquery,请参阅外部js

    所以我改变你的html以使其变得简单

    <ul class="dropdown-menu">
            <a href="#" class="specialLink" id="54th-65hy">
                <li>54th-65hy</li>
              </a>
    
           <a href="#" class="specialLink" id="HT45-YT6T">
                <li>HT45-YT6T</li>              
              </a>
      </ul>
    

    请参阅我将您的值设为ID

    这是我的js

    $( ".specialLink" ).click(function() {
          var value = this.id; //get value for throw to controller
        alert(value);  
    
      $.ajax({
             type: "POST", //send with post
             url: "<?php echo site_url('welcome/post) ?>", //for example of mine , using your controller
           data: "value=" + value, //assign the var here 
             success: function(msg){
                alert(msg);
             }
        });
    });
    

    查看id我保存到名称值的变量中以发送到控制器欢迎/发布

    然后这是控制器

    <?php
    defined('BASEPATH') OR exit('No direct script access allowed');
    
    class Welcome extends CI_Controller {
    
        public function index()
        {
    
        }
    
        public function post()
        {
            $value=$this->input->post('value');
        echo $value;
        }
    
      }
    

    让你知道ajax,只需在demo中评论我的js就像这样

       $( ".specialLink" ).click(function() {
          var value = this.id; //get value for throw to controller
        alert(value);  
    
      $.ajax({
             type: "POST", //send with post
             url: "<?php echo site_url('welcome/post) ?>", //for example of mine , using your controller
           data: "value=" + value, //assign the var here 
             success: function(msg){
                alert(msg);
             }
        });
    });
    

    编辑:  1.让你的演示成为首先显示价值的你知道它只是将js复制并粘贴到codepen中  2.在演示中我在ps部分调用控制器  3.请参阅js文件演示数据:“value =”+ value,

答案 2 :(得分:0)

选项1:创建表单以通过帖子提交数据

<ul class="dropdown-menu">
    <li><a href="#" onclick="form.submit();">
      <?php

      foreach($sites as $site) {
        echo "<form action='index.php' method='POST'>";
        echo "<input type='hidden' name='attribute' value='1'>";
        echo "<li>".$site->site_key."</li>";
        echo "</form>";
      }


      ?>
    </a></li>

</ul>