Hello im new to the world of php, ive been designing a webpage in ma spare time and ive got to a stage where im stuck its basically ticking checkboxes and inserting them into the next page somehow using the $post method not sure how to do it. But the problem i have is the options i want to chose are searched from a database and i want to select an output and then send it to the next page using a submit button. Form Submit array, ive got no idea at all ive done some coding up to adding checkboxes next to each row. heres my coding so far. Also i dont think you can have form in the php section;

<?
<form name="Dates" action=".php" method="post" onsubmit="return checkCheckBoxe();">
// Loop through data and display
while($a_row = mysql_fetch_assoc($result))
{
echo "<tr>".
'<td style="color: red;"><input type="checkbox" name="bookings[]" value="'.$a_row.'"></td>'.
"<td style=\"color:red\">".$a_row."</td>".
"<td style=\"color:red\">".$a_row."</td>".
"<td style=\"color:red\">".$a_row."</td>".
"<td style=\"color:red\">".$a_row."</td>".
"<td style=\"color:red\">".$a_row."</td>".
"<td style=\"color:red\">".$a_row."</td>".
"<td style=\"color:red\">".$a_row."</td>".
"<td style=\"color:red\">".$a_row."</td></tr>";
}
// Close connection ! (please !)
mysql_close($connection);

<input type="submit" name="Submit" value="Submit!" />
</form>
?>

first, no, you cant have any html inside of the php tags unless it is in an echo/print . i would put the form and submit buttom outside of the tags.

second, before you try to loop through databaase results, you have to connect to the database and retrieve results.
example:

mysql_connect($dbhost, $dbuser, $dbpasswd) or die(mysql_error());
mysql_select_db($dbname) or die(mysql_error());
$sql = "SELECT * FROM your_db_table";
	$result = mysql_query($sql);

and also, the "onsubmit" attribute of the html form tag does not relate to php. that is only if you are submiting the form with javascript.

hope this helps :P

<form name="Dates" action=".php" method="post" onsubmit="return checkCheckBoxe();">

action=".php" should be action="filename.php".

Assuming the code from aran87 is correct. When a visitor click on one of the checkboxes, how do I know which checkbox he clicked?

When you submit the form, only those checkboxes which are checked will be posted. So, If you have an array of checkbox with distinct values, you can easily know which were checked. :)

My checkboxes do NOT have distinct values, they are generated inside a while loop after reading a database.

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)){
        $tripid = $row["trip_id"];
        $fromdate = $row["fromdate"];
        $todate = $row["todate"];
        echo "<tr>";
        echo "<td>$fromdate</td>";
        echo "<td>$todate</td>";
        echo "<td>$rank</td>";
        echo '<td><a onclick="validate($tripid)"><button name="press" title="Poor comment"></button></a></td>';
        echo "</tr>";
        }

I need to know $tripid when the corresponding button is clicked, but my code only gives me the maximum value of the array, i.e. if I have echoed 70 lines of output, the visitor clicked line 35, the $tripid value returned was 70... What I need is 35. Thanks.

Since you don't know the name of your checkbox "names" from the form, you could walk the global variable array $_POST and get the values from there.

use the following code and you'll see the values on your form parsing page.

print_r($_POST);

something like

<?php
  foreach ($_POST as $key => $value) {
   //do something with those variables here
  }
?>

Agree with kireol. And we could provide more help if you show us your code.

Problem solved, thanks everyone. However, I have another problem which relates to static variable, I started another thread, pls help solve it again. Many thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.