使用ajax失败的php codeigniter表单验证

时间:2011-06-23 15:12:14

标签: php ajax codeigniter validation

我正在尝试验证表单并将其保存在我的数据库中。我正在使用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=

的内容

1 个答案:

答案 0 :(得分:0)

我一直在盯着这看,不明白为什么它不起作用。当其他字段被省略或是否严格限于这3个字段时,您是否也会遇到意外行为?我看到的唯一“奇怪”,也许这是保持form_validation库正确加载,是

<?=fname?>

你正在传递这一行:

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