将 javascript 变量从视图发送到 Codeigniter 中的控制器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10529964/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 10:11:56  来源:igfitidea点击:

Sending javascript variable from view to controller in Codeigniter

phpjavascriptcodeigniterviewcontroller

提问by Chathuranga

Possible Duplicate:
How to get JavaScript function data into a PHP variable

可能的重复:
如何将 JavaScript 函数数据放入 PHP 变量

i have a javascript variable in order.php view and i want to send that variable to a controller and then to the model to write it in to the database.

我在 order.php 视图中有一个 javascript 变量,我想将该变量发送到控制器,然后发送到模型以将其写入数据库。

ex: var myOrderString = "something" to order.php controller.

例如: var myOrderString = "something" 到 order.php 控制器。

how to do this?

这个怎么做?

采纳答案by Eric H

To build on rayan.gigs and antyrat's answers, below is a fix to rayan.gigs answer and how you would receive that information on the controller side:

为了建立 rayan.gigs 和 antyrat 的答案,以下是对 rayan.gigs 答案的修复以及您将如何在控制器端接收该信息:

rayan.gigs fixed answer (modified to match controller example below):

rayan.gigs 固定答案(修改以匹配下面的控制器示例):

// your html
<script>
    var myOrderString = "something";

    $.ajax({
        type: "POST",
        url: <?= site_url('OrderController/receiveJavascriptVar'); ?>
        data: {"myOrderString": myOrderString},  // fix: need to append your data to the call
        success: function (data) {
        }
    });
</script>

The jQuery AJAX docsexplain the different functions and parameters available to the AJAX function and I recommend reading it if you are not familiar with jQuery's AJAX functionality. Even if you don't need a success callback, you may still want to implement error so that you can retry the AJAX call, log the error, or notify the user if appropriate.

jQuery的AJAX文档说明可供AJAX功能的不同功能和参数,我建议你阅读它,如果你不熟悉jQuery的AJAX功能。即使您不需要成功回调,您可能仍然希望实现错误,以便您可以重试 AJAX 调用、记录错误或在适当时通知用户。

antyrat (modified to fit controller example below):

antyrat(修改为适合下面的控制器示例):

<form action="<?= site_url('OrderController/receiveJavascriptVar'); ?>" method="POST"  id="myForm">
  <input type="hidden" name="myOrderString" id="myOrderString">
</form>
<script>
  var myOrderString = "something";
  document.getElementById('myOrderString').value = myOrderString;
  document.getElementById('myForm');
</script>

The controller could look like this for both of the above cases:

对于上述两种情况,控制器可能如下所示:

<?php
    class OrderController extends CI_Controller
    {
        public function receiveJavascriptVar()
        {
            $myJSVar = $this->input->post('myOrderString');
            // push to model
        }
    }
?>

I would recommend rayan.gigs method instead of abusing forms. However, if you are trying to submit user information or submit information along side an existing form submission, you could use antyrat's method and just insert a hidden that you fill either on render, or via javascript.

我会推荐 rayan.gigs 方法而不是滥用表格。但是,如果您尝试提交用户信息或与现有表单提交一起提交信息,您可以使用 antyrat 的方法并插入您在渲染时或通过 javascript 填写的隐藏。

回答by Sameh Serag

You want to send var from view to anther controller ..... it's mean new request

您想将 var 从视图发送到另一个控制器 ..... 这意味着新请求

It's easy to do using Ajax request

使用 Ajax 请求很容易做到

In View

在视图中

 // your html
 <script>
  var X = "bla bla ";

   $.ajax
     ({
     type: "POST",
     url: // ur controller,
     success: function (html)
                            {
     }
     });

 </script>

回答by antyrat

You can create simple html <form>with hidden field. Assign your JS variable to that field and submit the form.

您可以创建<form>带有隐藏字段的简单 html 。将您的 JS 变量分配给该字段并提交表单。

Simple example:

简单的例子:

<form action="order.php" id="myForm">
  <input type="hidden" name="myOrderString" id="myOrderString">
</form>
<script>
  var myOrderString = "something";
  document.getElementById('myOrderString').value = myOrderString;
  document.getElementById('myForm');
</script>

Now in order.phpYou will have _GETvalue of myOrderString.

现在order.php你将有_GETmyOrderString

Also you can do it simply using AJAX.

你也可以简单地使用 AJAX 来完成。