首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我们能把链接发到邮局吗?

我们能把链接发到邮局吗?
EN

Stack Overflow用户
提问于 2016-02-23 12:54:40
回答 3查看 252关注 0票数 0

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

现在,当我单击该链接时,我希望将该数据以POST方式发送给我的控制器。这有可能吗?

这是我的下课:

代码语言:javascript
复制
<ul class="dropdown-menu">
    <li><a href="#">
    <?php
    foreach($sites as $site) {
        echo "<li>".$site->site_key."</li>";
    }
    ?>
    </a></li>
</ul>

经修订的守则:

代码语言:javascript
复制
 <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>

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-02-24 07:23:24

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

  1. 他希望通过单击将变量发送给控制器(我使用Jquery)

这是我的演示基于你的问题

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

所以我改变了你的html,让它变得简单

代码语言:javascript
复制
<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

代码语言:javascript
复制
$( ".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,该变量具有要发送到控制器欢迎/发布的名称值。

那么这就是控制器

代码语言:javascript
复制
<?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,只需在演示中评论我的js就可以这样

代码语言:javascript
复制
   $( ".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在回答到代码2。在演示i pust控制器在js第3部分,请参阅js文件演示数据:"value=“+值,

票数 1
EN

Stack Overflow用户

发布于 2016-02-23 12:59:31

您可以重写链接的通常行为,而不是在单击时发送一个帖子,然后只需提供链接的href作为参数。

发送帖子的最简单方法可能是将隐藏的表单设置为POST,然后按编程设置表单的字段,然后当有人单击链接时提交。

示例

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

代码语言:javascript
复制
<! 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
EN

Stack Overflow用户

发布于 2016-02-23 13:06:29

选项1:创建一个表单,通过post提交数据

代码语言:javascript
复制
<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>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35578176

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档