Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling Arrays of HTML Input Elements with Request.Form Like PHP

Tags:

asp.net

How can I properly receive these Array of Inputs on asp.net?

<input type=hidden name='field[name][]' value='alex' />
<input type=hidden name='field[name][]' value='mark' />
<input type=hidden name='field[name][]' value='helen' />

<input type=hidden name='field[age][]' value='22' />
<input type=hidden name='field[age][]' value='30' />
<input type=hidden name='field[age][]' value='29' />

In php you can access field by $field = $_POST["field"]

$field["name"] and $field["age"] are simply arrays containing names and ages.

like image 633
EBAG Avatar asked Dec 30 '10 10:12

EBAG


People also ask

How do you input an array in HTML?

--taking array input by input name array[]--> <input type="number" name="array[]" value="4"> <input type="submit" name="submit"> </form> <? php $a=$_POST['array']; echo "Input :" . $a[3]; // Displaying Selected array Value foreach ($a as $v) { print_r($v); //print all array element. }

How can we store form data in an array in PHP?

You have to use JSON in order to update your array constantly. the important point is you have to load your students in a variable and then push the new student to that variable so, in the end, you can save your students variable which contains all students in one array like you want.

Is $_ post an array?

The PHP built-in variable $_POST is also an array and it holds the data that we provide along with the post method.

How to take array input from user in JavaScript?

For our purpose, we are going to use this function and get the input from the user and store it inside the variable. var inputArray = []; var size = 5; //Maximum Array size for(var i=0; i<size; i++) { //Taking Input from user inputArray[i] = prompt('Enter Element ' + (i+1)); } //Print the array in the console.


2 Answers

actually, it does exist with asp.net. - you can use

string[] MyTest = Request.Form.GetValues("MyTest");

or

string[] MyTest = Request.QueryString.GetValues("MyTest");
like image 120
webDeveloper Avatar answered Oct 05 '22 19:10

webDeveloper


You can use a standard string split - this is all php does for you behind the scenes. PHP is not a strongly typed language however which means that it is a LOT easier for them to provide the "appearance" of this functionality.

The reality is that php is just allowing you to provide comma delimited input and it automatically splits it for you.

So... if you want to do this in asp.net, you can use the same input name a number of times and it will be returned with the request object as a comma delimited list. I would not recommend using this approach for user entered input, but it will work fine if you are contrrolling the input like combining a list into a hidden input from a jquery script.

To get your head around what is happening (with php too.. all web dev techs are using the same http rules), just try posting a form with an input (don't set runat server) that is there twice.

<input type="text" name="MyTest" value="MyVal1" />
<input type="text" name="MyTest" value="MyVal2" />

On pageload add this

if(IsPostBack)
{
   Response.Write(Request["MyTest"]);
}

You should see

MyVal1,MyVal2

On the screen.

Now, if you want this into an array, you can:

string[] myvals = Request["MyTest"].Split(',');

if you want Integers or other datatypes (php doesn't know/care what a datatype is really), you will have to loop through it and parse it into another array/generic list.

I don't know what your wanting as an end result, but understanding what the browser posts back is the first step and the short answer is...

Yes, ASP.NET can do this to (just a little more manually).

like image 37
Gats Avatar answered Oct 05 '22 17:10

Gats