I am trying to implement a search module by using AJAX.

There is an index.ctp file in my Items Controller and I have linked my 
index.ctp file of Items to my search.ctp file which is present under Items 
controller as below:

For the search.ctp pages the URL displayed is : 
http://onlineelectronic.com/Items/search

In my search.ctp file the code is as follows:
<head>



    <title> Search Results</title>

    <?php echo $this->Html->script(
'//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js', array(
'inline' => false));?>

    <script type="text/javascript">

        $(document).ready(function() {

            $("#Submit1").click(function () {

                alert('here');

                $.ajax({

                    type: 'post',

                    url: "/Items/search",

                    data: {

                        name: search

                    },

                    beforeSend: function(){

                        $("#resultField").html("Loading...");

                    },

                    success: function (result) {

                        jQuery('#resultField').html(result.valueOf($search
));

                    },

                    error: function (response, error) {

                        alert("Search :Error"+error);

                    },

                    dataType: 'json',

                    global: false

                });

            });

        });

    </script>

</head>

<div>

    <?= $this->Form->create() ?>

    <fieldset>

    <legend><?= __('Search Item') ?></legend>

    <?php

    echo $this->Form->input('search',['label'=>'Search']);

    ?>

    </fieldset>

    <?=$this->Form->submit('Search Items',['id'=>'Submit1']); ?>

    <?= $this->Form->end() ?>

</div>

<fieldset>

    <div id="resultField">

    </div>  
</fieldset>


So my ItemsController code is as follows:
 
class ItemsController extends AppController
{




    public $helpers = ['Form', 'Html', 'Time'];


    public function initialize()
    {
        parent::initialize();
        $this->loadComponent('RequestHandler');
    }


    public function search(){
    //dummy
    }


    /**
     *obtains search result for a given string.
     */
    public function searchData()
    {
       
        echo "here";
        $search_data=[];
        var_dump($search_data);
        //$search_results = [];
        if ($this->request->is('post')) {
            $search_data= $this->request->data;
            $search_data=implode("|",$search_data);
            $search_results = $this->Items->find('all', array('conditions'=>
array('Items.itemName LIKE'=>"%$search_data%")));
            if(!empty($search_results)) {
                $this->set(compact($search_results));
                $this->set('_serialize',array('search_results'));
                echo json_encode($search_results);
            }
        }


    }

    public function beforeFilter(Event $event)
    {
        parent::beforeFilter($event);

        $this->Auth->allow(['index', 'view','search','searchData']);

    }
}


What i have observed is that form tries to call action method search() from 
Items Controller so I included a dummy method in controller with no code.
But when the submit button is clicked only the alert comes up and then the 
page is no more responsive.

-- 
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

--- 
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.

Reply via email to