CodeIgniter Forums
AJAX help wanted - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: AJAX help wanted (/showthread.php?tid=68489)



AJAX help wanted - John_Betong - 07-18-2017

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.


RE: AJAX help wanted - Wouter60 - 07-18-2017

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.


RE: AJAX help wanted - John_Betong - 07-21-2017

(07-18-2017, 10: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


RE: AJAX help wanted - InsiteFX - 07-22-2017

I have had that happen before, I reboot my computer system and then everything is fine.


RE: AJAX help wanted - PaulD - 07-22-2017

(07-21-2017, 01: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.