Re: Read dynamic variable from HTML form into PHP

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 




There are essentially 2 ways:
1. All POSTed data is present in the $_POST superglobal array. So you
could just loop over that, ignore the fields you already knew were there,
and the data remaining is then essentially the data you seek. The keys in
the $_POST array are the fieldnames you are looking for.

2. There's a special trick in PHP, when you name a field "name[]" in HTML
and then POST it to a PHP script, it will turn into an array field. So
<input name="a[]" value="1">  <input name="a[]" value="2">  will then end up
in:
$_POST = [
    'a' =>  [
       0 =>  '1',
       1 =>  '2'
    ]
]

If you had not added the square-brackets, you would have:
<input name="a" value="1">  <input name="a" value="2">  ending up in:
$_POST = [
    'a' =>  '2'
]
Thus not ever seeing the value '1'.




<form>
checkbox field name="input_1" value="y"
checkbox field name="input_2" value="y"
field name input_n
..
</form>

<?
//checkboxes return on submit only if ticked
$query="SELECT id FROM table WHERE etc";
$result=mysql_db_query($db, $query,$connection);
$count=mysql_num_rows($result);
while($row=mysql_fetch_row($result)) {
	$id=$row[0];
	//dynamic variable
	//if form uses textfield that returns on submit
	//if(${"input_".$id}=="1"){
	//if checkbox that only returns if ticked
	if(ISSET(${"input_".$id})){
		echo "checked 1";
	}
}
?>

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux