我是codeigniter的新手,并创建了将数据发送到邮件的表单。 现在我正在进行表单验证。 我想如果任何人单击提交按钮而不填写表单数据,则错误应显示在文本框中的占位符中。 这是我在视图中的contact_us_view页面
<?php
$form=array('class'=>'contact-form default-form','id'=>'myform');
echo form_open('success_form',$form);
?>
<div class="row">
<div class="col-md-12">
<div class="preamble col-md-12">
<h3>Get in touch</h3>
</div>
</div>
<div class="col-md-6">
<?php
$name=array('placeholder'=>'Name','name'=>'name');
echo form_input($name);
?>
</div>
<div class="col-md-6">
<?php
$company=array('placeholder'=>'Company','name'=>'company');
echo form_input($company);
?>
</div>
<div class="col-md-6">
<?php
$number=array('placeholder'=>'Phone Number','name'=>'number');
echo form_input($number);
?>
</div>
<div class="col-md-6">
<?php
$email=array('placeholder'=>'Email','name'=>'email');
echo form_input($email);
?>
</div>
<div class="col-md-6">
<span class="arrival calendar">
<?php
$arrival=array('name'=>'arrival','placeholder'=>'Arrival','data-dateformat'=>'m/d/y');
echo form_input($arrival);
?>
<i class="fa fa-calendar"></i>
</span>
</div>
<div class="col-md-6">
<span class="departure calendar">
<?php
$departure=array('name'=>'departure','placeholder'=>'Departure','data-dateformat'=>'m/d/y');
echo form_input($arrival);
?>
<i class="fa fa-calendar"></i>
</span>
</div>
<div class="col-md-12">
<span class="select-box" title="hotel-type">
<select name="person" data-placeholder="Number of People">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="10+">10+</option>
</select>
</span>
</div>
<div class="col-md-12">
<textarea placeholder="How we can help you"></textarea>
</div>
<div class="col-lg-md text-center">
<button class="btn btn-transparent-white">Send Message</button>
</div>
</div>
<?php echo form_close();?>
这是控制器中的控制器success_form
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Success_form extends CI_Controller{
public function __construct() {
parent::__construct();
$this->load->library(array('session', 'form_validation', 'email'));
}
public function index(){
// Set Validation Rule
$this->form_validation->set_rules('name','Name','trim|required|xss_clean|callback_alpha_space_only','required');
$this->form_validation->set_rules('company','Company','trim|required|xss_clean|callback_alpha_space_only');
$this->form_validation->set_rules('number','Mobile Number','required|regex_match[/^[0-9]{10}$/]');
$this->form_validation->set_rules('email', 'Emaid ID', 'trim|required|valid_email');
$this->form_validation->set_rules('arrival','Arrival','trim|required|xss_clean');
$this->form_validation->set_rules('departure','Departure','trim|required|xss_clean');
$this->form_validation->set_rules('person','Number of Person','trim|required|xss_clean|numeric');
//Run Validation on Form Input
if($this->form_validation->run()==FALSE){
// Validation fails
$this->load->view('contact_us_view');
}
else{
echo "successfully";
}
}
}
如果没有填写表格,如果任何一个点击提交按钮,那么错误应该显示在表格的占位符中。 请帮我找出答案..我试了很多但没有得到解决方案..请帮助
答案 0 :(得分:1)
在您的contact_us_view中,在您需要添加的<form>
之前
显示所有错误
<?php echo validation_error();?>
显示个别错误
<?php echo form_error('name')?>
<?php echo form_error('comapny')?>
答案 1 :(得分:0)
使用form_error('fieldname')
显示错误。
例如,显示字段公司错误。
<?php
$company = array('placeholder' => !empty(form_error('company')) ? form_error('company') : 'Company' ,'name' => 'company');
echo form_input($company);
?>