简体   繁体   English

如何将Ajax变量值传递给Codeigniter中的控制器

[英]How to pass ajax variable value to controller in codeigniter

Here i want to pass the 'q' value from ajax to controller function in codeigniter. 在这里我想将aq的'q'值传递给codeigniter中的控制器函数。

ajax code:   
function getdata(str) 
    {
        if (str == "") {
            document.getElementById("yer").innerHTML = "";
            return;
        } else { 
            if (window.XMLHttpRequest) {
                // code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp = new XMLHttpRequest();
            } else {
                // code for IE6, IE5
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlhttp.onreadystatechange = function() {
                if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                    document.getElementById("bus").innerHTML = xmlhttp.responseText;
                }
            }
            xmlhttp.open("GET","<?php echo site_url('money_c/taxcalculator1'); ?>?q="+str,true);
            xmlhttp.send();
               window.location="<?php echo site_url('money_c/taxcalculator'); ?>"

        }
    }
controller:

    function taxcalculator1()
            {
                $bt=$_GET['q'];
                echo $bt;
            }

Here i want to pass the 'q' value from ajax to controller function in codeigniter. 在这里我想将aq的'q'值传递给codeigniter中的控制器函数。

As soon as you have started the Ajax request sending with this: 一旦启动Ajax请求,就发送以下信息:

 xmlhttp.send(); 

You leave the page with this: 您使用以下方法离开页面

 window.location="<?php echo site_url('money_c/taxcalculator'); ?>" 

… which aborts the Ajax request, removes the place you are trying to edit with innerHTML and destroys the JavaScript that would be trying to do that anyway. …会中止Ajax请求,删除您要使用innerHTML编辑的位置,并破坏无论如何都将尝试执行此操作的JavaScript。


If you want to use Ajax then: 如果要使用Ajax,则:

  • Put the data you want to show to the user the response from taxcalculator1 将要显示给用户的数据放到taxcalculator1的响应中
  • Use onreadystatechange to show it to the user 使用onreadystatechange将其显示给用户
  • Don't leave the page before that happens (remove the window.location line). 在此之前不要离开页面 (删除window.location行)。

If you want to load an entirely new page: 如果要加载一个全新的页面:

  • Don't use Ajax 不要使用Ajax
  • Just submit a form to a URL 只需将表单提交到URL
  • Display the data you want the user to see (in the form of an HTML document) in the response to that request 在该请求的响应中显示您希望用户查看的数据(以HTML文档的形式)

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

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