【问题标题】:HTML form input passed into PHP function argument as arrayHTML 表单输入作为数组传递给 PHP 函数参数
【发布时间】:2014-01-15 00:12:40
【问题描述】:

我正在用 HTML 填写一个表单,我想将它作为数组参数传递给 PHP 中的函数。

这是它的样子。

稍后在insert 函数内部,我将传递这些值以将记录添加到我的数据库表中,但现在为了调试目的而实现了回显。

<?php

echo '
    Dodaj koncert
    <form action="" method="post">
Nazwa klubu: <input type="text" name="data[0]"><br>
Adres Klubu: <input type="text" name="data[1]"><br>
Nazwa zespolu: <input type="text" name="data[2]"><br>
Ilosc czlonkow: <input type="text" name="data[3]"><br>
Data koncertu: <input type="text" name="data[4]">
<input type="submit" name="dodaj_koncert" value="Dodaj">
</form> ';

    if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    insert(Koncerty, data);
}

function insert($table, $data)
    {
        echo $data[2] . '<br>';
        echo $data[3] . '<br>';
        echo $table;
    }   

?>

我确定data 有问题,我需要成为一个数组,这要么是我从输入表单分配值的方式,要么是我处理它的方式。如何使它作为一个数组工作并且可以从 insert 函数中读取填充的表单?

【问题讨论】:

  • 切换到 if ($_POST) { insert('Koncerty', $_POST); }
  • 我将添加empty() 稍后查看。它有那么大的改变吗?如果更改为$_POST,我以后不需要检查它,或者会发生什么?
  • @Dacto,Koncerty 是数据库中的一个表,而data 应该是 PHP 数组。然后$table 将在函数insert() 中执行以使用此表并将数组中的数据插入其中。
  • @nobodynoone:哦。我现在明白你的意思了。

标签: php arrays forms


【解决方案1】:
echo '
Dodaj koncert
<form action="" method="post">
    Nazwa klubu: <input type="text" name="data1"><br>
    Adres Klubu: <input type="text" name="data2"><br>
    Nazwa zespolu: <input type="text" name="data3"><br>
    Ilosc czlonkow: <input type="text" name="data4"><br>
    Data koncertu: <input type="text" name="data5">
    <input type="submit" name="dodaj_koncert" value="Dodaj">
</form> ';

// If the submit button field exists then we will insert the data
if (isset($_POST['dodaj_koncert'])) {
    insert('Koncerty', $_POST);
}

// When looking at the form fields from the POST
// You use the name of the field to access it in the $_POST array.
function insert($table, $data)
{
    echo $data['data1'] . '<br>';
    echo $data['data2'] . '<br>';
    echo $table;
}   

【讨论】:

    猜你喜欢
    • 2014-12-08
    • 1970-01-01
    • 2020-12-23
    • 2021-07-16
    • 1970-01-01
    • 2013-12-09
    • 2018-11-26
    • 2012-12-07
    相关资源
    最近更新 更多