【问题标题】:Invoke a controller's function using a form button on PHP/CodeIgniter使用 PHP/CodeIgniter 上的表单按钮调用控制器的功能
【发布时间】:2014-01-30 02:51:08
【问题描述】:

我正在尝试使用 CodeIgniter 和 NetBeans 在 PHP 中做一个小项目。我有这个 HTML 视图:

<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> 

别介意半右边的 div,那段代码还没有完成。我的问题是:我想在控制器中调用一个函数(控制器 Test.php,函数 any())。我谷歌它,发现我可以使用属性'action'所以

action="my_controller/my_function"

但是当我在我的代码上尝试时,我得到了很好的结果

“未找到

在此服务器上找不到请求的 URL /Project/Test/any。"

test.php 在 Project\application\controllers 中

我做错了什么???

【问题讨论】:

  • 你正在使用 CI 。所以用 form_open() 而不是
  • 您可以像@meenakshi 所说的那样使用 fork_open。此外,您可以在表单中使用action = "&lt;?php echo site_url("controller/function_name");?&gt;"。
  • 您能把代码贴在您构建的控制器中吗?

标签: php html codeigniter


【解决方案1】:

在控制器名称前添加index.php,即

action = "index.php/my_controller/my_function"

您还可以通过 URL 重写来消除 URL 中对 index.php 的需要:

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

如手册中所述:http://ellislab.com/codeigniter/user-guide/general/urls.html(在删除 index.php 文件下)

【讨论】:

  • 这给了我“遇到错误无法加载请求的文件:test.php”
  • 尝试从浏览器 url 运行这个函数。是否抛出同样的错误?
  • 不,算了。测试是 HTML 文件而不是 PHP 文件。您的解决方案运行良好。每次需要调用控制器的函数时,MVC 标准添加 index.php 是否正确?
  • 您可以在项目的htaccess文件中写入“rewrite”规则,避免每次都写入“index.php”
【解决方案2】:

您可以使用 form_open() 代替

<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> 

【讨论】:

    【解决方案3】:

    我一直用于 Code Igniter 的解决方案是这个。

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

    这不仅解决了表单或 AJAX 请求的问题,还解决了大多数 CSS 和 JS 包含的问题。

    然后您可以使用 index.php/controller/method 访问任何控制器

    至于 CSS 和 JS 文件,

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

    假设您的 js 文件位于 assets/js 文件夹中,而 assets 与您的 index.php 位于同一文件夹中

    【讨论】:

      【解决方案4】:

      为此,您使用&lt;form&gt;...&lt;/form&gt;like-

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

      【讨论】:

      • @Ashir 你尝试输入 url 硬编码 like- localhost/folder_name/controller_name/Function_name 如果你没有删除 index.php 然后在 url 中使用 like- localhost/folder_name/index.php/controller_name/Function_name
      【解决方案5】:

      您是否在 application/config/config.php 中设置了 base_url 值?应该是这样的

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

      那么在你看来你可以做到。

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

      如果你还没有配置 Codeigniter 来删除 index.php 他们你必须这样做

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

      我还会仔细检查您的控制器是否正确命名等。尤其是在使用 Netbeans 时,一个常见的错误是不小心忘记了控制器上的 .php 文件扩展名或类似的东西.

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-11-28
        • 2020-09-07
        • 2021-07-22
        • 2019-06-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多