Leah
Leah

Reputation: 245

codeigniter : passing data from view to controller not working

I have this code in my view file (searchV.php):

<html>
    <head> 
        <title>Search Domains</title>
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
        <script>
            function noTextVal(){
                $("#domaintxt").val("");
            }

            function searchDom(){
                var searchTxt = $("#searchTxt").val();
                var sUrl = $("#url").val();

                $.ajax({
                    url : sUrl + "/searchC",
                    type : "POST",
                    dataType : "json",
                    data :  { action : "searchDomain", searchTxt : searchTxt },
                    success : function(dataresponse){
                        if(dataresponse == "found"){
                            alert("found");
                        }
                        else{
                            alert("none");
                        }
                    }

                });
            }
        </script>
    </head>
    <body>

        <form id="searchForm">
                <input type="text" id="searchTxt" name="searchTxt" onclick="noTextVal()" >
                <input type="submit" id="searchBtn" name="searchBtn" value="Search" onclick="searchDom()" />
                <input type="hidden" id="url" name="url" value="<?php echo site_url(); ?>" />
        </form>

        <?php
            var_dump($domains);
            if($domains!= NULL){
                foreach ($domains->result_array() as $row){
                    echo $row['domain'] . " " . $row['phrase1'];
                    echo "<br/>";
                 }
            }

        ?>

    </body>
</html>

and below is my controller (searchC.php):

<?php

class SearchC extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
        $this->load->model('searchM');
    }

    public function index()
    {
        $data['domains'] = $this->searchM->getDomains();
        $this->load->view('pages/searchV', $data);



        switch(@$_POST['action']){
            case "searchDomain":
                echo "test";
                $this->searchDomains($_POST['searchTxt']);
                break;
            default:
                echo "test2";
                echo "<br/>action:" . ($_POST['action']);
                echo "<br/>text:" . $_POST['searchTxt'];
        }
    }

    public function searchDomains($searchInput)
    {
        $data['domains'] = $this->searchM->getDomains($searchInput);
        $res = "";

        if($data['domains']!=NULL){ $res = "found"; }
        else{ $res = "none"; }

        echo json_encode($res);
    }
} //end of class SearchC

?>

Now I've done a test on the controller using switch to check if the json data passed was successful but it's always showing undefined.. What's wrong here? Can someone explain why the data is not recognized in the controller??

Upvotes: 1

Views: 3576

Answers (2)

Samer Bechara
Samer Bechara

Reputation: 2109

I believe that the data is being correctly returned, but the problem is with your code check. The $.ajax function parses the JSON and transforms it into a JavaScript object. Therefore you would need to modify your code as follows:

if(dataresponse.res == "found"){ // Changed from dataresponse to dataresponse.res
   alert("found");
}
else{
  alert("none");
}

This should work for you.

Upvotes: 0

brightintro
brightintro

Reputation: 1016

You aren't passing the data in via the url, so you need to use $this->input->post() to retrieve the data.

For example,

public function searchDomains()
{
    $data['domains'] = $this->searchM->getDomains($this->input->post('searchTxt'));
    $res = "";

    if($data['domains']!=NULL){ $res = "found"; }
    else{ $res = "none"; }

    echo $res;
}

Upvotes: 1

Related Questions