Welcome Guest, Not a member yet? Register   Sign In
CodeIgniter Ajax Search Display
#1

[eluser]Katsune[/eluser]
Hey guys,

Here's my new code:

javascript file EDITED:
Code:
$(document).ready(function(){
//form validation for the empty search field
    var searchInputVar = $('#searchInput').val();
    $('form.ajaxForm').on('submit',function(){    
        if($("#searchInput").val().length<=0){
           $("#SearchPrompt").css({"display":"inline","color":"red"});
        return false;
        }
        else
        {          
        $('form.ajaxForm').on('submit',function() {
                $.ajax({
                        type: 'POST',
                        url: 'http://localhost/AnimeInventory/index.php/maincontroller/testForAjax',
                        data: searchInputVar,
                        
                        success: function(smg) {  
                            $("#info1").html('<li>Test'+smg+'</li>');
                        }
                });
        });
        return false;
        }
});
});

view
Code:
echo form_input('search','','id="searchInput"');

controller
Code:
public function testForAjax(){
           $valueContainer = $data = array ('Holder'=>$this->input->post('search'));
          
            print_r($valueContainer);
        
        }

SAMPLE RESULT:
Code:
TestArray ( [Holder] => )

It's weird because what I see in the firebug is the source code of the website. But when I use console.log('test'); I can see the text "test" in firebug. Im expecting that the msg should produce the value that was echoed in the controller.

Please help. Thanks,
#2

[eluser]Tpojka[/eluser]
You missed to name variable which value you are posting
Code:
data: { someVar : $("#searchInput").val() }, // in controller you approach to this value as $this->input->post('someVar');

Docs.
#3

[eluser]www.sblog.in[/eluser]
You can try with.
If you have not removed index.php using htaccess

Code:
$.ajax({
            type: "POST",
            url:  "&lt;?php echo base_url(); ?&gt;index.php/mainController/testForAjax",
            data:  "q="+$("#searchInput").val(),
            success: function(){
                console.log('Test result message of ajax');
            }
        });

If you have removed index.php using htaccess

Code:
$.ajax({
            type: "POST",
            url:  "&lt;?php echo base_url(); ?&gt;mainController/testForAjax",
            data:  "q="+$("#searchInput").val(),
            success: function(){
                console.log('Test result message of ajax');
            }
        });
#4

[eluser]Katsune[/eluser]
Hi I updated my code I think it's getting closer, I tried the
Code:
url:  "&lt;?php echo base_url(); ?&gt;mainController/testForAjax",
but it's causing disallowed character. I also verified that the url is working without the echo base_url.
#5

[eluser]InsiteFX[/eluser]
Code:
HTML:
// Change the $ in script to an s
&lt;head&gt;
<$cript type="text/javascript" charset="utf-8">
  //&lt;![CDATA[
   var base_url = "&lt;?php echo base_url(); ?&gt;";
   var site_url = "&lt;?php echo site_url(); ?&gt;";
  // ]]>
</$cript>
&lt;/head&gt;

Java$cript:

url:  base_url + "mainController/testForAjax",
url:  site_url + "mainController/testForAjax",
#6

[eluser]Katsune[/eluser]
[quote author="InsiteFX" date="1399638751"]
Code:
HTML:
// Change the $ in script to an s
&lt;head&gt;
<$cript type="text/javascript" charset="utf-8">
  //&lt;![CDATA[
   var base_url = "&lt;?php echo base_url(); ?&gt;";
   var site_url = "&lt;?php echo site_url(); ?&gt;";
  // ]]>
</$cript>
&lt;/head&gt;

Java$cript:

url:  base_url + "mainController/testForAjax",
url:  site_url + "mainController/testForAjax",
[/quote]

Hey, I got the url part working, but that's about it. Whenever I tried to do something the console.log(msg); still shows the whole page source code instead of the echo from the controller.T_T
#7

[eluser]InsiteFX[/eluser]
Don't use the console log message and see what happens
#8

[eluser]Katsune[/eluser]
I removed the console.log and also the msg inside the function and the echo inside the controller. Then just print_r the line inside the controller.

The html code blocks disappeared now, but it only shows this:
Code:
Array
(
    [Holder] =>
)

the actual input is not visible.
But I validated that the input box's value was taken by doing alert($("#searchInput").val());
and it's there.
#9

[eluser]CroNiX[/eluser]
Your controller code would be helpful.
#10

[eluser]Katsune[/eluser]
Here, please feel free to check.

Code:
public function testForAjax(){
           $valueContainer = $data = array ('Holder'=>$this->input->post('search'));
          
            print_r($valueContainer);
        
        }

I'm done once I got the value passed.
NOTE: I update my original post




Theme © iAndrew 2016 - Forum software by © MyBB