简体   繁体   中英

Fetch post data from dynamically generated inputs

I'm using javascript to generate input fields in a form. The form looks as follows:

<form action="page.php" method="post">
    <input type="text" name="team_name" value="" />
    <input type="text" name="main_member" value="" />
    // every time a div is clicked, javascripts appends a new text input to the
    // form with name="members[]"
</form>

I am unable to fetch the posted data for the dynamically created input fields in php.

Here's the php:

$team_name = htmlspecialchars($_POST['team_name']);
$main_member = htmlspecialchars($_POST['main_member']);

$members = $main_member . join(',', $_POST['members[]']);

$STH = $DBH->prepare( "
        INSERT INTO teams (id, team_name, members)
        VALUES (NULL, '" .$team_name. "', '" .$members. "')
    " );
$STH->execute();

The database shows only the $main_member having been inserted. The php is not picking up the dynamically generated input fields.

What am I missing?

$members = $main_member . join(',', $_POST['members[]']); 

will not work. $_POST['members'] is the array generated by the form, which you can join on.

So it should be :

$members = $main_member . join(',', $_POST['members']);

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