php代码点火器表单验证与ajax失败


php codeigniter form validation with ajax failing

我正在尝试验证一个表单并将其保存在数据库中。我正在使用codeigniter的验证库,但它失败了。

这是视图中的ajax函数

function save_form(){   
    $.ajax({
        url: '/projects/cafe/index.php/welcome/save_form',
        type: 'post',
        data: {'fname' : $('#fname').val(),
                'lname' : $('#lname').val(),
                'story_name' : $('#story_name').val(),
                'email' : $('#email').val(),
                'address_street' : $('#address_street').val(),
                'city' : $('#city').val(),
                'state' : $('#state').val(),
                'zip' : $('#zip').val(),
                'phone1' : $('#phone1').val(),
                'phone2' : $('#phone2').val(),
                'phone3' : $('#phone3').val(),
                'age_18' : $('#age_18').val(),
                'ci_csrf_token' : $('input[name=ci_csrf_token]').val()},
        success: function(data, textStatus, jqXHR){
            //Redirect user to next page here...
            if(data == '1'){
                location.href = 'http://www.voltage.com/projects/cafe/index.php/welcome/create4';
            }else{
                alert('form save failed');
            }
        },
        error: function(jqXHR, textStatus, errorThrown){
            console.log('error: '+jqXHR);
            console.log(textStatus);
            console.log(errorThrown);
        }
    });
} 

这是我的控制器中的功能

class Welcome extends MY_Controller {
public function save_form(){
        $this->load->helper(array('form', 'url'));
        $this->load->library('form_validation');
        $this->form_validation->set_rules('fname', 'First Name', 'required|min_length[6]|max_length[12]');
        $this->form_validation->set_rules('lname', 'Last Name', 'required');
        $this->form_validation->set_rules('story_name', 'Story Name', 'required');
        $this->form_validation->set_rules('email', 'email', 'required');
        $this->form_validation->set_rules('address_street', 'Street Address', 'required');
        $this->form_validation->set_rules('city', 'City', 'required');
        $this->form_validation->set_rules('state', 'State', 'required');
        $this->form_validation->set_rules('zip', 'Zip', 'required');
        $this->form_validation->set_rules('phone1', 'Area Code', 'required');
        $this->form_validation->set_rules('phone2', 'Phone Number', 'required');
        $this->form_validation->set_rules('phone3', 'Phone Number', 'required');
        $this->form_validation->set_rules('age_18', 'Age Verification', 'required');
        if($this->form_validation->run() == FALSE)
            die();
        $this->load->model('user');
        $results = $this->user->save_form($this->session->userdata('fbid'),
                                            $this->input->post());
        echo $results;
    }

我感到困惑的是,如果我没有phone1、phone2或phone3的任何必需值,if($this->form_validation->run() == FALSE)是如何传递的。

        $this->form_validation->set_rules('phone1', 'Area Code', 'required');
        $this->form_validation->set_rules('phone2', 'Phone Number', 'required');
        $this->form_validation->set_rules('phone3', 'Phone Number', 'required');

这就是发送到服务器fname=First&lname=Last&story_name=Your+Story&email=yourname%40domain.com&address_street=Street&city=City&state=State&zip=Zip&phone1=&phone2=&phone3=&age_18= 的内容

我一直盯着这个看,不明白为什么它不起作用。当其他字段被省略时,你是否也会出现意外行为,或者它严格限制在这3个字段内?我看到的唯一"奇怪"的是,也许这是为了阻止form_validation库正确加载

<?=fname?>

你在这条线上经过:

$this->form_validation->set_rules('<?=$fname?>', 'First Name', 'required|min_length[6]|max_length[12]');