简体   繁体   中英

HTML form input passed into PHP function argument as array

I'm filling out a form in HTML, that I want to pass as an array argument to a function in PHP.

Here's how it looks like.

Inside insert function later on I'm going to be passing those values to add record into my database table, but for now on there's echoing implemented for debuggnig purposes.

<?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;
    }   

?>

I'm sure there's something wrong with the data that I need to be an array and it's either how I assign values from input form or how I process it. How do I make it work as an array and can read filled form from within the insert function?

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;
}   

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM