我是codeigniter的新手,我目前正在处理注册表。目前我只想让用户输入他们的名字和图片。但是,我有两个不同的控制器来处理这个问题,我不知道如何将它们合并为单个表单而没有两个不同的提交按钮。
查看
<div class="container">
<div class="row">
<section class="page col-md-8">
<h2 class="page-title">Registration Form</h2>
<form method="post" id="expertiseForm" action="" >
<?php echo $this->session->flashdata('msg'); ?>
<?php $attributes = array("name" => "expertiseform");
echo form_open("index.php/expertise/index", $attributes);?>
<div class="form-group required">
<label class="control-label" for="fname">First Name </label>
<input class="form-control" name="fname" type="text" value="<?php echo set_value('fname'); ?>" />
<span class="text-danger"><?php echo form_error('fname'); ?></span>
</div>
<div class="form-group">
<button type="submit" class="btn btn-default" value="form-submit'">Submit</button>
</div>
<?php echo form_close(); ?>
</form>
<!-- Upload form -->
<?php echo form_open_multipart('index.php/upload/do_upload', array('id' => 'uploadForm'));?>
<div class="form-group required">
<label class="control-label" for="filephotograph">Photograph: </label>
<input type="file" class="form-control-file" id="filephotograph" name="filephotograph" aria-describedby="fileHelp" value="<?php echo set_value('filephotograph'); ?>">
<small id="fileHelp" class="form-text text-muted">Upload a photograph smaller than 2 MB in size.</small>
<span class="text-danger"><?php echo form_error('filephotograph'); ?></span>
</div>
<input type="submit" value="upload" />
<?php echo form_close(); ?>
</form>
</section>
</div>
</div>
名字控制器
<?php
class Expertise extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->database();
$this->load->model('expertise_model');
}
function index()
{
$this->load->view('templates/navbar');
$this->load->view('templates/header');
$this->form_validation->set_rules('fname', 'First Name', 'trim|required|alpha|min_length[2]|max_length[30]|xss_clean');
if ($this->form_validation->run() == FALSE)
{
// fails
$this->load->view('expertise_form');
$this->load->view('templates/footer');
}
else {
$data = array(
'fname' => $this->input->post('fname')
);
if ($this->expertise_model->insert_expertise($data))
{
$this->session->set_flashdata('msg','<div class="alert alert-success text-center">You have successfully submit your expertise form for review.</div>');
redirect('index.php/expertise/index');
}
else
{
// error
$this->session->set_flashdata('msg','<div class="alert alert-danger text-center">Error. Please review your information and try again.</div>');
redirect('index.php/expertise/index');
}
}
}
?>
上传照片的控制器
<?php
class Upload extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
}
public function index()
{
$this->load->view('upload_form', array('error' => ' ' ));
}
public function do_upload()
{
$config['upload_path'] = './application/CAHSIfiles/uploads/photos';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 100;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('filephotograph'))
{
$error = array('error' => $this->upload->display_errors());
redirect('index.php/expertise/index');
}
else
{
$data = array('upload_data' => $this->upload->data());
redirect('index.php/expertise/index');
}
}
}
?>
答案 0 :(得分:1)
只有一组表单标签,其中url放入一个处理第一组表单项的函数,然后执行实际的上传逻辑
<?php echo form_open_multipart('index.php/index.php/expertise/proccess_profile', array('id' => 'uploadForm'));?>
<div class="form-group required">
<label class="control-label" for="fname">First Name </label>
<input class="form-control" name="fname" type="text" value="<?php echo set_value('fname'); ?>" />
<span class="text-danger"><?php echo form_error('fname'); ?></span>
</div>
<div class="form-group required">
<label class="control-label" for="filephotograph">Photograph: </label>
<input type="file" class="form-control-file" id="filephotograph" name="filephotograph" aria-describedby="fileHelp" value="<?php echo set_value('filephotograph'); ?>">
<small id="fileHelp" class="form-text text-muted">Upload a photograph smaller than 2 MB in size.</small>
<span class="text-danger"><?php echo form_error('filephotograph'); ?></span>
</div>
<div class="form-group">
<button type="submit" class="btn btn-default" value="form-submit'">Submit</button>
</div>
<?php echo form_close(); ?>
控制器
public function process_profile(){
//logic from your upload controller and other controller here
}