Ashir
Ashir

Reputation: 581

Invoke a controller's function using a form button on PHP/CodeIgniter

I'm trying to do a small proyect in PHP using CodeIgniter and NetBeans. I have this HTML view:

<div class="section section_with_padding" id="users"> 
        <h1>Users Access</h1> 
        <div class="half left">
            <h4>Log In</h4>
            <p>Log In here.</p>
            <div id="login">
                <form method="post" name="contact" action="Test/any">
                    <div class="left">
                        <label for="fullname">User Name:</label> 
                        <input name="fullname" type="text" class="required input_field" id="fullname" maxlength="30" />
                    </div>
                    <div class="right">                           
                        <label for="email">Email:</label> 
                        <input name="email" type="text" class="validate-email required input_field" id="email" maxlength="30" />
                    </div>
                    <div class="clear"></div>
                    <label for="message">Message:</label> 
                    <textarea id="message" name="message" rows="0" cols="0" class="required"></textarea>
                    <input type="submit" class="submit_btn float_l" name="submit" id="submit" value="Login" />
                </form>
            </div>
        </div>

        <div class="half right">
            <h4>Mailing Address</h4>
            <h6><strong>Company Name</strong></h6>
            680-780 Aliquam semper dignissim,<br />
            Fusce cursus turpis lacus, 16780<br />
            Sit amet tortor

            <div class="clear h20"></div>
            <div class="img_nom img_border"><span></span>
            <iframe width="360" height="240" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.com/maps?f=q&amp;source=s_q&amp;hl=en&amp;geocode=&amp;q=Central+Park,+New+York,+NY,+USA&amp;aq=1&amp;sll=35.101934,-95.712891&amp;sspn=61.425057,135.263672&amp;vpsrc=6&amp;ie=UTF8&amp;hq=&amp;hnear=Central+Park,+New+York&amp;ll=40.771133,-73.974187&amp;spn=0.057126,0.132093&amp;t=m&amp;z=14&amp;output=embed"></iframe></div>
        </div>

        <a href="#home" class="home_btn">home</a> 
        <a href="#galeria" class="page_nav_btn previous">Prev</a>
        <a href="#admin" class="page_nav_btn next">Next</a>
    </div> 

Don't mind the half right div, that code isn't finnished yet. My questions is this: I want to call a function inside a controller (controller Test.php, function any()). I google it and found that I could use the attribute 'action' so

action="my_controller/my_function"

But when I try that on my code, I get a nice

"Not Found

The requested URL /Project/Test/any was not found on this server."

test.php is inside Project\application\controllers

What I'm doing wrong???

Upvotes: 2

Views: 8785

Answers (5)

Snehal S
Snehal S

Reputation: 875

Add index.php before your controller name i.e.

action = "index.php/my_controller/my_function"

You can also remove the need for index.php in the URL with URL re-writing:

RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]

as is explained in the manual: http://ellislab.com/codeigniter/user-guide/general/urls.html (under Removing the index.php file)

Upvotes: 5

user1741851
user1741851

Reputation:

A solution that I always use for Code Igniter is this.

<base href="<?php echo base_url(); ?>" />

This solves the problem for not only your form or AJAX requests but also to most of the CSS and JS includes.

You can then access any controller using index.php/controller/method

As for CSS and JS files,

<script src="assets/js/jsfile.js"></script>

Assuming your js files are inside assets/js folder while assets is in the same folder as your index.php

Upvotes: 1

Pattle
Pattle

Reputation: 6016

Have you set the base_url value in your application/config/config.php? It should be something like

$config['base_url'] = "http://localhost/folder_name/";

Then in your view you can do.

<form method="post" name="contact" action="<?php echo base_url();?>test/any">

If you have not configured Codeigniter to remove index.php them you'll have to do

<form method="post" name="contact" action="<?php echo base_url();?>index.php/test/any">

I would also double check that your controller is named correctly etc. A common mistake to make especially if you are using Netbeans is to accidentally forget the .php filename extension on your controller or something like that.

Upvotes: 0

Reena Shirale
Reena Shirale

Reputation: 2042

for this you use <form>...</form>like-

<form method="post" name="contact" action="<?php echo base_url();?>Test/any">

Upvotes: 0

Adarsh M Pallickal
Adarsh M Pallickal

Reputation: 821

You can use form_open() instead of

<div class="section section_with_padding" id="users"> 
        <h1>Users Access</h1> 
        <div class="half left">
            <h4>Log In</h4>
            <p>Log In here.</p>
            <div id="login">
                <?php $attributes = array('class' => 'class_name', 'id' => 'id' , 'name'=>'form_name');?>
    <?php echo form_open('test/any', $attributes); ?>
                    <div class="left">
                        <label for="fullname">User Name:</label> 
                        <input name="fullname" type="text" class="required input_field" id="fullname" maxlength="30" />
                    </div>
                    <div class="right">                           
                        <label for="email">Email:</label> 
                        <input name="email" type="text" class="validate-email required input_field" id="email" maxlength="30" />
                    </div>
                    <div class="clear"></div>
                    <label for="message">Message:</label> 
                    <textarea id="message" name="message" rows="0" cols="0" class="required"></textarea>
                    <input type="submit" class="submit_btn float_l" name="submit" id="submit" value="Login" />
                </form>
            </div>
        </div>

        <div class="half right">
            <h4>Mailing Address</h4>
            <h6><strong>Company Name</strong></h6>
            680-780 Aliquam semper dignissim,<br />
            Fusce cursus turpis lacus, 16780<br />
            Sit amet tortor

            <div class="clear h20"></div>
            <div class="img_nom img_border"><span></span>
            <iframe width="360" height="240" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.com/maps?f=q&amp;source=s_q&amp;hl=en&amp;geocode=&amp;q=Central+Park,+New+York,+NY,+USA&amp;aq=1&amp;sll=35.101934,-95.712891&amp;sspn=61.425057,135.263672&amp;vpsrc=6&amp;ie=UTF8&amp;hq=&amp;hnear=Central+Park,+New+York&amp;ll=40.771133,-73.974187&amp;spn=0.057126,0.132093&amp;t=m&amp;z=14&amp;output=embed"></iframe></div>
        </div>

        <a href="#home" class="home_btn">home</a> 
        <a href="#galeria" class="page_nav_btn previous">Prev</a>
        <a href="#admin" class="page_nav_btn next">Next</a>
    </div> 

Upvotes: 3

Related Questions