开发者

php codeigniter form validation with ajax failing

I am trying to validate a form and save it in my database. I am using codeigniter's validation library and it fails.

this is the ajax function inside the view

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);
        }
    });
} 

this is the function in my controller

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'),
            开发者_Python百科                                $this->input->post());

        echo $results;
    }

What I'm confused about is how if($this->form_validation->run() == FALSE) passes if I don't have any values for phone1, phone2, or phone3, which are 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 is what being sent to the server 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=


I've been staring at this and can't see why it wouldn't work. Are you also getting unexpected behavior when other fields are omitted or is it strictly limited to these 3 fields? The only thing "odd" I see, and maybe this is keeping the form_validation library from loading correctly, is the

<?=fname?>

you are passing in this line:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