gpt4 book ai didi

php - set_value 不适用于 Codeigniter 上的输入类型文件

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:47:28 26 4
gpt4 key购买 nike

我正在为我的项目使用 HMVC Codeigniter 模式。在我的项目中,我需要在输入字段类型为文件的表单上设置值。所以,我很感谢帮助我解决这个问题的他们。

我的 Controller 是test.php

<?php
class Test extends Controller{
function __construct(){
parent::__construct();

}
function index(){

redirect('test/form');


}
function form(){
$this->load->library('form_validation');
$this->load->helper('url');
$this->form_validation->set_rules('fname','First Name','required|trim');
$this->form_validation->set_rules('mname','Middle Name','required|trim');
$this->form_validation->set_rules('lname','Last Name','required|trim');


if (empty($_FILES['userfile']['name']))
{
$this->form_validation->set_rules('userfile', 'Attach Your File', 'required');
}
if($this->form_validation->run($this)){

$_SESSION['msg']="Your form has been submitted succesfully!!";
redirect('test/form');
}
else
{

$_SESSION['err']="Opp! There was some wrong to fill up a form so try again!!!";
redirect('test/form');


}

}
$data['pgtitle']='Test';
$this->load->view('test',$data);
}
}

我的 View 文件是test.php

<form action="<?php echo site_url()?>test/form" method="post" enctype="multipart/form-data" onsubmit="return confirm('Do you really want to submit the form?');">
<table width="100%" border="0" cellspacing="0" cellpadding="0">


<tr>
<td width="">Name</td>
<td width="158"><label for="fname"></label>
<input type="text" name="fname" id="fname" placeholder="First Name" class='input-row' value="<?php echo set_value('fname')?>"/><span class="required"><?php echo form_error('fname');?></span></td>
<td width="158"><label for="mname"></label>
<input type="text" name="mname" id="mname" placeholder="Middle Name" class='input-row' value="<?php echo set_value('mname')?>"/></td>
<td width="158"><label for="lname"></label>
<input type="text" name="lname" id="lname" placeholder="Last Name" class='input-row' value="<?php echo set_value('lname')?>"/><span class="required"><?php echo form_error('lname');?></span></td>
</tr>


<tr>
<td>Attach Your File</td>
<td colspan="2"><label for="userfile"></label>
<input type="file" name="userfile" id="usefile" value="<?php echo set_value('userfile')?>"/><span class="error"><?php echo form_error('userfile')?></span></td>
</tr>
<tr>

<td><input type="submit" name="submit" id="submit" value="Submit" class="Button" /></td>
</tr>
</table>
</form >

最佳答案

Set_value 将不起作用,因为您正在重定向 redirect('test/form'); 当验证失败时。您必须加载 form view 而不进行重定向才能正常工作。

if ($this->form_validation->run() == FALSE)
{
$this->load->view('myform');
}

关于php - set_value 不适用于 Codeigniter 上的输入类型文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29074801/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com