Php 表单验证困难

Php 表单验证困难,php,codeigniter,Php,Codeigniter,我正在用codeIgniter构建一个表单。我的表单中有一些必填字段,当必填字段为空时,表单将转到同一页。我想做的是,当一个必填字段为空时,它将提示一个警报,说明这些字段是必填字段。由于我是编程新手,我发现很难实现这个功能。谁能告诉我怎么做。谢谢仅供参考,表单的其他功能很好 我的控制器的一部分: function index() { $this->form_validation->set_rules('address', 'address', 'required');

我正在用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>

我找到了关于jquery和ci表单验证的参考资料

好像第一个链接比其他链接好


  • 您应该从jquery调用控制器函数,在调用该函数之前,请确保使用
    if($(“#textbox”).val().length==0验证文本框是否为空,并提示一个警告框某个字段为空
    0表示文本框没有值,并且为空。在此之后,您可以使用测试示例调用控制器函数
    document.location.href='

    您的控制器文件:

    <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    
    class Welcome extends CI_Controller {
    
        public function index()
        {
            $this->load->library('session');
            $this->load->helper('url');
            $this->load->library('form_validation');
    
            if ($this->input->server('REQUEST_METHOD') == 'POST') { // make sure it's post request
    
                $this->form_validation->set_error_delimiters('', '');
    
                $this->form_validation->set_rules('address', '<b>address</b>', 'required|xss_clean');
                $this->form_validation->set_rules('area', '<b>area</b>', 'xss_clean');
                $this->form_validation->set_rules('lat', '<b>lat</b>', 'required|xss_clean');
                $this->form_validation->set_rules('lng', '<b>lng</b>', 'required|xss_clean');
                $this->form_validation->set_rules('subject', '<b>subject</b>', 'required|xss_clean');
                $this->form_validation->set_rules('problem', '<b>problem</b>', 'required|xss_clean');
    
                if ($this->form_validation->run() !== FALSE) { // validation has passed
    
                    // insert the form data into database
                    $this->db->insert('info', $form_data);
    
                    // do other stuff
    
                    redirect('/index.php/success-page');
                } else {
                    $this->session->set_userdata($this->session->flashdata_key . ':old:error', 'We have some form error. Please review them!');
                }
            }
    
            $this->load->view('welcome_message');
        }
    }
    

    您可以使用javascript在客户端(HTML页面)验证表单字段-

    <script>    
    function yourfunctionname()
          {
            if(document.formname.textfieldname.value=="")
                {
                    alert("text field is required..");
                    document.formname.textfieldname.focus();
                    return false;
                }
          }
    </script>
    
    
    函数yourfunctionname()
    {
    if(document.formname.textfieldname.value==“”)
    {
    警报(“需要文本字段…”);
    document.formname.textfieldname.focus();
    返回false;
    }
    }
    
    请添加更多详细信息:什么对您不起作用以及原因?您好,我想做的是在必填字段未填写时提示一个警报,而不是返回同一表单。你能告诉我在字段未填充时如何发出警报吗。谢谢你想要像jquery一样的验证吗?是的,jquery可以。但是我如何将jquery与这段代码集成。谢谢如果你使用jquery进行验证,那么也可以使用codeigniter验证来验证它。永远不要只相信javascript验证,因为它可以很容易地通过!他没有要求jQuery,显然他不明白它有什么含义。
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="utf-8">
        <title>Welcome to CodeIgniter</title>
    
        <style type="text/css">
    
        body {
            background-color: #fff;
            margin: 40px;
            font: 13px/20px normal Helvetica, Arial, sans-serif;
            color: #4F5155;
        }
        </style>
    </head>
    <body>
        <form method="post">
    
            <?php if ($this->session->flashdata('error') != '') { ?>
                <?php echo $this->session->flashdata('error');?><br />
            <?php } ?>
    
            <label>address</label>
            <input type="text" name="address" value="<?php echo set_value('address');?>" /><br />
            <?php if (form_error('address')) { ?><?php echo form_error('address');?><br /><?php } ?>
    
            <label>area</label>
            <input type="text" name="area" value="<?php echo set_value('area');?>" /><br />
            <?php if (form_error('area')) { ?><?php echo form_error('area');?><br /><?php } ?>
    
            <label>lat</label>
            <input type="text" name="lat" value="<?php echo set_value('lat');?>" /><br />
            <?php if (form_error('lat')) { ?><?php echo form_error('lat');?><br /><?php } ?>
    
            <label>lng</label>
            <input type="text" name="lng" value="<?php echo set_value('lng');?>" /><br />
            <?php if (form_error('lng')) { ?><?php echo form_error('lng');?><br /><?php } ?>
    
            <label>subject</label>
            <input type="text" name="subject" value="<?php echo set_value('subject');?>" /><br />
            <?php if (form_error('subject')) { ?><?php echo form_error('subject');?><br /><?php } ?>
    
            <label>problem</label>
            <input type="text" name="problem" value="<?php echo set_value('problem');?>" /><br />
            <?php if (form_error('problem')) { ?><?php echo form_error('problem');?><br /><?php } ?>
    
            <input type="submit" value="Submit">
    
        </form>
    </body>
    </html>
    
    <script>    
    function yourfunctionname()
          {
            if(document.formname.textfieldname.value=="")
                {
                    alert("text field is required..");
                    document.formname.textfieldname.focus();
                    return false;
                }
          }
    </script>