Php 表单验证后重定向codeigniter多个表单?

Php 表单验证后重定向codeigniter多个表单?,php,codeigniter,phpunit,Php,Codeigniter,Phpunit,在表单验证后,我遇到重定向问题。我在不同的视图中有3个不同的表单,经过验证后,如果表单未能提交,我想将其重定向到上一个登录页面。它工作正常,但在提交表单后,如果我单击url并按enter键。当我在主页中时,它会重定向到联系人。我希望它重定向到他提交的上一个登录页面。我认为这不是正确的做法,提前谢谢 <?php echo validation_errors(); ?> <?php echo form_open('con/validate_form'); ?> <h

在表单验证后,我遇到重定向问题。我在不同的视图中有3个不同的表单,经过验证后,如果表单未能提交,我想将其重定向到上一个登录页面。它工作正常,但在提交表单后,如果我单击url并按enter键。当我在主页中时,它会重定向到联系人。我希望它重定向到他提交的上一个登录页面。我认为这不是正确的做法,提前谢谢

<?php echo validation_errors(); ?>

<?php echo form_open('con/validate_form'); ?>

<h5>Username</h5>
<input type="text" name="username" value="" size="50" />

<h5>Password</h5>
<input type="text" name="password" value="" size="50" />

<h5>Password Confirm</h5>
<input type="text" name="passconf" value="" size="50" />

<h5>Email Address</h5>
<input type="text" name="email" value="" size="50" />

<div><input type="submit" name='submit1' value="Submit1" /></div>

</form>

</body>
</html>
myform1.php 我的表格1

<?php echo validation_errors(); ?>

<?php echo form_open('con/validate_form'); ?>

<h5>Username</h5>
<input type="text" name="username" value="" size="50" />

<h5>Password</h5>
<input type="text" name="password" value="" size="50" />

<h5>Password Confirm</h5>
<input type="text" name="passconf" value="" size="50" />

<h5>Email Address</h5>
<input type="text" name="email" value="" size="50" />

<div><input type="submit" name='submit1' value="Submit1" /></div>

</form>

</body>
</html>
控制器控制器

<?php echo validation_errors(); ?>

<?php echo form_open('con/validate_form'); ?>

<h5>Username</h5>
<input type="text" name="username" value="" size="50" />

<h5>Password</h5>
<input type="text" name="password" value="" size="50" />

<h5>Password Confirm</h5>
<input type="text" name="passconf" value="" size="50" />

<h5>Email Address</h5>
<input type="text" name="email" value="" size="50" />

<div><input type="submit" name='submit1' value="Submit1" /></div>

</form>

</body>
</html>
    public function myform1(){
        $this->load->view('myform1');
    }

    public function myform2()   {
        $this->load->view('myform2');
    }

    public function myfrom3()   {
        $this->load->view('myform3');
    }

我不明白为什么你有三个相同的表格,但好吧。。。为什么不呢?这可能是一种继续的方式(使用相同的验证方法以避免重复代码)

<?php echo validation_errors(); ?>

<?php echo form_open('con/validate_form'); ?>

<h5>Username</h5>
<input type="text" name="username" value="" size="50" />

<h5>Password</h5>
<input type="text" name="password" value="" size="50" />

<h5>Password Confirm</h5>
<input type="text" name="passconf" value="" size="50" />

<h5>Email Address</h5>
<input type="text" name="email" value="" size="50" />

<div><input type="submit" name='submit1' value="Submit1" /></div>

</form>

</body>
</html>
有不同的方法进行:

<?php echo validation_errors(); ?>

<?php echo form_open('con/validate_form'); ?>

<h5>Username</h5>
<input type="text" name="username" value="" size="50" />

<h5>Password</h5>
<input type="text" name="password" value="" size="50" />

<h5>Password Confirm</h5>
<input type="text" name="passconf" value="" size="50" />

<h5>Email Address</h5>
<input type="text" name="email" value="" size="50" />

<div><input type="submit" name='submit1' value="Submit1" /></div>

</form>

</body>
</html>
1) 对当前位置使用隐藏输入:

<?php echo validation_errors(); ?>

<?php echo form_open('con/validate_form'); ?>

<h5>Username</h5>
<input type="text" name="username" value="" size="50" />

<h5>Password</h5>
<input type="text" name="password" value="" size="50" />

<h5>Password Confirm</h5>
<input type="text" name="passconf" value="" size="50" />

<h5>Email Address</h5>
<input type="text" name="email" value="" size="50" />

<div><input type="submit" name='submit1' value="Submit1" /></div>

</form>

</body>
</html>
myform1.php

<?php echo validation_errors(); ?>

<?php echo form_open('con/validate_form'); ?>

<h5>Username</h5>
<input type="text" name="username" value="" size="50" />

<h5>Password</h5>
<input type="text" name="password" value="" size="50" />

<h5>Password Confirm</h5>
<input type="text" name="passconf" value="" size="50" />

<h5>Email Address</h5>
<input type="text" name="email" value="" size="50" />

<div><input type="submit" name='submit1' value="Submit1" /></div>

</form>

</body>
</html>
<?php echo validation_errors(); ?>

<?php echo form_open('con/validate_form'); ?>

<h5>Username</h5>
<input type="text" name="username" value="" size="50" />

<h5>Password</h5>
<input type="text" name="password" value="" size="50" />

<h5>Password Confirm</h5>
<input type="text" name="passconf" value="" size="50" />

<h5>Email Address</h5>
<input type="text" name="email" value="" size="50" />

<input type="hidden" name="redirect_to" value="<?php echo current_url(); ?>" />
<div><input type="submit" name='submit1' value="Submit1" /></div>

</form>

</body>
</html>
但它并不完美,因为使用
redirect()
方法无法轻松地重新填充表单

<?php echo validation_errors(); ?>

<?php echo form_open('con/validate_form'); ?>

<h5>Username</h5>
<input type="text" name="username" value="" size="50" />

<h5>Password</h5>
<input type="text" name="password" value="" size="50" />

<h5>Password Confirm</h5>
<input type="text" name="passconf" value="" size="50" />

<h5>Email Address</h5>
<input type="text" name="email" value="" size="50" />

<div><input type="submit" name='submit1' value="Submit1" /></div>

</form>

</body>
</html>
2) 这就是为什么我们通常不使用
验证表单()

<?php echo validation_errors(); ?>

<?php echo form_open('con/validate_form'); ?>

<h5>Username</h5>
<input type="text" name="username" value="" size="50" />

<h5>Password</h5>
<input type="text" name="password" value="" size="50" />

<h5>Password Confirm</h5>
<input type="text" name="passconf" value="" size="50" />

<h5>Email Address</h5>
<input type="text" name="email" value="" size="50" />

<div><input type="submit" name='submit1' value="Submit1" /></div>

</form>

</body>
</html>
您希望发布表单,对其进行验证,然后在验证失败时再次显示带有验证错误的表单,或者在验证通过时调用register方法

<?php echo validation_errors(); ?>

<?php echo form_open('con/validate_form'); ?>

<h5>Username</h5>
<input type="text" name="username" value="" size="50" />

<h5>Password</h5>
<input type="text" name="password" value="" size="50" />

<h5>Password Confirm</h5>
<input type="text" name="passconf" value="" size="50" />

<h5>Email Address</h5>
<input type="text" name="email" value="" size="50" />

<div><input type="submit" name='submit1' value="Submit1" /></div>

</form>

</body>
</html>
最好的方法是将表单发回自己。这样,在您的方法中,您可以检查表单是否已提交,并确定要执行的操作:

<?php echo validation_errors(); ?>

<?php echo form_open('con/validate_form'); ?>

<h5>Username</h5>
<input type="text" name="username" value="" size="50" />

<h5>Password</h5>
<input type="text" name="password" value="" size="50" />

<h5>Password Confirm</h5>
<input type="text" name="passconf" value="" size="50" />

<h5>Email Address</h5>
<input type="text" name="email" value="" size="50" />

<div><input type="submit" name='submit1' value="Submit1" /></div>

</form>

