Re: Read dynamic variable from HTML form into PHP

From: Date: Fri, 08 Jun 2012 09:01:31 +0000
Subject: Re: Read dynamic variable from HTML form into PHP
References: 1 2 3 4 5 6 7  Groups: php.general 
Request: Send a blank email to php-general+get-318192@lists.php.net to get a copy of this message
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"; } } ?>

« previous php.general (#318192) next »