tags:

views:

86

answers:

3

I have created a form, with 5 textbox fields and I want to add those five entries in the database. I want to use the textbox "array", that way I can use a for-each when saving to the database. As anyone, any code on how to do this or can direct me in the right path?

input type="text" value="whateva" name= ?php text[0] ?> 
input type="text" value="whateva" name= ?php text[1] ?> 
input type="text" value="whateva" name= ?php text[2] ?> 

if (isset($_POST['Submit']) {
  //add to db
  (for-each $text as $val) {
    //add to db
  }
}

Is this possible?

A: 

Yes, HTML supports arrays. just name your textareas like this:

<textarea name="field[]"></textarea> /* Notice square brackets */

For this example, in PHP, your $_GET or $_POST will have array key with name 'field' and values from these textareas.

Deniss Kozlovs
A: 

If 'Submit' is the name of the submit button. yeah that will work.

but few suggestions:

  1. correct it as:

    < input type="text" value="whateva" name= "" />

  2. Use validation for the text submitted by user

  3. IMPORTANT: "GET A BOOK ON PHP" and learn it. Seriously, if you learn this way, you wont become a good programmer. You are learning it the hardway. Book is must for you.

claws
the site wasn't recognizing my tags. blanking the data sorry
ferronrsmith
A: 

HTML

<input type="text" value="whateva" name="text[]" />
<input type="text" value="whateva" name="text[]" />
<input type="text" value="whateva" name="text[]" />

PHP

if (!empty($_POST['text'])) {
    foreach ($_POST['text'] AS $value) {
        // add to the database
        $sql = 'INSERT INTO tableName SET fieldName = "' . mysql_real_escape_string($value) . '"';
    }
}
cballou