• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do I insert Javascript in CI?

#1
[eluser]EthanSP[/eluser]
I have a form in CI and I want to insert some Javascript for the form's validation. How do I do it? I tried the code below, but I don't know where does CI go when it encounters the said script:

Code:
<? $attributes = array('onSubmit'=>'return checkform()');
echo form_open('welcome/login',$attributes); ?>

Where should I put my checkform() function codes... in the view, model, or control file? How exactly CI acts from this point onwards? I tried searching the forum but to no avail.

I hope someone will be of help unlike on my last post where no one replied anymore. I believe that this forum is still alive.

Thank you.

#2
[eluser]davidbehler[/eluser]
Basically you got two options:

1. You can add a "[removed][removed]" tag to your view and insert your js function there or
2. Put your function in a js file and include that file in your view using "[removed][removed]"

If the code of your function depends on the data that was loaded, e.g. the id of an element you work on, then you can't put it in a js file but have to put it in your view and fill all the parts that are variable using <?=?>.

Correct me if I am wrong, but that's what I have been doing so far and I could not come up with anything else that seemed more appropriate. (Except that you can create your js code in the controller and give it to the view file using the $data array)

#3
[eluser]EthanSP[/eluser]
[quote author="waldmeister" date="1215443884"]Basically you got two options:

1. You can add a "[removed][removed]" tag to your view and insert your js function there or
2. Put your function in a js file and include that file in your view using "[removed][removed]"
[/quote]

Waldmeister, thanks for your reply. I haven't used the [removed] tags ever. I'll try to use it now but if you got time, can I ask for a working example?

Quote:...you can create your js code in the controller and give it to the view file using the $data array)

Yes, I tried doing this once but it seems to be defeating the purpose of JS and that, as what I understand, is to prevent accessing the CGI script from the server as much as possible. That's why, I tried experimenting and researching on some other sort, w/c brought me here. And I thank you for giving a piece of what you know. So, I think, it's time to try it out.

Again, thank you.


Jong


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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