【问题标题】:How can I pass an array in HTML file?如何在 HTML 文件中传递数组?
【发布时间】:2020-05-06 12:07:20
【问题描述】:

我的 HTML 文件中有一个标签,我的 PHP 文件中有“myfunc”函数。如何将 href 中的数组传递给我的“myfunc”函数?

<a class="btn btn-danger" href="<?php echo myfunc("this is arg1 witch is string", ['id']); ?>">delete</a>

这是我的 PHP 函数:

function myfunc($str,$param=[])
{ 
    return true;
}

【问题讨论】:

  • href里怎么会有数组?
  • @vivek_23 我想将一个字符串和一个数组传递给 myfunc 方法并回显返回值。

标签: php html codeigniter


【解决方案1】:

你不能有URL 的数组,但如果你必须它应该是encoded 格式,我已经写了一个演示代码,看看这是否能解决你的问题↓↓

查看

<?php 
    $id = array('1', '2');

    // convert the array to JSON objects and then convert the string(object) to query part of the URL.
    $id = urlencode(json_encode($id));
?>
<a class="btn btn-danger" href="<?php echo base_url("home/form/this is arg1 witch is string/{$id}"); ?>">delete</a>

控制器

function form($str, $param){

    echo urldecode($str); // convert the URL string back into its original form 
    echo '<br>';
    print_r(json_decode(urldecode($param)));  // Convert the URL string into encoded form and then convert it back to an array.
}

输出

this is arg1 which is string
Array ( [0] =&gt; 1 [1] =&gt; 2 )

阅读更多关于json_encodeurlencodeurldecodejson_decode的信息

【讨论】:

    【解决方案2】:

    在 Codeigniter 中,您可以在控制器中创建一个函数,然后,

    class Controller extends CI_Controller{
        function myfunction($str, $param = []){
             return "something";
        }
    }
    

    在各自的视图中

    <a class="btn btn-danger" href="<?php echo $this->myfunc("this is arg1 witch is string", ['id']); ?>">delete</a>
    

    但推荐的方法是预处理数据并将其传递给视图,在here 中进行了描述。

    【讨论】:

      猜你喜欢
      • 2023-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多