1

I have a form which setup looks similar to this:

<form action = "test.php" method="post">
 <input type="text" name="name" />
 <textarea name="item[]" rows="4" cols="90" maxlength="500" ></textarea>
 <input id="submitButton" type="submit" name="submit" class="submit action-    button" value="Verzenden" ></textarea>
</form>

Now I have a button also on the same page on which the form resides, which executes a jquery function. This function adds an extra textarea (with name="item[]" to the form. So it could be that if the user clicks that button two times that I eventually have three textareas..

Now when user submits the form then it will go to another page: the test.php page. In this page I execute a PHP script in which I test if POST isset and then catch the POST values..

Now every user sees the $_POST['name'] value, but for the $_POST['item'] array, sometimes the array is empty(I saw that it was on mozilla firefox 51.0.1 browser).. even if they filled the textareas with text.. and they get the warning for this line(which is in the test.php script):

foreach($_POST['item'] as $item)

Strange, but does anyone recognizes these problem?

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
Nuri Ensing
  • 1,899
  • 1
  • 19
  • 42
  • 1
    Possible duplicate of [Multiple inputs with same name through POST in php](http://stackoverflow.com/questions/7880619/multiple-inputs-with-same-name-through-post-in-php) – Kumar Abhirup Mar 16 '17 at 07:37
  • bdy share your jquery code please which adds the new text-areas (may be problem is there). Also your text-area is not closed. `` is missing – Alive to die - Anant Mar 16 '17 at 07:37
  • 2
    forget to close `` tag may be the cause of it – Saty Mar 16 '17 at 07:37
  • I think you need to close before button code start. – Jalpa Mar 16 '17 at 07:39
  • Wht are u trying to achieve? Why is your text area named item[]? Do u know the role of text area? And your html muckup is invalid – Masivuye Cokile Mar 16 '17 at 07:40
  • 1
    *"it was on mozilla 5.1"* -- [Mozilla](https://www.mozilla.org/en-US/about/) is a company, not a browser. If you are talking about [Mozilla **Firefox**](https://www.mozilla.org/en-US/firefox/new/), version 5.1 [doesn't exist](https://en.wikipedia.org/wiki/Firefox_version_history). You probably mean version 51. Make sure the HTML you write or generate is [valid HTML](https://validator.w3.org/); otherwise strange things may happen and PHP or the browsers are not guilty. – axiac Mar 16 '17 at 07:57

1 Answers1

1

1.Closing text-area is missed. add it like below:-

<textarea name="item[]" rows="4" cols="90" maxlength="500"></textarea>

2.Make sure that you jQuery code adding every new textarea with below HTML:-

<textarea name="item[]" rows="4" cols="90" maxlength="500"></textarea>
<!--name is area of concern here and must be same -->

3.Apply check befor using foreach():-

if(count($_POST['item'])>0){
  // here do you foreach() loop coding
}

Note:- In a single text-area you can pass massive amount of data? so why multiple text-areas?

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98