dongzai0020 2015-05-04 16:27
浏览 42
已采纳

按下按钮时的Codeigniter更新表

I am trying to use Codeigniter to create an application where when a button is pressed that it'll add 1 to an INT column in a table.

There is 3 buttons and depending on which button is pressed it'll update one of the 3 columns adding the value.

I've got so when a user logs in that the username and password they log in with is set into the table with the inputs.

Table:

+--+--------+--------+--------+--------+----------+
|ID|Password|Username|inputOne|inputTwo|inputThree|
+--+--------+--------+--------+--------+----------+
|  |        |        |        |        |          |
|  |        |        |        |        |          |
+-------------------------------------------------+

After the user is logged in this should be recorded

+--+--------+--------+--------+--------+----------+
|ID|Password|Username|inputOne|inputTwo|inputThree|
+--+--------+--------+--------+--------+----------+
|1 |Pass    |User1   |        |        |          |
|  |        |        |        |        |          |
+-------------------------------------------------+

I only need it to update the column depending on which button is being pressed. The other columns should not add. Until that button is pressed.

I am also trying to do it so that the page doesn't redirect when the button is pressed.

The Process:

View

    <script src="<?php echo site_url('application/views/js/buttons.js')?>"></script>
    <form method="post">
     <input class="btn btn-lg btn-success" type="submit" id="inputOne" value="Button One"><br><br>
     <input class="btn btn-lg btn-primary" type="submit" id="inputTwo" value="Button Two"><br><br>
     <input class="btn btn-lg btn-danger" type="submit" id="inputThree" value="Button One">
   </form>

buttons.js

$(document).ready(function(){

$("#inputOne").click(function()
{
 $.ajax({
     type: "POST",
     url: "<?php echo base_url(); ?>index.php/home/view",
     data: {"1"},
     success:
          function(){
            sleep(5);  //STOP POST
          }
      });
 return false;
});

$("#inputTwo").click(function()
{
 $.ajax({
    type: "POST",
    url: "<?php echo base_url(); ?>index.php/home/view",
    data: {"1"},
    success:
         function(){
           sleep(5);  //STOP POST
         }
     });
 return false;
});

$("#inputThree").click(function()
{
 $.ajax({
   type: "POST",
   url: "<?php echo base_url(); ?>index.php/home/view",
   data: {"1"},
   success:
        function(){
          sleep(5);  //STOP POST
        }
    });
 return false;
});


});

Controller

public function buttons() {
 if (!$this->session->userdata('username')){
  redirect (base_URL(). 'index.php/login/view');
 } else {
  if (!$this->session->userdata('password')) {
   redirect (base_URL(). 'index.php/login/view');
  } else {
   if (date('H:i:s') > $this->session->userdata('endTime')) {
     redirect (base_URL(). 'index.php/login/view');
   } else {
     $this->model->load('buttons_model');
   }
  }
 }
}

Model

<?php

class buttons_model extends CI_Model {


      public function __construct()
  {
          parent::__construct();
  }


public function alterinput() {

    $Username = $this->session->userdata('username');
$Password = $this->session->userdata('password');

$sql = "UPDATE input 
 SET inputOne = (inputOne + 1),
  inputTwo = (inputTwo + 1),
  inputThree = (inputThree + 1)
 WHERE (classPassword = '" . $Username . "',
  Username = '" . $Password . "') ";

      $this->db->query($sql);

  //---------------------------------------
  // END OF FILE
  //---------------------------------------
}

Where am I going wrong in trying to get the columns to update depending on the button press?

The error I'm getting is that I cannot get the values to enter separately, when Button One is pressed Only inputOne should add and the rest shouldn't

As an example, if I pressed Button One whilst I'm logged in as User1 the table should now look like:

+--+--------+--------+--------+--------+----------+
|ID|Password|Username|inputOne|inputTwo|inputThree|
+--+--------+--------+--------+--------+----------+
|1 |Pass    |User1   |1       |        |          |
|  |        |        |        |        |          |
+-------------------------------------------------+
  • 写回答

1条回答 默认 最新

  • duanjuebiao6730 2015-05-04 20:11
    关注

    You will need one click event for all buttons that runs an AJAX request to a controller that does your process, once the controller finishes and you want to update the DB, call teh model and the method for updating by + 1.

    JS

    $(".btn-lg").on('click', function(e){
        e.preventDefault(); // this will prevent the defualt behavior of the button
    
        // find which button was clicked
        butId = $(this).attr('id');
    
        $.ajax({
              method: "POST",
              url: "/controllerDummy/run/",
              data: { button: butId }
            })
          .done(function( msg ) {
            // do something
          });        
    });
    

    Controller

    // first get the button that was pressed
    button = $_POST['button']; // will be the id of the button that was clicked
    
    // do your code here.....
    
    // once completed load the model and run the method of updating...
    $this->load->model('button_model');
    $this->button_model->methodName();
    
    // if you need it only for specific buttons use an if statement
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址