2

In my html I have

<input type="hidden" name="Code_value" value="xyz">
<input type="hidden" name="Code_value" value="abc">
<input type="hidden" name="Code_value" value="rst">`

when i submit I take it in my ASp.net code I take them as

string Items = Request.Form["Code_value"]; 

the value of Items is xyz,abc,rst

in PHP $Items=$_POST['Code_value'];

The value of items is rst

It takes the last value in php is it because there are multiple Code_value items if so why is it not taking just the last value in Asp.net

Toby Allen
  • 10,997
  • 11
  • 73
  • 124
bleu
  • 87
  • 1
  • 2
  • 11

2 Answers2

1

why is it not taking just the last value in Asp.net

Asp.net works this way

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
1

This is simply down to the way PHP and ASP.NET read the request. If ASP.NET encounters the same form field name multiple times it will append the values. PHP, on the other hand, will overwrite the previous value with the current one, so you'll only get the last value in $_GET or $_POST.

You have two options.

The first is to simply tell PHP to treat the fields in question as an array. This is the easiest approach because all you have to do is put square brackets at the end of the field name.

<input name="field[]" value="1" />
<input name="field[]" value="2" />
<input name="field[]" value="3" />

You can give each element in the array a particular key if you want too.

<input name="field[foo]" value="1" />
<input name="field[bar]" value="2" />
<input name="field[baz]" value="3" />

The other approach is to read the raw input the the script and process it yourself.

$postdata = file_get_contents("php://input");

This method is a lot more complex, as you have to parse all the input data yourself. It also has limitations.

For that reason I'd recommend you use the first method.

Community
  • 1
  • 1
GordonM
  • 31,179
  • 15
  • 87
  • 129