如何从SQL查询中检索数据并在我的javascript函数中使用它

时间:2014-10-16 10:23:14

标签: javascript sql ajax json codeigniter

我有一个这样的表单(在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)

我想知道:

  1. 这样做的正确方法是什么? (我听说过使用ajax,但欢迎使用任何解决方案。)
  2. 正确的语法和代码展示位置是什么?

1 个答案:

答案 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')));
    }