Click here to Skip to main content
15,915,336 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have a list of checkboxes, with values retrieved from a SQL table.

PHP
$sql = "SELECT Name FROM APKs";
$resultado = mysql_query($sql) or die ("problema-- ".mysql_error());
$numero = 0;

print "<FORM NAME=\"form1\" METHOD=\"POST\" ACTION=\"index.php\">";
while($registo = mysql_fetch_array($resultado))
{
    $name = $registo['Name'];
    print "<Input type='Checkbox' Name='.$name.'>$name</br>";
    $nomes[numero] = $name;
    $numero++;
}



What I just want to do is save all name choices. I think the best way is storing is using an array, the problem is the data is lost with the POST

PHP
if (isset($_POST['Submit1'])) {

    echo $nomes[0]; // I get here a null variable
}


How can solve this problem in an efficient way?

EDIT: I think the optimal solution is using

XML
$name[numero] = $registo['Name'];
    print "<Input type='Checkbox' Name='.$name[numero].'>$name[numero]</br>";
    //$nomes[numero] = $name;
    $numero++;


and then

echo "aqui" . $_POST['$name'];


but it's not working because the $name variable it's not saved. How can I accomplish that?
Posted
Updated 9-Apr-11 11:58am
v3

You are printing the markup. Instead move the markup to another file (no printing) and once you got the array use required_once('your mark up file'). Now the array will be available for the markup page to display in the selection boxes. Handle the display logic there. The array in the code page will be available as such when you post back to the same page. This way you can separate the markup and the code.

Use an action id parameter in the markup to tell the code which function to execute on post back. Happy programing!
 
Share this answer
 
Add another attribute in checkbox called "value".
print "<Input type='Checkbox' Name='.$name[numero].' value='.$name[numero].' >$name[numero]</br>";
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900