我正在用 codeIgniter 构建一个表单。我的表单中有一些必填字段,当必填字段为空白时,表单会转到同一页面。我想要做的是,当必填字段为空时,它会提示一个警报,说该字段是必填的。由于我是编程新手,我发现很难执行此功能。谁能告诉我我该怎么做。谢谢。仅供参考,表单的其他功能很好。
我的控制器的一部分:
function index() {
$this->form_validation->set_rules('address', 'address', 'required');
$this->form_validation->set_rules('area', 'area', '');
$this->form_validation->set_rules('lat', 'latitude', 'required');
$this->form_validation->set_rules('lng', 'longitude', 'required');
$this->form_validation->set_rules('subject', 'subject', 'required');
$this->form_validation->set_rules('problem', 'problem detail', 'required');
// validation hasn't been passed
if ($this->form_validation->run() == FALSE)
{
$this->load->view('report_view',$data );
}
else // passed validation proceed to post success logic
{
///do something.....
}
我的部分观点:
<?php // Change the css classes to suit your needs
$attributes = array('class' => '', 'id' => '');
echo form_open_multipart('report', $attributes);
?>
<p>
<br/>
<label for="address">Address <span class="required">*</span></label>
<?php echo form_error('address'); ?>
<br />
<input id="address" type="text" name="address"
value="<?php echo set_value('address'); ?>" />
</p>
<p>
<label for="area">Area </label>
<?php echo form_error('area'); ?>
<br />
<input id="area" type="text" name="area"
value="<?php echo set_value('area'); ?>" />
</p>
<p>
<label for="lat">Latitude<span class="required">*</span></label>
<?php echo form_error('lat'); ?>
<br />
<input id="lat" type="text" name="lat"
value="<?php echo set_value('lat'); ?>" />
<p>
<label for="lng">Longitude<span class="required">*</span></label>
<?php echo form_error('lng'); ?>
<br />
<input id="lng" type="text" name="lng"
value="<?php echo set_value('lng'); ?>" />
</p>
<p>
<label for="subject">Subject:<span class="required">*</span></label>
<?php echo form_error('subject'); ?>
<br />
<input id="subject" type="text" name="subject"
value="<?php echo set_value('subject'); ?>" />
</p>
<p>
<label for="problem">Problem detail:<span class="required">*</span></label>
<?php echo form_error('problem', '</div>'); ?>
<br />
<textarea id="problem" style="width:300px; height:80px"
type="text" name="problem"><?php echo set_value('problem'); ?>
</textarea>
</p>
我的模型:
function SaveForm($form_data)
{
$this->db->insert('info', $form_data);
if ($this->db->affected_rows() == '1')
{
return TRUE;
}
return FALSE;
}