【问题标题】:Pass a javascript array to php in Code Igniter [duplicate]在 Code Igniter 中将 javascript 数组传递给 php [重复]
【发布时间】:2014-09-16 21:57:50
【问题描述】:

我正在使用 CodeIgniter,我的 js 代码在我的“视图”中,我想在我的控制器中传递值。

var storage =[];

function something()
{

storage.push('the value');

}

现在我想要一种更好的方法将存储数组传递到我的 PHP 中。提前致谢。 此代码不起作用,因为它位于单独的文件夹中。

$.ajax({
    url: 'yourPHPFile.php',
    type: 'POST',
    data: {data:storage.toString()},
    success: function(result) {
            // handle your success
    },
    error: function() {
      // alert("error");
    }
});

【问题讨论】:

  • 您可以为此目的使用 JSON
  • 您能否详细说明"push storage array into my php"。它相当广泛和模糊。
  • 我知道,但我不知道语法
  • 不要介意推送..一切正常..唯一的问题是将数组传递给 php 文件
  • 你想在php中如何使用它?您需要提供更多信息。

标签: javascript php html codeigniter


【解决方案1】:

使用 JQuery 发布数据

$.ajax({ type: "POST",
             url: "Filename.php",
             data: storage,//no need to call JSON.stringify etc... jQ does this for you
             success: function(resopnse)
             {//check response: it's always good to check server output when developing...
                 }});

在你的 PHP 文件中

$array=json_decode($_POST['jsondata']);

【讨论】:

    【解决方案2】:

    最简单的方法是使用jQuery

    您可以通过使用$.ajax 将数据发送到server 来做到这一点

    $.ajax({
        url: 'yourPHPFile.php',
        type: 'POST',
        data: {data:storage.toString()},
        success: function(result) {
                // handle your success
        },
        error: function() {
          // alert("error");
        }
    });
    

    在服务器端,在您的 PHP 代码中执行,

    $data = $_POST['data'];
    echo $data;
    

    $data 将包含the value

    【讨论】:

      【解决方案3】:

      使用 JSON 编写 javascript...

      JSON.stringify(array)
      

      并使用...检索

      $array=json_decode($_POST['jsondata']);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-06-20
        • 1970-01-01
        • 2012-05-27
        • 2013-03-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多