我有一个这样的表单(在CodeIgniter View页面中):
<form action="<?php echo base_url(); ?>index.php/admin_logins/regis2" onsubmit="return checkTime();" method="post">
<fieldset>
<legend id="registration_legend">Registration</legend>
<div>
<label id= "std_id_add_label">Student ID:</label>
<input type="text" name="std_id" id = "student_id_add" placeholder="Student ID" required="1"/>
</div>
<br>
<div>
<label id= "is_member_add_label">Member?</label>
<input type="checkbox" name ="is_member_checkbox" class ="membercheck" checked="checked" value="1" onchange="valueChanged()"/>
</div>
<br>
<div id = "privilege_code">
<label id= "privilege_code_add_label">Privilege Code:</label>
<input type="text" name="privilege_code" id = "privilege_code_add" placeholder='0'/>
</div>
</form>
我有一个像这样的Javascript:
<script>
function checkTime()
{
var today = new Date();
var hour = today.getHours();
if(hour < '17')
{
return true;
}
else
{
alert("Sorry, registrations are closed after 5 pm.");
location.href = '<?php echo base_url(); ?>index.php/admin_logins/registration';
return false;
}
}
</script>
我想做的就是这样:
<script>
function checkTime()
{
var today = new Date();
var hour = today.getHours();
//alert(hour);
if(hour < '17')
{
//well I want something like the following
query = select count(std_id) from registration where std_id = document.getElementById("student_id_add") and date = today
if(query > 1)
{
alert("You are already registered.");
return false;
}
else
{
return true;
}
}
//don't worry about the above syntax, I know it's wrong and I'm looking for the right stuff
else
{
alert("Sorry, registrations are closed after 5 pm.");
location.href = '<?php echo base_url(); ?>index.php/admin_logins/registration' ;
return false;
}
}
</script>
如果提交时std_id
的次数超过一次,则表示如果现有std_id
,则不会提交数据。
在表单提交上,数据将转到此控制器方法 -
function registration_add
{
if($_POST)
{
date_default_timezone_set('Asia/Dacca');
$rdata['std_id'] = $this->input->post('std_id');
$rdata['entry_date'] = date('Y-m-d');
$rdata['is_member'] = $this->input->post('is_member_checkbox');
$rdata['privilege_code'] = $this->input->post('privilege_code');
$res = $this->registration_model->insert_registration($rdata);
if($res)
{
$this->session->set_flashdata('message','Registration information added successfully');
header('location:'.base_url()."index.php/admin_logins/regis");
}
}
else
{
$this->load->view('admin_logins/registration_add');
}
}
模型方法 -
public function insert_registration($data)
{
return $this->db->insert('registration', $data);
}
我的数据库是MySQL,我的表名是registration
,结构如下:
Columns Data Types
id int PK
std_id varchar(15)
entry_date date
is_member int
privilege_code varchar(12)
我想知道:
答案 0 :(得分:0)
可能为时已晚,但我认为它会对你有帮助..
这只是一个ajax请求。 在视图文件
中jQuery.ajax({
type : "POST",
url : "<?php echo site_url('route_name'); ?>",
data : {
"key" : value
},
success: function(result,status,xhr){
if(result.length > 0) {
msg = JSON.parse(result);
// do your work here.
},
error: function(xhr,status,error){
alert('Error Occurred.');
}
});
在控制器中
// AJAX
function ajax_function()
{
$this->load->model('model_name');
echo json_encode($this->model_name->model_function($this->input->post('key')));
}