【问题标题】:How to call controller function from javascript如何从javascript调用控制器函数
【发布时间】:2017-08-22 03:33:15
【问题描述】:

使用 mvc 和 codeigniter 创建 Web 应用。在视图中,单击按钮会调用 javascript 方法。此方法应将 3 张图像的来源作为参数,并将它们传递给控制器​​方法。然后,控制器方法将它们转换为单个 $record 并将其传递给模型中的函数,该函数随后会将记录内容插入数据库。

查看按钮:

<input type="submit" class="btn btn-primary" value="Assemble bot" onclick="insertAssembled()"></input>

Javascript 函数:

function assemble(var head, var body, var legs) {
    var head = document.getElementById("HeadImage".src);
    var body = document.getElementById("BodyImage".src);
    var legs = document.getElementById("FeetImage".src);
    // call controller function here, passing head body and legs
}

控制器方法:

    public function insertAssembled($head, $body, $legs) {
        //Build record and send to saveBot       
        $record['id'] = 1;
        $record['head'] = $head;
        $record['body'] = $body;
        $record['legs'] = $legs;
        $this->Robotsdata->saveBot($record);
   }

模型方法(很粗略,传参不​​需要帮忙):

public function saveBot($record) {
    $con = mysql_connect("localhost","root@localhost","");
    mysql_select_db("factory", $con);
    $bot_id = $_POST['id'];
    $bot_head = $_POST['head'];
    $bot_body = $_POST['body'];
    $bot_legs = $_POST['legs'];
    $query = "INSERT INTO `factory`.`assembledbots` ('id', 'head', 'body', 'legs') "
            . "VALUES ('$bot_id', '$bot_head', '$bot_body', '$bot_legs');";
    mysql_query($query);
    mysql_close($con);
}

【问题讨论】:

  • 请阅读 SQL 注入
  • ajax 是你唯一的朋友

标签: javascript php codeigniter web-applications


【解决方案1】:

如果您已包含 jQuery 库,您可以尝试使用 ajax 传递数据:

$.ajax({
    url: 'Path/To/YourController.php',
    type: 'POST',
    dataType: 'text',
    data: {
        head: head, //You'll get it with $_POST['head']
        body: body, //You'll get it with $_POST['body']
        legs: legs  //You'll get it with $_POST['legs']
    },
})
.done(function(data) {
    // Do what you want in case of success
})
.fail(function(err) {
    //Do what you want incase of error
});

【讨论】:

  • 而使用 ajax,我什至需要一个控制器功能吗?
  • 是的,您应该像往常一样将数据从控制器发送到模型。
【解决方案2】:

您可以通过使用 ajax 来实现这一点。此外,在您的模型中,尝试使用 CI 的查询生成器类(活动记录),这样您的查询是安全的并避免 SQL 注入。

javascript

function assemble(var head, var body, var legs) {
   var head = document.getElementById("HeadImage".src);
   var body = document.getElementById("BodyImage".src);
   var legs = document.getElementById("FeetImage".src);

   //ajax
   //you post variables
   var fields = {
      head  : head,
      body  : body,
      legs  : legs,
      submit_a: true
   };

   $.ajax({
     url: 'domain/controller/insertAssembled',
     type: 'POST',
     data: fields,
     dataType:'JSON',
     success: function(result){
        console.log(result);
     }
   });
}

控制器

public function insertAssembled() 
{

  if ($this->input->is_ajax_request()) { // just additional, to make sure request is from ajax
    if ($this->input->post('submit_a')) {
        $record['id'] = 1;
        $record['head'] = $this->input->post('head');
        $record['body'] = $this->input->post('body');
        $record['legs'] = $this->input->post('legs');

        // to model
        $this->Robotsdata->saveBot($record);

        // return to view
        echo json_encode(array("error"=>false));
        return false;
    }
  }
}

型号

public function saveBot($record) 
{
  if ($record) {

    $save_fields = array(
        'id'   => $record['id'],
        'head' => $record['head'],
        'body' => $record['body'],
        'legs' => $record['legs']
    );
    $this->db->insert('table_name', $save_fields);

    return true;
  }
  return false;
}

【讨论】:

  • 谢谢,但目前我无法正常工作,是否需要将“submit_a”更改为特定于我的程序的内容?
  • @Bobbis 你可以改变它取决于你。 submit_a 只是一个标识符,以确保它确实是来自该特定 ajax 请求的特定 POST 变量。只需确保重命名时,从 ajax 到您将在控制器中调用的变量名称必须相同。你现在遇到了什么错误?
猜你喜欢
  • 2017-07-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-15
  • 1970-01-01
  • 1970-01-01
  • 2012-01-17
  • 1970-01-01
相关资源
最近更新 更多