我正在用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
函数中读取填充的表单?
答案 0 :(得分: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;
}