我想驗證表單並將其保存在我的數據庫中。我使用codeigniter的驗證庫,它失敗。PHP 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);
}
});
}
裏面的AJAX功能,這是在我的控制器功能
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;
}
我感到困惑的是如何if($this->form_validation->run() == FALSE)
傳球,如果我沒有任何phone1,phone2或phone3的值,這是必需的。
$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? – tgriesser