Kamil Gosciminski
Kamil Gosciminski

Reputation: 17177

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?

Upvotes: 1

Views: 85

Answers (1)

Rottingham
Rottingham

Reputation: 2604

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

Upvotes: 1

Related Questions