0

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?

3 Answers 3

1

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) . '"';
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

0

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.

1 Comment

the site wasn't recognizing my tags. blanking the data sorry

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.