简体   繁体   中英

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.

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.

As soon as you have started the Ajax request sending with this:

 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.


If you want to use Ajax then:

  • Put the data you want to show to the user the response from taxcalculator1
  • Use onreadystatechange to show it to the user
  • Don't leave the page before that happens (remove the window.location line).

If you want to load an entirely new page:

  • Don't use Ajax
  • Just submit a form to a URL
  • Display the data you want the user to see (in the form of an HTML document) in the response to that request

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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