• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
AJAX help wanted

#1
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.
CI4 - Playground:
    https://www.john-betong.tk/test/1/42
CI 3.15  -  AmpProject 
     http://tools.pingdom.com/fpt/#!/uC1KU/ww...-jokes.com

Reply

#2
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.
Reply

#3
(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
CI4 - Playground:
    https://www.john-betong.tk/test/1/42
CI 3.15  -  AmpProject 
     http://tools.pingdom.com/fpt/#!/uC1KU/ww...-jokes.com

Reply

#4
I have had that happen before, I reboot my computer system and then everything is fine.
What did you Try? What did you Get? What did you Expect?

Joined the CodeIgniter Community in 2009.          ( Skype: insitfx )
Reply

#5
(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.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2017 MyBB Group.