0

我在处理使用 codeigniter 创建的表单时遇到问题。我尝试了多种在网页上呈现表单的方法。但是在不同的点上我都不成功。

我正在寻找渲染这个

我试图用代码渲染一个表单:

<?php $this -> load -> helper('form');
    $formattr = array('id="form1"'); 
    form_open('registerUsr', $formattr);?>
        <fieldset>
            <legend>Registration Form</legend>
            <p class="first">
                <label for="firstName">First Name: </label>
                <input type="text" id="name" name="firstName" size="20" value="<?php echo set_value('first_name'); ?>" />
            </p>
        </fieldset>
        <p class="submit"><button type="submit" value="Submit" /></p>
<?php echo form_close(); ?>

我尝试使用 codeigniter 函数创建它

<?php echo form_open('registerUsr'); 
  echo form_input('$data'); 
  echo form_submit('submit', 'Submit'); 
  echo form_close();?>

我尝试了混合使用 html 标签和辅助类函数的混合品种。

但在所有方面,最终呈现网页上的表单<form class=“form1”&gt;标签都丢失了。它也不是 index.php/registerUsr,而是进入 index.php/searchUniv 页面。(我已经检查了路线配置文件。它被正确指出)

任何建议/想法表示赞赏。

蒂亚:)

更新:这是控制器和路由文件

public function registration() {
        $this -> template -> title -> set('Register');
        $this -> template -> _content -> view('register_index', $overwrite = TRUE);
        $this -> template -> _sidebar -> view('gen_sidebar', $overwrite = TRUE);
        $this -> template -> publish();
    }

public function registerUsr(){
        $this -> template -> title -> set('Register');
        $searchItem = $this -> input -> post('form1');
        $this -> template -> publish();
    }
}

register_index.php 是问题中的代码,现在看起来还可以。

$route['searchUniv']="sglobal/searchUniv";
$route['registration']="sglobal/registration";
$route['registerUsr']="sglobal/registerUsr";

模板是我编写的自定义库,在所有其他页面中都可以正常工作。这个应用程序处于最后阶段,所以我可以有 95% 的信心说,模板工作正常。此外,我正在查看最终渲染页面的来源以及它到底想要什么。那么,我们可以在指向模板之前查看任何配置吗?

现在最终的渲染看起来像:

<form action="http://localhost/univapp/index.php/registerUsr" method="post" accept-charset="utf-8" id="form1" class="form1">

但是在提交它的时候http://localhost/univapp/index.php/searchUniv

仅供参考,我在同一页面上还有另一个搜索表单(很远的 div,不同的 id,不同的名称,不同的类),它使用指向 index.php/searchUniv 的 POST。有什么问题吗?

4

3 回答 3

0

你的第一个例子没有 echo form_open()。尝试在函数调用之前添加回声。

<?php $this -> load -> helper('form');
    $formattr = array('id="form1"'); 
    echo form_open('registerUsr', $formattr);?>
        <fieldset>
            <legend>Registration Form</legend>
            <p class="first">
                <label for="firstName">First Name: </label>
                <input type="text" id="name" name="firstName" size="20" value="<?php echo set_value('first_name'); ?>" />
            </p>
        </fieldset>
        <p class="submit"><button type="submit" value="Submit" /></p>
<?php echo form_close(); ?>

默认情况下,表单也会提交到当前页面,这可能是它被定向到 index.php/searchUniv 的原因。

于 2013-02-07T14:13:34.613 回答
0

如果不需要,尽量避免使用 Codeigniter 助手,html 渲染速度比 php 快。表单助手所需要的只是 form_open() ,因为这会添加 csrf 令牌。您的代码中似乎也有一些错字!

清单:

应用程序/config/config.php

$config['base_url'] = 'http://www.mysite.com/'; //I suspect this is your problem with routing
$config['csrf_protection'] = TRUE;

应用程序/config/routes.php

$route['register']  = 'controller/get_method'; //form
$route['register/post'] = 'controller/post_method'; //validation

意见/registerUsr.php

<?php if( validation_errors() ) : ?>
  <p class=error>Please Correct the Errors below</p>
<?php endif; ?>

<?=form_open('register/post', array('id'=>'', 'class'=>''))?>

<fieldset>
    <legend></legend>
    <label for="firstname">
        <span class=required>*</span>
        First Name
        <small class=helper>Enter your First Name</small>
    </label>
    <input type=text id=firstname name=firstname value="<?=set_value('firstname')?>"/>
    <?=form_error('firstname')?>

    <button type=submit>Submit</button>
</fieldset>

<?=form_close()?>
于 2013-02-07T14:21:38.167 回答
0

发现错误。我正在查看“查看源代码”选项以了解页面的呈现方式以及呈现的数据。但是查看 DOM 检查器(Inspect Element)的输出告诉我 DOM 仍然没有考虑表单标签。作为对 DOM 对 PHP 填充的无效 HTML 执行的更正的一部分,它删除了它。

任何给出的属性<input name=... .... />都会被填充,<input name=... .... >因此会留下很多打开的标签。一旦< />模板中的每个标签都被更改<> </>,它就开始工作了。

简而言之,代码通过验证器运行,现在一切正常。

感谢您所有的帮助!

于 2013-02-08T06:47:56.013 回答