我有我的登錄和執行相同的實現,但在我的聯繫表單上它不是,我沒有錯誤信息只是路由到主頁。 奇怪的是,如果在我的控制器我實現了指數函數是這樣的:使用Codeigniter驗證表單不起作用
<?php
$attributes = array('class' => 'form-contact', 'id' => 'myform2');
echo form_open('contact',$attributes);
?>
而不是調用
public function index() {
$this->myprocess();
}
和我的觀點我的功能myprocess所有驗證工作,但沒有用的形式與我的標題頁腳等
波紋管,這是我希望我的代碼工作,我希望你能幫助我。
我的控制器:
<?php if (! defined('BASEPATH')) exit('No direct script access allowed');
class contact extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->model('contact_m');
}
public function index()
{
$data = array('title' => 'Contact', 'main_content' => 'contact_v');
$this->load->view('template', $data);
}
public function myprocess(){
$this->form_validation->set_rules('dfname', 'Name', 'required|trim|xss_clean|max_length[40]');
$this->form_validation->set_rules('dfemail', 'Email', 'required|trim|xss_clean|valid_email|max_length[50]|callback__verifyemail');
$this->form_validation->set_rules('dfmsg', 'dfmsg', 'trim|xss_clean');
$this->form_validation->set_error_delimiters('<br /><span class="error">', '</span>');
if ($this->form_validation->run() == FALSE) // validation hasn't been passed
{
$this->load->view('contact_v');
}
else
{
$form_data = array(
'dfname' => set_value('dfname'),
'dfemail' => set_value('dfemail'),
'dfmsg' => set_value('dfmsg')
);
if ($this->contact_m->SaveForm($form_data) == TRUE)
{
redirect('contact/success');
}
else
{
echo 'An error occurred saving your information. Please try again later';
}
}
}
function success()
{
echo 'this form has been successfully submitted with all validation being passed. All messages or logic here. Please note
sessions have not been used and would need to be added in to suit your app';
}
public function verifyemail(){
$name = $this->input->post('dfName');
$pass = $this->input->post('dfemail');
if($this->contact_m->email_exist($name,$pass)){
if ($this->session->userdata('site_lang') == 'portuguese')
{
$this->form_validation->set_message('_verifyuser','Usuario Inexistente!');
}else{
$this->form_validation->set_message('_verifyuser','User Not Found!');
}
$this->index();
return false;
}else{
return true;
}
}
}
?>
我的觀點:
<div class="container">
<div class="row">
<div class=" col-md-4 col-md-offset-4">
<?php echo br(2)?>
<div class="account-wall drop-shadow">
<?php
$title = $this->my_library->my_title($this->session->userdata('site_lang'),FORM_CONTACT);
echo '<h1 class="text-center login-title">'. $title. '</h1>';
echo br(1)
?>
<?php
$attributes = array('class' => 'form-contact', 'id' => 'myform2');
echo form_open('contact/myprocess',$attributes);
?>
<p>
<label for="dfname">Name</span></label>
<?php echo form_error('dfname'); ?>
<input id="dfname" type="text" class="form-control" placeholder="Name" name="dfname" maxlength="40" value="<?php echo set_value('dfname'); ?>" autofocus />
<?php echo br(1)?>
</p>
<p>
<label for="dfemail">Email</span></label>
<?php echo form_error('dfemail'); ?>
<input id="dfemail" type="text" class="form-control" placeholder="Email" name="dfemail" maxlength="50" value="<?php echo set_value('dfemail'); ?>" />
<?php echo br(1)?>
</p>
<p>
<label for="dfmsg">Message</label>
<?php echo form_error('dfmsg'); ?>
<?php echo form_textarea(array('name' => 'dfmsg', 'rows' => '4', 'cols' => '43', 'value' => set_value('dfmsg')))?>
<?php echo br(1)?>
</p>
<p>
<?php echo form_submit('submit', 'Submit'); ?>
</p>
<?php form_close();?>
</div>
</div>
</div>
</div>
<?php echo br(5)?>
我找不到$ this-> load-> library('form_validation');在你的代碼 –
我加載在我的autoloader.php,就像我說它是爲我的登錄表單工作,但不是爲這個。即使我添加到我的控制器:function __construct() { parent :: __ construct(); $ this-> load-> library('form_validation'); \t \t $ this-> load-> database(); \t \t $ this-> load-> helper('form'); \t \t $ this-> load-> helper('url'); \t \t $ this-> load-> model('contact_m'); }不會工作,如果我拿出我的 –
我沒有看到任何代碼應該將您重定向到主頁。你的application/config/routes.php設置是否正確? – versalle88