简体   繁体   中英

codeigniter call a view / controller on ajax call sucess

I am new to codeigniter , I am trying to call a controller on AJAX call sucess... I tried to search stackoverflow but i didn't get what i needed..

Form in my view

<form onsubmit="categorysearch()" method="GET">



                <label for="username">Category : </label>
                <select id="category" name="category">
                    <option value="android">android</option>
                    <option value="iphone">iphone</option>
                    <option value="windowsphone">windowsphone</option>
                    <option value="blackberry">blackberry</option>

                </select>


                <input type="submit" name="searchkeywordsubmit" title="Search" id="searchkeywordsubmit" />


            </form>

When the form is submitted the below java script is executed.

JavaScript in my View

 function categorysearch()
        {
            var categorykeyword = document.getElementById("category").value;
            alert("Category keyword is " + categorykeyword);


            $.ajax({
                type: "GET",
                async: false,
            //dataType: "json",
            url: "http://localhost/2009074/index.php/rest/resource/categorysearch/category/" + categorykeyword + "",
            success: function(data)
            {

               alert("Returned data is " + data);
               // i want to call the constructor here with the returned data


                //$("body").html(data);

                //$('body').append(output_string);


            },
            error: function(data)
            {
                alert("error is " + data);
            }

        });
    }

AJAX call works successfully , I can see the returned data in the alert (Returned data is JSON encoded)

Now i want to call another controller with the recieved data ... Please help me to figure this out

you just need to redirect the control the required controller :

success: function(data)
            {

              window.location.href="http://localhost/my_project_name/my_controller_name";// you just need to add this event on success call.

            },

A really dirty way to do it is save the JSON object as session variable and navigate to a page or reload the page and in the constructor of the page, check for the session variable and use the variable.

well that's the only way I can think of at the moment.

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