CodeIgniter Forums

Full Version: AJAX help wanted
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have managed to get an CI4 AJAX Dynamic Search routine working but it was an 'Orrible Kludge.

I had a tremendous amount of problems in trying to call this dbSearchResults.php file using the following JavaScript:
Code:
   <script type="text/javascript">
     function showResult(str)
     {
       ...
       ...
       ...
       
       xmlhttp.open("GET","dbSearchResults.php?q="+str,true);
       xmlhttp.send();
     }// endfunc ShowResults
   </script>


My knowledge of JavaScript is very basic and no solved or other solutions were found.

I would be very grateful for a link to a tutorial. If none are available then I would like to know the correct method to call and use a PHP file with AJAX.
First, load jQuery in the head section of your view.
Then, before the </body> closing tag, have this piece of code:

Code:
<script>
$(document).ready(function(){
    var url = "<?= site_url();?>controller/method";
    $.get( url, { p1: val1, p2 : val2 })
    .done(function(data) {
            //handle the returned data here
    });    
});
</script>

In the controller, use $this->input->get('p1') to read the value of p1.
Let the controller method echo the data that you need back in the view.
(07-18-2017, 11:41 AM)Wouter60 Wrote: [ -> ]First, load jQuery in the head section of your view.
Then, before the </body> closing tag, have this piece of code:

Code:
<script>
$(document).ready(function(){
    var url = "<?= site_url();?>controller/method";
    $.get( url, { p1: val1, p2 : val2 })
    .done(function(data) {
            //handle the returned data here
    });    
});
</script>

In the controller, use $this->input->get('p1') to read the value of p1.
Let the controller method echo the data that you need back in the view.

Many thanks for the suggestion. 

The following day I tried the AJAX call again and it worked for some weird reason! The previous day I had checked the filename and path, also tried renaming the file and using a different path, checked log files, file permissions, htaccess file, etc, etc, etc  and all to no avail.

Don't you love programming Smile
I have had that happen before, I reboot my computer system and then everything is fine.
(07-21-2017, 02:22 AM)John_Betong Wrote: [ -> ]Don't you love programming Smile

LOL.

For a whole day I had a https error message implying my domain name was not secure. Today it just suddenly disappeared. WTF?

I sometimes wonder what my host is actually doing at times. Just cannot be bothered to change but perhaps I should.

Paul.