Php 如果表单值通过jquery插入,如何验证表单输入字段?

Php 如果表单值通过jquery插入,如何验证表单输入字段?,php,jquery,codeigniter,Php,Jquery,Codeigniter,视图: 型号: public function register() { $register = $this->Fetch_data->contact(); if(!empty($register_user)) { return true; } else { return false; } } 在这段代码中,我在模式中创建了一个表单,在这里我将表单值插入数据库表name contact。此处,

视图:

型号:

public function register()
{
    $register  = $this->Fetch_data->contact();
    if(!empty($register_user))
    {
        return true;
    }
    else
    {
        return false;
    }
}
在这段代码中,我在模式中创建了一个表单,在这里我将表单值插入数据库表name contact。此处,表单值插入成功,但验证工作不正常。如果输入字段为空,则不显示任何内容,如字段为必填项。那么,我们如何对所有字段进行验证请帮助我

谢谢

试试这个

替换为

public function contact()
  {
     $data = array(
        'name'  => $this->input->post('name'),
        'email' => $this->input->post('email'),
        'phone' => $this->input->post('phone'),
        'message' => $this->input->post('message'),
        's_date' => date('Y-m-d')
     );
     $this->db->insert('contact', $data);
  }
模型

如果你有任何错误,请告诉我

注意:启用表单验证库

试试这个

替换为

public function contact()
  {
     $data = array(
        'name'  => $this->input->post('name'),
        'email' => $this->input->post('email'),
        'phone' => $this->input->post('phone'),
        'message' => $this->input->post('message'),
        's_date' => date('Y-m-d')
     );
     $this->db->insert('contact', $data);
  }
模型

如果你有任何错误,请告诉我

注意:启用表单验证库


使用phpy使用后端验证您应该在前端和后端进行验证,我已经为所有输入字段提供了必需的验证,但它们也不起作用。为什么?您可以在客户端使用
jQuery
PHP
服务器端进行验证(以防万一,用户在其浏览器中禁用javascript)使用phpy使用后端验证您应该在前端和后端进行验证,我已经为所有输入字段提供了必需的验证,但它们也不起作用。为什么?您可以在客户端使用
jQuery
PHP
服务器端进行验证(以防万一,用户在其浏览器中禁用javascript)
<script>
    $(function(){
        $( "#insert").click(function(event)
        {
            event.preventDefault();

            var name= $("#name").val();
            var email= $("#email").val();
            var phone= $("#phone").val();
            var message= $("#message").val();
            $.ajax(
                {
                    type:"post",
                    url: "<?php echo base_url('index.php/'); ?>test/register",
                    dataType:'JSON',
                    data:{"name": name, "email":email, "phone":phone, "message":message},
                    success:function(data)
                    {
                        console.log(data);
                        $('#msd').text(data.message);
                    }
                });
        });
    });
</script>
public function register()
   {
      $this->load->library('form_validation');
      $post = $this->input->post();

      if (!empty($post)) {

//validate form input
         $this->form_validation->set_rules('name', 'Name', 'required');
         $this->form_validation->set_rules('email', 'E-mail', 'required');
         $this->form_validation->set_rules('phone', 'Phone', 'required');
         $this->form_validation->set_rules('message', 'Message', 'required');

         if ($this->form_validation->run() == true) {
            $register = $this->Fetch_data->contact($post);
            if (!empty($register)) {

               echo json_encode(array("status" => false, "message" => "Inserted Successfully"));
               exit(0);
            } else {
               echo json_encode(array("status" => false, "message" => "Unable to insert Try later"));
               exit(0);
            }
         } else {
            echo json_encode(array("status" => false, "message" => validation_errors()));
            exit(0);
         }
      } else {
         echo json_encode(array("status" => false, "message" => "Invalid Request"));
         exit(0);
      }
   }
public function contact($post)
   {
      $data = array(
                               'name' => $post['name'],
                               'email' => $post['email'],
                               'phone' => $post['phone'],
                               'message' => $post['message'],
                               's_date' => date('Y-m-d', time())
      );
      $this->db->insert('contact', $data);
      return $this->db->insert_id();
   }