so i have this table in mysql called colegue in database addressbook1. it has the following fields: colegue_id, firstname, lastname, telephone and email. what i want is to get the data within the firstname column and display it within a form that uses checkboxes.for example:
checkbox jerry
checkbox mary
checkbox cindy
and so on. then, the checked data is supposed to be sent to a script which echoes the data selected. here’s the code that loads data onto the page:
<?php
//connect to mysql
mysql_connect("localhost", "root", "");
//select database
mysql_select_db("adressbook1");
//select data from table
$query=("SELECT * FROM colegue");
//perform query
$result=mysql_query($query);
?>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>testing</title>
</head>
<body>
<h3>testing a checkbox form</h3>
<?php
//start the form
echo "<form method=\"post\" action=\"process.php\">";
while($row=mysql_fetch_array($result))
{
//assign value from associative array to variable
$data=$row['firstName'];
/*echo data and assign the value in the input the current value of $data while loading onto array*/
echo "<input type=\"checkbox\" name=\"firstname[]\" value=\"<?php $data;?>\">".$data." <br>";
}
echo "<br>";
//send data to process.php when clicked
echo "<input type=\"submit\" name=\"sent\" value=\"add\">";
echo "</form>";
?>
</body>
</html>
now for the script that processes the data:
<?php
//get the data from the form in an array
$entry=$_POST['firstname'];
//check if the array is empty or not
if (empty($entry))
{
echo("execution failed");
}
//if not empty, continue
else
{
//get the array size
$N=count($entry);
//display the size of the array
echo "$N"."<br>";
//this loop should display all the data in the array
for($i=0; $i<$N; $i++)
{
echo "$entry[$i]"."<br>";
}
//end script by showing successful result
echo ("request successful!");
}
?>
the code runs fine when i check the names i want sent and submit them to the script. the script does show me the size of the array, (e.g. if i clicked on four names, it shows me the number four) but it doesn’t show me anything else, it doesn’t display the actual names that i clicked on the page. could anyone kindly help me solve this? i’m thinking that maybe i haven’t properly assigned values in the array firstname[].
Please note that i have to use this method (checkboxes) to submit the data gotten from the database so that it is displayed in the script-page. i am very new to programming, so i kindly ask for help, thanks.
You need to concatenate $data with the string, just alter
too