我试图在 jquery ajax 的帮助下在 codeigniter 中创建一个表单,但我似乎在使它工作时遇到了问题
我想做的是……
创建一个表单,该表单将在将数据插入数据库之前检查用户是否填写了所有必要的字段
如果有一个字段不包含任何答案,则会出现一个 div 说明哪些字段需要回答
如果所有字段都填满,表单将提交,更新数据库,然后会出现一个 div 表示表单已成功提交
这是我的代码
控制器
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Site extends CI_controller {
public function __construct()
{
parent::__construct();
// Your own constructor code
$this->load->model("site_model");
//$cms_db = $this->load->database('cms', TRUE); /* database conection for write operation */
$site_db = $this->load->database('default', TRUE);
}
public function index()
{
//$this->load->helper('form');
$data = $this->_initialize_data();
$data['about'] = $this->site_model->get_about();
$this->load->vars($data);
$this->load->view('site/index');
}
public function learner()
{
$this->site_model->submit_learner();
redirect(base_url() . "site/index");
}
public function _initialize_data()
{
$data['cp'] = $this->site_model->get_cp();
$data['op'] = $this->site_model->get_op();
$data['learnedu'] = $this->site_model->get_learnedu();
$data['learnals'] = $this->site_model->get_learnals();
$data['learnbrgy'] = $this->site_model->get_learnbrgy();
$data['learnben'] = $this->site_model->get_learnben();
return $data;
}
}
模型
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
// CI 2.0 Compatibility
if(!class_exists('CI_Model')) { class CI_Model extends Model {} }
class Site_model extends CI_Model
{
function submit_learner()
{
//$this->load->helper('date');
$cms_db = $this->load->database('cms', TRUE);
$fname = $this->input->post("fname");
$mname = $this->input->post("mname");
$lname = $this->input->post("lname");
$ename = $this->input->post("ename");
$bday = $this->input->post("bday");
$opt = $this->input->post("opt");
$educ = $this->input->post("educ");
$als = $this->input->post("als");
$brgy = $this->input->post("brgy");
$guardian = $this->input->post("guardian");
$relation = $this->input->post("relation");
$benefit = $this->input->post("benefit");
$newdata = array('first_name'=>$fname,
'middle_name'=>$mname,
'last_name'=>$lname,
'extension_name'=>$ename,
'bday'=>$bday,
'gender'=>$opt,
'education'=>$educ,
'als'=>$als,
'brgy'=>$brgy,
'guardian'=>$guardian,
'relationship'=>$relation,
'added_by'=>'OTH',
'added_id'=>''
);
$cms_db->insert('records', $newdata);
$id = $cms_db->insert_id();
foreach($benefit as $b)
{
$ben = array('record_id'=>$id,'beneficiary_id'=>$b);
$cms_db->insert('is_beneficiary', $ben);
}
$date = date('Y-m-d g:ia');
$date_logged = array('action'=>'Added','action_date'=>$date,'activity'=>$fname . " " . $mname . " " . $lname . ' was added through the Abot Alam Website');
$cms_db->insert('date_logged', $date_logged);
}
}
看法
<?php
$attributes = array('class' => 'form-vertical','id'=>'form_learner','name'=>'form_learner');
echo form_open(base_url() . 'site/learner', $attributes);
?>
<div class="control-group">
<label class="control-label" >First Name</label>
<div class="controls">
<input type="text" class="input input-xlarge" name="fname" id="fname" placeholder="">
</div>
</div>
/*-------- Theres a bunch of other fields here that asks things like last name, middle name but i decided to omit it to make the post shorter ----------------*/
<input type="submit" class="btn btn-primary" value="Save" placeholder="" name="submit_learner" id="submit_learner">
</form>
</div>
脚本.js
$("#submit_learner").click(function(e) {
e.preventDefault();
fname = $("#fname").val();
mname = $("#mname").val();
lname = $("#lname").val();
ename = $("#ename").val();
bday = $("#bday").val();
educ = $("#educ").val();
als = $("#als").val();
brgy = $("#brgy").val();
guardian = $("#guardian").val();
relation = $("#relation").val();
if(fname==''){
$("#learner_status").show();
$("#learner_status").html("<h4>Input First Name</h4>");
$("#fname").focus();
}
else if(mname==''){
$("#learner_status").show();
$("#learner_status").html("<h4>Input Middle Name</h4>");
$("#mname").focus();
}
else if(lname==''){
$("#learner_status").show();
$("#learner_status").html("<h4>Input Last Name</h4>");
$("#lname").focus();
}
else if(ename==''){
$("#learner_status").show();
$("#learner_status").html("<h4>Input Extention Name</h4>");
$("#ename").focus();
}
else if(bday==''){
$("#learner_status").show();
$("#learner_status").html("<h4>Input Birthdate</h4>");
$("#bday").focus();
}
else if(educ==''){
$("#learner_status").show();
$("#learner_status").html("<h4>Input Highest Formal Education</h4>");
$("#educ").focus();
}
else if(als==''){
$("#learner_status").show();
$("#learner_status").html("<h4>Input ALS Programs Involved</h4>");
$("#als").focus();
}
else if(brgy==''){
$("#learner_status").show();
$("#learner_status").html("<h4>Input Barangay</h4>");
$("#brgy").focus();
}
else if(guardian==''){
$("#learner_status").show();
$("#learner_status").html("<h4>Input Guradian</h4>");
$("#guradian").focus();
}
else if(relation==''){
$("#learner_status").show();
$("#learner_status").html("<h4>Input Relationship to Guardian</h4>");
$("#relation").focus();
}
else{
var datastr = 'fname='+fname + '&mname='+mname + '&lname='+lname + '&ename='+ename + '&bday='+bday + '&educ='+educ + '&als='+als + '&brgy='+brgy + '&guardian='+guardian + '&relation='+relation;
$.ajax({
url:"<?php echo base_url(); ?>site/learner",
type:'POST',
data:datastr,
dataType:"json",
success:function(result){
$("#learner_status").show();
$("#learner_status").attr('class', 'span 12 alert alert-success');
$("#learner_status").html("<h4>Form Successfully Submitted</h4>");
}
});
}
});
我有一种感觉,错误在我的 javascript 中的某个地方
特别是这条线
$.ajax({
url:"<?php echo base_url(); ?>site/learner",
如果我错了,请纠正我,但据我所知,你不能在脚本标签中使用 php 标签
但这就是人们在互联网上看到的其他示例中的做法
我错过了什么吗?
谢谢
编辑:顺便说一句,我忘了提到我的代码的问题是它没有插入数据库。