【问题标题】:unable to send parameter through ajax to a page itself无法通过 ajax 将参数发送到页面本身
【发布时间】:2013-05-15 19:48:30
【问题描述】:

我在 keyup 事件上向页面本身发送参数。

我通过像url:"http://localhost/application/views/pages/users/tags.php?tagfilter=lif",这样的ajax发送一个参数

并在 javascript 函数中获取其值 ('/instruction/showtags/<?php if( isset($_GET['tagfilter']) && $_GET['tagfilter'] == "") {echo $_GET['tagfilter'];} ?> ', {

我检查了我的控制台,它没有显示错误和警告。 但我确信它要么没有发送参数,要么可能没有获取参数。

请告诉我,为什么我的参数没有发送? ajax代码的写法是否正确?

如果你不能理解我的问题,请告诉我,我会尝试用其他方式解释。

完整源代码:

tags.php

 <input placeholder="Search" id="tagfilter" name="tagfilter" type="text"/>

jquery/ajax 函数

   <script type="text/javascript"> 
      $(document).ready(function() {    
      $('#tagfilter').keyup(function(e){
       // alert("called"); 
        $.ajax({
            url:"http://localhost/application/views/pages/users/tags.php?   
       tagfilter=lif",
            type:"get",  
            success: function(){ 

            } 
        });
    });
 });     
</script>

我的无限滚动js文件,包括在同一页面中

 <script>
         (function($) { 
                $.fn.scrollPagination = function(options) {

                        var settings = { 
                                nop     : 10, // The number of posts per scroll to be loaded
                                offset  : 0, // Initial offset, begins at 0 in this case
                                error   : 'No More Data To Display!', // When the user reaches the end this is the message that is
                                                            // displayed. You can change this if you want.
                                delay   : 500, // When you scroll down the posts will load after a delayed amount of time.
                                               // This is mainly for usability concerns. You can alter this as you see fit
                                scroll  : true // The main bit, if set to false posts will not load as the user scrolls. 
                                               // but will still load if the user clicks.
                        }

                         .............................
                         .............................
                         .............................

更新:

我到底想做什么?


我已经用 jquery 实现了无限滚动。当我的页面加载时,它会调用我的控制器,并从控制器中获取数据并将其显示在我的视图页面中。

现在,我打算在此页面上无限滚动搜索。 我有一个文本框,我正在尝试从这个文本框中搜索 keyup 事件。

我正在尝试这种逻辑。如果您有更好的逻辑,请与我分享,因为我长期以来一直在努力实现这一点。


【问题讨论】:

  • 在你的代码顶部,不应该是$_GET['tagfilter'] != ""吗? (注!=
  • @JerrySeeger 我听不懂你的话。请以其他方式告诉它。谢谢
  • 只是好奇:您是否对your other account 提出您自己的问题?
  • if( isset($_GET['tagfilter']) &amp;&amp; $_GET['tagfilter'] == "") 应该是if( isset($_GET['tagfilter']) &amp;&amp; $_GET['tagfilter'] != "")
  • 当您可以使用分段时,为什么要在 Ci 中发送 $_GET。我认为您在实施您的想法时遇到了问题。请详细描述您的想法,以便我们提供更多帮助

标签: php javascript jquery ajax codeigniter


【解决方案1】:

好的。我不知道你的结构,所以我有一些提示。

首先:您需要知道,当 PHP 是 Server Side 语言时,只要 JS 是 Client Side 语言,javascript 的处理速度要比 PHP 快得多。所以尝试将你的 php 数据保存在 javascript 变量中,如下所示:

var seg3 = "<?php echo $this->uri->segment(3) ?>";
$.post("/instruction/showtags/" + seg3, {...etc

然后为了您的安全,请尝试检查您是否需要所有 url 而不仅仅是其中的一部分(取决于 CI 配置),以便您的代码可以像这样出现

var seg3 = "<?php echo $this->uri->segment(3) ?>";
var myurl = "<?php echo site_url('casting/send_email'); ?>" + seg3;
$.post(myurl, {...etc

并且一定不要使用$_GET

其次,确保您正在发送片段并且片段存在。您可以随时使用 console.log() 在 javascript 中跟上数据,看看它是否为空。

您是否希望它在 keyup 或 keydown 上或任何相同的。

如果您使用的是 json,请确保将其作为最后一个参数添加到您的 $.post() 方法中,您可以看到它的 jquery api

【讨论】:

    【解决方案2】:

    您检查是否$_GET['tagfilter'] == "",并且仅当字符串为空时才打印它。 所以应该是:

    <?php if( isset($_GET['tagfilter']) && $_GET['tagfilter'] != "") { echo $_GET['tagfilter'];} ?>
    

    【讨论】:

    • 我不确定你是否知道 PHP 是服务器端的,并且只有在加载 javascript 后才会执行,这可能是问题所在。
    • 是的,这就是问题所在。您知道如何使用其他一些技术来完成这项工作吗?
    • 你应该看看 jQuery-ajax-function 来将你的数据传输到服务器并得到答案:api.jquery.com/jQuery.ajax
    • 但是您已经在使用 $.post 也应该可以工作。所以 $_GET['tag..'] 是页面加载时查询字符串中的标签,例如index.php?tag=test
    猜你喜欢
    • 2019-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-11
    相关资源
    最近更新 更多