1

I have an array of values stored in variable "do" It's something like this but not exactly

<html>
  <input type='text' name='do' id='do'>
  <input type='text' name='do' id='do'>
  <input type='text' name='do' id='do'>
  <input type='text' name='do' id='do'>
  <input type='text' name='do' id='do'>
</html>

and I print it all with the use of [print_r] then it gives a result

<?php
print_r($_POST['do']);

//and i try this also
foreach($_POST as $key => $val){
  echo $key . ' : ' . htmlentities($val,ENT_QUOTES) . "<br>\n";?>   
}


[do=1&do=2&do=3]

how could I modify or just print the values like this:

1
2
3
2
  • what you are trying to do with the same names?? Commented Feb 21, 2012 at 9:45
  • Pls post what's stored in $_POST['do'] Commented Feb 21, 2012 at 9:50

6 Answers 6

1

Do not assign multiple elements the same ID (that's why it's out) but change the name in order to send an array of elements as POST

<input type='text' name='do[]'>
<input type='text' name='do[]'>
<input type='text' name='do[]'>
<input type='text' name='do[]'>
<input type='text' name='do[]'>
Sign up to request clarification or add additional context in comments.

Comments

0
foreach($_POST as $key => $val)
  echo $val;

^^ would do. However you cannot edit like $val=something. To change value, use $POST[$key]=something;

Comments

0
<input type='text' name='do[]' id='do'>
<input type='text' name='do[]' id='do'>
<input type='text' name='do[]' id='do'>
<input type='text' name='do[]' id='do'>
<input type='text' name='do[]' id='do'>

Comments

0

this won't work properly because you're overriding the same variable with different values and end up having one variable with the last value assigned.

Change this to

<input type='text' name='do[]' id='do'>
<input type='text' name='do[]' id='do'>
<input type='text' name='do[]' id='do'>
<input type='text' name='do[]' id='do'>
<input type='text' name='do[]' id='do'>

So you can loop through it like this:

foreach($_POST['do'] as $key => $val)

Comments

0

PHP overwrites the values with same plain names when parsing POST data into $_POST.

You may instead write the following form code:

<html>
<input type='text' name='do[]' id='do'>
<input type='text' name='do[]' id='do'>
<input type='text' name='do[]' id='do'>
<input type='text' name='do[]' id='do'>
<input type='text' name='do[]' id='do'>
</html>

Now, $_POST['do'] is the array containing five do strings.

Comments

0

I think it can be done this way:

<input type="text" name="do[]" id="do_1" />
<input type="text" name="do[]" id="do_2" />
<input type="text" name="do[]" id="do_3" />
<input type="text" name="do[]" id="do_4" />
<input type="text" name="do[]" id="do_5" />

Then on POSTing, just look inside $_POST['do'] to get an array.

Edit: note that the apostrophes have been switched to quotes, the IDs have been made unique, and each tag has been self-closed. In general it will not validate otherwise.

Comments

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.