santa
santa

Reputation: 12512

List values into text fields

I have a form with quite a few text fields.

I would like to add a functionality where I would add a textarea where user can drop multiple values, perhaps one per line, and on submit, populate those values into text fields of my form.

I use PHP and jQuery on this page. Something tells me I should go with PHP and do a loop for $_POST. Am I on the right track? I build the text fields via a loop anyway, on page load:

for ($i = 1; $i <= 10; $i++) {
    echo '
    <div>
        <input type="text" maxlength="100" name="field[]">
    </div>';
}

How would I combine this with the new feature where I would separate values from a bulk submission-?

foreach ($_POST as $listValue) {
    echo '
    <div>
        <input type="text" maxlength="100" name="field[]" value='.$listValue.'">
    </div>';
}

Upvotes: 2

Views: 138

Answers (2)

George Cummins
George Cummins

Reputation: 28906

Create your textarea:

<textarea id="field" name="field"></textarea>

Then parse the results in PHP:

$text_area_content = $_POST['field'];
$lines_array = explode("\n", $text_area_content);

$lines_array now contains an element for each line of the textarea.

Upvotes: 1

Brice Favre
Brice Favre

Reputation: 1527

I'll do this way.

In the form :

<textaera cols="20" rows="60" name="values">

in the PHP :

$array_values = explode("\n", $_POST['values']);

foreach ($array_values as $listValue) {
echo '
    <div>
       <input type="text" maxlength="100" name="field[]" value="'.htmlentities($listValue).'">
    </div>';
}

Upvotes: 2

Related Questions