Welcome Guest, Not a member yet? Register   Sign In
AJAX and jQuery
#1

[eluser]Jason Tan Boon Teck[/eluser]
I am trying to learn to use AJAX with jQuery, but I can't seem to get this to work. I don't seem to be able to retrieve the data from the other method using the AJAX call. The button seems to be working as I could do an alert. Any help, please.


ehr_ajax.php
Code:
class Ehr_ajax extends MY_Controller

{
    function Ehr_ajax()

    {

        parent::Controller();

        
        $this->load->helper('url');
        $this->load->helper('form');
    $this->load->library('form_validation');
    $this->load->model('memr_rdb');
    }


    function index($continent = NULL)
    {
        $data['patient_id'] =   "19081987230220111298434484";
        // Load view.
        $this->load->view('test_ajax', $data);
    }


    function show_immunisation()
    {
        $data['patient_id'] = $_GET[patient_id];  //set the record ID
        //$data['patient_id'] = $this->uri->segment(3);

        $data['vaccines_list']     = $this->memr_rdb->get_vaccines_list($data['patient_id'],0);

        // Testing
        echo "Well done".$data['patient_id'] ;
        //$this->load->view(AJAX_record,$data['vaccines_list']);
    }
}


test_ajax.php
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

&lt;html &gt;
  &lt;head&gt;
    &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"/&gt;
    &lt;title&gt;jQuery AJAX&lt;/title&gt;
    &lt; script src="/jquery/jquery-1.4.2.min.js" type="text/javascript"&gt;[removed]

  &lt;/head&gt;
&lt;body&gt;

&lt;?php
$siteurl    =   site_url()."/ehr_ajax/show_immunisation";
echo $siteurl;
?&gt;

&lt;form&gt;  
    &lt;input type='submit' id='see_all' value='See all' /&gt;
    <div id='message'>Fill this</div>
&lt;/form&gt;  
&lt;/body&gt;

    [removed]
        $(document).ready(function() {
          $('#see_all').click(function () {
            var siteurl = "&lt;?php echo $siteurl; ?&gt;";
            var patient_id = "&lt;?php echo $patient_id; ?&gt;";
            var data = 'patient_id=' + patient_id;
            //alert(siteurl+data);
            $.ajax({
              type:'GET',
              url:siteurl,
              data: data,
              success: function (result) {
                $('#message').html(result);
              }
            });
            return false;
          });
        });
    [removed]
#2

[eluser]SPeed_FANat1c[/eluser]
what errors are you getting? does the show_immunisation work when you call it from address field in browser, e.g. www.yourdomain.com/Ehr_ajax/show_immunisation?patient_id=xxxx


but I guess I see at least one error:

$_GET[patient_id]

this is array, I think it can only work this way:

$_GET['patient_id']

Also since you are new to this, you may not know that you can use a console, for example in google chrome it is called with ctrl + shift + i and see what errors are you getting and many more info.
#3

[eluser]Jason Tan Boon Teck[/eluser]
[quote author="SPeed_FANat1c" date="1307052842"]what errors are you getting? does the show_immunisation work when you call it from address field in browser, e.g. www.yourdomain.com/Ehr_ajax/show_immunisation?patient_id=xxxx


but I guess I see at least one error:

$_GET[patient_id]

this is array, I think it can only work this way:

$_GET['patient_id']

Also since you are new to this, you may not know that you can use a console, for example in google chrome it is called with ctrl + shift + i and see what errors are you getting and many more info.[/quote]

If i am not mistaken, I should at least get a simple response from show_immunisation -
echo "Well done".$data['patient_id'] ;

Once I can get that to display in the <div id=message></div>, I will work on returning a list of data retrieved from model and sending that to a view.

I've changed the GET's syntax but still the same, no result.

I have been using the error console in Firefox. It shows no errors at all. Anyway, I opened up the Google's console and found this error when I click the button:

