在我提交空表单的时候,它会在我的数据库中插入一个空的raw。我也在使用CodeIgniter表单验证。如何使用CodeIgniter避免这种情况。请帮帮我。 ex-如果从空是没有提交。
$this->form_validation->set_rules('name', 'Name', 'trim|required|min_length[5]');
// validate name
$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
// validate email
$this->form_validation->set_rules('phone', 'Phone Number', 'trim|required|min_length[10]|max_length[10]');
// validate phone number
$this->form_validation->set_rules('comment', 'Questions or Comments', 'required|min_length[10]');
// validate phone number
if ($this->form_validation->run() == FALSE)// if from validation failed load following pages from views
{
$this->load->view('template/header.php');
$this->load->view('pages/contacts');
$this->load->view('template/footer.php');
}
else // if from validation success load following pages from views
{
$this->load->view('template/header.php');
$data['result']="Thank you for contacting us! We will reply you soon... ";
$this->load->view('pages/contacts',$data);
$this->load->view('template/footer.php');
}
我也在使用必要的验证规则,请帮我解决这个问题
<?php echo form_open('form/contact') ?><!--Creates an opening form
ex :<form method="post" accept-charset="utf-8" action="localhost/index.php/form(controller)/subscribe(function)" />-->
<!-- Contact form Start -->
<div class="span7">
<div class="box">
<?php echo validation_errors('<div class="notice marker-on-bottom bg-darkRed fg-white" id="bottom_form_error">', '</div>'); ?>
<?php if(isset($result)){echo '<div class="notice marker-on-bottom bg-green fg-white">'.$result.'</div>';}?>
<!-- Display Validation error massages -->
<h6>*All fields marked are required</h6>
<lable>Your name*</lable>
<div class="input-control text" data-role="input-control">
<input type="text" name="name" value="<?php echo set_value('name'); ?>" placeholder="type your name">
<button class="btn-clear" tabindex="-1"></button>
</div>
<lable>Email*</lable>
<div class="input-control text" data-role="input-control">
<input type="text" name="email" value="<?php echo set_value('email'); ?>" placeholder="type email address">
<button class="btn-clear" tabindex="-1"></button>
</div>
<lable>Phone*</lable>
<div class="input-control text" data-role="input-control">
<input type="text" name="phone" value="<?php echo set_value('phone'); ?>" placeholder="type phone number">
<button class="btn-clear" tabindex="-1"></button>
</div>
<lable>Questions or Comments*</lable>
<div class="input-control textarea" data-role="input-control">
<textarea name="comment" value="<?php echo set_value('comment'); ?>" >...</textarea>
</div>
<input name='contactus_status' type='hidden' value='no'/>
<input type="submit" value="Submit" class="info">
</div>
</div>
<?php echo form_close(); ?><!-- Contact form End -->
答案 0 :(得分:1)
您正在寻找必需的Codeigniter服务器端验证
$this->form_validation->set_rules('username', 'Username', 'required');
完整文档Form validation codeigniter
如果您需要客户端验证jQuery Validation Plugin
以下是Jquery表单验证的Sample Demo
答案 1 :(得分:0)
您可以使用JavaScript阻止仅在客户端进行实际提交,但是使用该方法您无法确定是否提交了表单(因为可能会混淆客户端脚本)。相反,您必须验证提交的值。
答案 2 :(得分:0)
我似乎你的MySQL代码在if else语句的验证之外。一定要把它放在正确的位置。
以下是您样本中缺失的数据。
if ($this->form_validation->run() == FALSE){
}else{
// Be sure to put your database insert code here...
$data['name'] = set_value('name');
$data['email'] = set_value('email');
$data['phone'] = set_value('phone');
$data['comment'] = set_value('comment');
$this->db->insert('table_name',$data);
}