CodeIgniter Forums

Full Version: java/ci
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]minagabriel[/eluser]
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

Code:
[removed]
function doanything(){
    alert ('javascript ') ;
}

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

El Forum

[eluser]cahva[/eluser]
You are missing () after the doanything in onsubmit.

El Forum

[eluser]minagabriel[/eluser]
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 ?

El Forum

[eluser]cahva[/eluser]
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:
Code:
<!DOCTYPE HTML>
&lt;html&gt;
&lt;head&gt;
    &lt;title&gt;Some page&lt;/title&gt;
&lt;/head&gt;
&lt;body&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>
&lt;/body&gt;
&lt;/html&gt;

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:
Code:
&lt; script src="&lt;?=site_url('somecontroller/somemethod') ?&gt;"&gt;&lt;/ script>

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

    function __construct()
    {
        parent::__construct();
    }
    
    function somemethod()
    {
        $this->load->view('some_view_containing_js');
    }
}

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.

El Forum

[eluser]minagabriel[/eluser]
thnx alot cahva that was very helpful i will try this Smile