</body>
</html>
public function myform1()
{
    $this->load->library('form_validation');

    $this->form_validation->set_rules('username', 'Username', 'required');
    $this->form_validation->set_rules('password', 'Password', 'required');

    if ($this->form_validation->run() == FALSE)
    {
        $this->load->view('myform');
    }
    else
    {
        $this->register();
    }
}
观点:

<?php echo validation_errors(); ?>

<?php echo form_open('con/validate_form'); ?>

<h5>Username</h5>
<input type="text" name="username" value="" size="50" />

<h5>Password</h5>
<input type="text" name="password" value="" size="50" />

<h5>Password Confirm</h5>
<input type="text" name="passconf" value="" size="50" />

<h5>Email Address</h5>
<input type="text" name="email" value="" size="50" />

<div><input type="submit" name='submit1' value="Submit1" /></div>

</form>

</body>
</html>
<html>
<head>
<title>My Form 1</title>
</head>
<body>

<?php echo validation_errors(); ?>

<?php echo form_open('con/myform1'); ?>

<h5>Username</h5>
<input type="text" name="username" value="" size="50" />

<h5>Password</h5>
<input type="text" name="password" value="" size="50" />

<h5>Password Confirm</h5>
<input type="text" name="passconf" value="" size="50" />

<h5>Email Address</h5>
<input type="text" name="email"  value="" size="50" />

<div><input type="submit" name-'submit' /></div>

</form>

</body>
</html>

我的中一
用户名
密码
密码确认
电子邮件地址

我要做的是为不同类型的表单错误创建一个错误文件夹。。。。。 对于表单验证错误,我将其重定向到特定的错误页面。。。 这对我来说很好

<?php echo validation_errors(); ?>

<?php echo form_open('con/validate_form'); ?>

<h5>Username</h5>
<input type="text" name="username" value="" size="50" />

<h5>Password</h5>
<input type="text" name="password" value="" size="50" />

<h5>Password Confirm</h5>
<input type="text" name="passconf" value="" size="50" />

<h5>Email Address</h5>
<input type="text" name="email" value="" size="50" />

<div><input type="submit" name='submit1' value="Submit1" /></div>

</form>

</body>
</html>

我还觉得您可能试图对多个表单视图使用同一个控制器。。。 这就产生了一个问题

<?php echo validation_errors(); ?>

<?php echo form_open('con/validate_form'); ?>

<h5>Username</h5>
<input type="text" name="username" value="" size="50" />

<h5>Password</h5>
<input type="text" name="password" value="" size="50" />

<h5>Password Confirm</h5>
<input type="text" name="passconf" value="" size="50" />

<h5>Email Address</h5>
<input type="text" name="email" value="" size="50" />

<div><input type="submit" name='submit1' value="Submit1" /></div>

</form>

</body>
</html>
我认为您的联系人和登录表单都有相同的重定向控制器con/myform1。。。。 看看

<?php echo validation_errors(); ?>

<?php echo form_open('con/validate_form'); ?>

<h5>Username</h5>
<input type="text" name="username" value="" size="50" />

<h5>Password</h5>
<input type="text" name="password" value="" size="50" />

<h5>Password Confirm</h5>
<input type="text" name="passconf" value="" size="50" />

<h5>Email Address</h5>
<input type="text" name="email" value="" size="50" />

<div><input type="submit" name='submit1' value="Submit1" /></div>

</form>

</body>
</html>

希望这有帮助……

“我也觉得您可能试图对多个表单视图使用同一个控制器……这会产生问题……”是的,完全正确。这不是一个好的解决办法。
<?php echo validation_errors(); ?>

<?php echo form_open('con/validate_form'); ?>

<h5>Username</h5>
<input type="text" name="username" value="" size="50" />

<h5>Password</h5>
<input type="text" name="password" value="" size="50" />

<h5>Password Confirm</h5>
<input type="text" name="passconf" value="" size="50" />

<h5>Email Address</h5>
<input type="text" name="email" value="" size="50" />

<div><input type="submit" name='submit1' value="Submit1" /></div>

</form>

</body>
</html>