【问题标题】:php codeigniter form validation with ajax failing带有ajax的php codeigniter表单验证失败
【发布时间】:2011-06-23 15:12:13
【问题描述】:

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

【问题讨论】:

  • php脚本运行时的输出是什么?它是否将表单保存在数据库中并立即返回 1?

标签: php ajax codeigniter validation


【解决方案1】:

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

<?=fname?>

你正在通过这一行:

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

【讨论】:

  • 我已经改过了,是错误的 fname 不是 =$fname?> 如果我省略所有字段,验证通过
猜你喜欢
  • 2018-12-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-11
  • 1970-01-01
  • 1970-01-01
  • 2016-05-08
相关资源
最近更新 更多