我试图通过ajax在php codeigniter中插入文件上传和验证的作业表单数据,但它不起作用。作为参考,我的代码在下面提到..请帮助我。我可以将数据发送到带有文件的数据库。
SRC = “https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js” >
<?php echo form_open_multipart('MainController/submitjoinusForm', 'class="" id="joinusform"', $hidden = array());?>
<div class="messages">
<div class="col-md-12">
<div id="status"></div>
</div>
</div>
<div class="controls">
<div class="form-group">
<div class="col-md-12 col-sm-12">
<label for="contactname">Name</label>
<input id="contactname" name="name" class="form-control" required="required" placeholder="Enter your name *" data-error="Valid name required." value="" type="text">
<!-- <span class="help-block with-errors"></span> -->
</div>
</div>
<div class="form-group">
<div class="col-md-12 col-sm-12">
<label for="contactemail">Email Address</label>
<input id="contactemail" name="email" class="form-control" required="required" placeholder="Enter your email *" value="" type="email">
</div>
</div>
<div class="form-group">
<div class="col-md-12 col-sm-12">
<label for="form_phone">Phone Number</label>
<input id="contactphone" name="phone" class="form-control" required="required" placeholder="Enter your phone" value="" type="tel">
</div>
</div>
<div class="form-group">
<div class="col-md-12 col-sm-12">
<label for="form_message">How Can We Help?</label>
<textarea id="contactmessage" value="" name="message" class="form-control" required="required" placeholder="Message for us *" rows="5"
data-error="Leave us a message."></textarea>
<div class="help-block with-errors"></div>
</div>
</div>
<div class="form-group">
<div class="col-md-4 col-sm-4">
<?php echo $captcha['image']; ?>
</div>
<div class="col-md-4 col-sm-4">
<input type="text" id="contactcaptcha" value="" name="captcha" required="required" class="form-control" placeholder="Type Captcha...">
</div>
<div class="col-md-4 col-sm-4">
<input class="btn btn-secondary btn-send form-control" name="submit" value="Send message" type="submit">
</div>
</div>
</div>
</form>
function submitjoinusForm()
{
$response = array();
$oword = $this->session->userdata('captcha_key');
$tword = $this->input->post('captcha');
//form field validation rules
$this->form_validation->set_rules('fullname', 'Name', 'trim|required');
$this->form_validation->set_rules('dob', 'Date of Birth', 'trim|required');
$this->form_validation->set_rules('gender', 'Gender', 'trim|required');
$this->form_validation->set_rules('maritalstatus', 'Marital Status', 'trim|required');
$this->form_validation->set_rules('nationality', 'Nationality', 'trim|required');
$this->form_validation->set_rules('visastatus', 'Visa Status', 'trim|required');
$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
$this->form_validation->set_rules('contact', 'Contact', 'trim|required');
$this->form_validation->set_rules('captcha', 'Captcha', 'trim|required');
if ( ($this->form_validation->run() == TRUE) && (strtolower($oword) == strtolower($tword)) )
{
if($_FILES['file']['error'] != 0)
{
$response = array(
'status' => 'alert-danger',
'data'=> 'Please upload your CV file in pdf or word file, maximum size of 2MB.',
);
echo json_encode($response);
return FALSE;
}
else
{
$config['upload_path'] = './files/';
$config['allowed_types'] = 'pdf|doc|docx';
$config['max_size'] = 1024 * 3;
$config['encrypt_name'] = TRUE;
$this->load->library('upload',$config);
$this->upload->initialize($config);
if ($this->upload->do_upload('userfile'))
{
$datafile['upload_data'] = $this->upload->data('userfile');
$userfile_name = $datafile['upload_data'];
}
else
{
$userfile_name = '';
}
$data = array(
'fullname' => $this->input->post('fullname'),
'dob' => $this->input->post('dob'),
'gender' => $this->input->post('gender'),
'maritalstatus' => $this->input->post('maritalstatus'),
'nationality' => $this->input->post('nationality'),
'visastatus' => $this->input->post('visastatus'),
'email' => $this->input->post('email'),
'contact' => $this->input->post('contact'),
'file' => $userfile_name
);
$this->joinusModel->submitJoinus($data);
if($this->db->insert_id())
{
$response = array(
'status' => 'alert-success',
'data' => "You message has been sent successfully. We'll get back to you shortly",
);
echo json_encode($response);
}
else
{
$response = array(
'status' => 'alert-danger',
'data' => "Oops! Something went wrong while sending message to us.",
);
echo json_encode($response);
}
}
}
}
jQuery(document).ready(function($)
{
$('#joinusform').ajaxForm(
{
beforeSubmit: function(formData, jqForm, options)
{
$("div#status").html('');
},
$.ajax({
url : $("#joinusform").attr('action'),
dataType : 'json',
type : 'POST',
data : formData,
contentType : false,
processData : false,
});
success:function(respnse)
{
var result= $.parseJSON(respnse);
$("div#status").html('<div class="alert '+result.status+' alert-dismissable" role="alert"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>'+result.data+'</div>');
if (result.status == 'success')
{
$('#contactform').clearForm();
}
}
});
});
答案 0 :(得分:0)
我重新编写了一些代码,通过分离某些东西来使错误更明显。等等。我在此处发现了一个错误:$this->upload->data('userfile')
数据的可选参数应仅为file_name
或full_path
,而不是文件字段的名称。
https://www.codeigniter.com/userguide3/libraries/file_uploading.html#CI_Upload::data
我还首先通过测试错误消除了一些else
语句,然后如果出现这些错误就会退出,如您所见。如果这仍然失败,你应该取消注释我注释掉的第一行,看看你是否得到了你应该得到的输入。请注意,通过ajax发送的文件需要以某种方式处理:$_POST is not working in ajax form submit?。
function submitjoinusForm() {
//echo '<pre>';
//echo 'post data: <br>';
//print_r($_POST);
//echo 'files: <br>';
//print_r($_FILES);
$response = array();
$oword = $this->session->userdata('captcha_key');
$tword = $this->input->post('captcha');
//form field validation rules
$this->form_validation->set_rules('fullname', 'Name', 'trim|required');
$this->form_validation->set_rules('dob', 'Date of Birth', 'trim|required');
$this->form_validation->set_rules('gender', 'Gender', 'trim|required');
$this->form_validation->set_rules('maritalstatus', 'Marital Status', 'trim|required');
$this->form_validation->set_rules('nationality', 'Nationality', 'trim|required');
$this->form_validation->set_rules('visastatus', 'Visa Status', 'trim|required');
$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
$this->form_validation->set_rules('contact', 'Contact', 'trim|required');
$this->form_validation->set_rules('captcha', 'Captcha', 'trim|required');
if (!$this->form_validation->run()) {
echo json_encode(array('status' => 'alert-danger', 'data' => validation_errors('', '<br>')));
exit;
}
if (strtolower($oword) !== strtolower($tword)) {
echo json_encode(array('status' => 'alert-danger', 'data' => 'Incorrect captcha.'));
exit;
}
$config['upload_path'] = './files/';
$config['allowed_types'] = 'pdf|doc|docx';
$config['max_size'] = 1024 * 3;
$config['encrypt_name'] = TRUE;
$this->load->library('upload', $config);
$this->upload->initialize($config);
if (!$this->upload->do_upload('userfile')) {
$error = 'Please upload your CV file in pdf or word file, maximum size of 2MB. <br>';
$error .= $this->upload->display_errors('', '<br>');
echo json_encode(array('status' => 'alert-danger', 'data' => $error));
exit;
}
$data = array(
'fullname' => $this->input->post('fullname'),
'dob' => $this->input->post('dob'),
'gender' => $this->input->post('gender'),
'maritalstatus' => $this->input->post('maritalstatus'),
'nationality' => $this->input->post('nationality'),
'visastatus' => $this->input->post('visastatus'),
'email' => $this->input->post('email'),
'contact' => $this->input->post('contact'),
//https://www.codeigniter.com/userguide3/libraries/file_uploading.html#CI_Upload::data
'file' => $this->upload->data('file_name')
);
$this->joinusModel->submitJoinus($data);
if ($this->db->insert_id()) {
$response = array(
'status' => 'alert-success',
'data' => "You message has been sent successfully. We'll get back to you shortly.",
);
echo json_encode($response);
} else {
$response = array(
'status' => 'alert-danger',
'data' => "Oops! Something went wrong while sending a message to us.",
);
echo json_encode($response);
}
}