user153887
user153887

Reputation: 29

how to get post element in form in this case( in PHP )

in a form which is handled by php language, how can we get a input elements which are in a kind of array like item1,item2,item3..... (if we only want those items that are having values)

example:-
<form method="post" action="<?php echo $PHP_SELF;?>">
item1 <input type="text" name="item1">
item2 <input type="text" name="item2">
item3 <input type="text" name="item3">
<input type="submit" value="submit" name="submit"> 
</form>

Upvotes: 0

Views: 1731

Answers (2)

Jase Whatson
Jase Whatson

Reputation: 4207

<form method="post" action="<?php echo $PHP_SELF;?>">
item1 <input type="text" name="item[]">
item2 <input type="text" name="item[]">
item3 <input type="text" name="item[]">
<input type="submit" value="submit" name="submit"> 
</form>

Then in php

$inputarray = $_REQUEST['item'];

echo $inputarray[0];

Upvotes: 3

Alex Mcp
Alex Mcp

Reputation: 19315

Do you mean beyond what's in the $_POST array? That contains everything submitted to the page.

So you can say something like

if (isset($_POST['item1'])){
    echo $_POST['item1'];
}

(Obviously beyond echoing it, you can save it to a DB, do operations on it and print it back to the page somehow, or whatever you're using the form for)

Upvotes: 0

Related Questions