Welcome Guest, Not a member yet? Register   Sign In

is it possible to call a javascript function when submitting CI form
i have tried this simple function but it is not working could any one please tell me why

function doanything(){
    alert ('javascript ') ;

form the controller $data['attributes'] = array ('onsubmit'=>'return doanything')
from the view
<?php echo form_submit($attributes); ?>

You are missing () after the doanything in onsubmit.

this is not the actual code .... my question is this is how you use javascript and CI ??
if not can you give me anu tutorial or any other documentation for that ?

First, theres no such thing as a "CI form". Its just a form which was generated with CI's form helper functions. It will output basic html, nothing else Smile

Well you can do the javascript like you would normally do. Create a .js script and put it somewhere where it can be accessed. Lets say you have view hello.php which you will load through controller:
    &lt;title&gt;Some page&lt;/title&gt;
    <h1>Hello world!</h1>
    &lt;?=form_open('hello',array('onsubmit' => 'dosomething()')) ?&gt;
    &lt;?=form_submit('submit','Submit') ?&gt;
    &lt;?=form_close() ?&gt;
    &lt; script src="&lt;?=base_url() ?&gt;assets/js/myscript.js"&gt;&lt;/ script>

You would have that dosomething() function in myscript.js file.

You can also put js code into views and create controller that shows the view. In this case you can include the js like this in the view:
&lt; script src="&lt;?=site_url('somecontroller/somemethod') ?&gt;"&gt;&lt;/ script>

Controller could be something like this:
class Somecontroller extends Controller {

    function __construct()
    function somemethod()

But try to keep your javascript in .js files as much as possible because its always an overhead because you will be parsing the script through PHP and CI. Advantage of outputting the js code from CI/PHP is that you can use php tags inside your scripts and create dynamic js if needed.

thnx alot cahva that was very helpful i will try this Smile

Theme © iAndrew 2016 - Forum software by © MyBB