Code:
Failed to load resource: the server responded with a status of 404 (Not Found)
ehr_ajax/show_immunisation?patient_id=19081987230220111298434484

I suspect it is the siteurl's fault which might be using:
http://127.0.0.1/thirra/index.php/ehr_aj...1298434484

My normal practise is to use segments to retrieve parameters.
#4

[eluser]SPeed_FANat1c[/eluser]
if you want you can use segments as parameters, I sometimes used them too, when used ajax calls. You tested it only with javascript as I understand, right?

Try to call this normaly, like you call other functions in CI and check what do you see in browser.

Maybe the constructor is causing problems, as I see you use different syntax for it than in CI user guide

http://ellislab.com/codeigniter/user-gui...nstructors
#5

[eluser]Jason Tan Boon Teck[/eluser]
[quote author="SPeed_FANat1c" date="1307061577"]if you want you can use segments as parameters, I sometimes used them too, when used ajax calls. You tested it only with javascript as I understand, right?

Try to call this normaly, like you call other functions in CI and check what do you see in browser.

Maybe the constructor is causing problems, as I see you use different syntax for it than in CI user guide

http://ellislab.com/codeigniter/user-gui...nstructors[/quote]

My controller looks different because I have some classes inside MY_controller.

I have tried to access the show_immunisation using the usual method of URL link. It works as expected, showing the "Well done" plus patient_id sentence.

I do not mind not using segments if necessary, as long as it works.

I believe the ajax link (siteurl) is showing ../index.php/ehr_ajax/show_immunisation?patient_id=xxxxxxx rather than ../index.php?patient_id=xxxxxxxxx
#6

[eluser]SPeed_FANat1c[/eluser]
Quote:I do not mind not using segments if necessary, as long as it works.

So why not trying them out?

Or you could also try using post mehtod.

But its weird that its not working, there should be some errors. Or if you have it onlive server, post link and we can check.

Without seeing it hard to say..
Quote:I believe the ajax link (siteurl) is showing ../index.php/ehr_ajax/show_immunisation?patient_id=xxxxxxx rather than ../index.php?patient_id=xxxxxxxxx

You can see in the console what url is it calling.

And I am not sure now if this way works. Actually I rarely use get method so I don't know.

var data = 'patient_id=' + patient_id;

I think you may also try add ending to this:

Code:
var siteurl = "&lt;?php echo $siteurl; ?&gt;" + "?id=xxx";

and then dont use variable 'data'

You may try this function:

Code:
$.post(siteurl, {id: xxx},  function(data) {
   alert("Data Loaded: " + data);
});


of course if you use this you must use $_POST in CI.
#7

[eluser]Jason Tan Boon Teck[/eluser]
I finally got it working by changing the ajax passing method to post instead of get.

Thank you all, for guidances and tips. I will seek for more, as I progress.
#8

[eluser]Unknown[/eluser]
Try this, it's a little library I have coded to help me with ajax code generation. doesn't use jquery though.
I have attached it with an example.
Put LoginC.php in Controllers and rename it to Login.php , LoginV.php & images folder in views & rename LoginV.php=>Login.php
ajax.php goes in the libraries folder.
#9

[eluser]predat0r[/eluser]
[quote author="Jason Tan" date="1307199771"]I finally got it working by changing the ajax passing method to post instead of get.

Thank you all, for guidances and tips. I will seek for more, as I progress.[/quote]

If you use CSRF protection, you have to pass the cookie at form submission to work well.
Need example?
#10

[eluser]Jason Tan Boon Teck[/eluser]
[quote author="predat0r" date="1308700282"][quote author="Jason Tan" date="1307199771"]I finally got it working by changing the ajax passing method to post instead of get.

Thank you all, for guidances and tips. I will seek for more, as I progress.[/quote]

If you use CSRF protection, you have to pass the cookie at form submission to work well.
Need example?[/quote]


Yes, please. Thank you.




Theme © iAndrew 2016 - Forum software by © MyBB