Re: Re: Dynamically Created Checkboxes

From: Date: Wed, 23 Feb 2011 16:31:47 +0000
Subject: Re: Re: Dynamically Created Checkboxes
References: 1 2 3  Groups: php.general 
Request: Send a blank email to php-general+get-311538@lists.php.net to get a copy of this message
On 2/23/2011 4:35 AM, Gary wrote: > "Pete Ford" <pete@justcroft.com> wrote in message > news:62.C1.32612.D49D46D4@pb1.pair.com... >> This bit? >> >> On 22/02/11 22:06, Gary wrote: >>> for($i=1; $i<=$_POST['counties']; $i++) { >>> if ( isset($_POST["county{$i}"] ) ) { >> >> You loop over $_POST['counties'] and look for $_POST["county$i"] >> >> I suspect that there is no field 'counties' in your form, so the server is >> complaining about the missing index - seems likely that the production >> server >> is not showing the error, but instead just giving up on the page. >> >> I think the IE7/Firefox browser difference is a red herring: it wasn't >> actually working in any browser, or the code changed between tests. >> Remember, PHP is server side and generates HTML (or whatever). Unless you >> tell the PHP what browser you are using and write PHP code to take account >> of that (not generally recommended) then the server output is the same >> regardless of browser. >> >> -- >> Peter Ford, Developer phone: 01580 893333 fax: 01580 >> 893399 >> Justcroft International Ltd. >> www.justcroft.com >> Justcroft House, High Street, Staplehurst, Kent TN12 0AH United >> Kingdom >> Registered in England and Wales: 2297906 >> Registered office: Stag Gates House, 63/64 The Avenue, Southampton SO17 >> 1XS > > > Pete > > Once again, thank you for your help. > > I was able to get it to work, I did not understand the browser differences > either, but on my testing server on IE, it worked as I wanted but not on > anything else. I chopped out most of the code and ended up with this: > > if ( isset($_POST['submit']) ) {} else { > print "<form action=\"phpForm3.php\" method=\"POST\">\n"; > if ($Recordset1) { > print "<table width=200 border=1>\n"; > print "<th>&nbsp; </th>\n"; > print "<th> State </th>\n"; //2 fields in Counties table, State and > County > print "<th> County </th>\n"; > print "</tr>\n"; > //create table > $i = 0; > while ( $row = mysql_fetch_array($Recordset1) ) { > $i++; > print "<tr>\n"; > print "<td><input type=\"checkbox\" name=\"county$i\" > value=\"$row[name]\"></td>\n"; > echo "<td>{$row['state_id']}</td>\n"; > echo "<td>{$row['name']}</td>\n"; > echo "</tr>\n"; > }//end while > print "</table>\n"; > } else { > echo("<P>Error performing query: " . > mysql_error() . "</P>"); > } > print "<input type=\"hidden\" name=\"counties\" > value=\"$i\"/>\n"; > print "<input type=\"submit\" name=\"submit\" > value=\"Go\"/>\n"; > } > ?> > > Again, thank you. > > Gary > > __________ Information from ESET Smart Security, version of virus signature database 5899 > (20110223) __________ > > The message was checked by ESET Smart Security. > > http://www.eset.com > If you would allow me to show you a little easier way of doing this. if ( !isset($_POST['submit']) ) { echo '<form action="phpForm3.php" method="POST">'; if ($Recordset1) { echo <<<HEADER <table width=200 border=1> <tr><th> &nbsp; </th><th> State </th><th> County </th></tr> HEADER; while ( $row = mysql_fetch_array($Recordset1) ) { echo <<<ROW <tr> <td><input type="checkbox" name="county[]" value="{$row['name']}"></td> <td>{$row['state_id']}</td> <td>{$row['name']}</td> </tr> ROW; }//end while echo '</table>'; } else { echo '<p>Error performing query: '.mysql_error().'</p>'; } echo '<input type="submit" name="submit" value="Go" />'; } Now, the main thing I want you to see is the line for the country checkbox'es The county[] turns the submitted values into an array. So, in the processing script, you would do this. <?php ... if ( !empty($_POST['county']) foreach ( $_POST['county'] AS $id => $name ) echo "{$id} {$name}\n"; ... ?> Hope this clears things up for you a little. Enjoy. Jim

« previous php.general (#311538) next »