tascam424 Posted January 3, 2014 Share Posted January 3, 2014 I am trying desperately to add checkbox names to a table on my database, i've read loads of tutorials and haven't really gotten very far. Here is my code <script language="JavaScript"> function toggle(source) { checkboxes = document.getElementsByTagName('input'); for(var i=0, n=checkboxes.length;i<n;i++) { checkboxes[i].checked = source.checked; } } </script> <?php $con=mysqli_connect("localhost","kslc_admin","password","kslc_cdg"); // Check connection if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } $result = mysqli_query($con,"SELECT DISTINCT DiscNo, Description FROM CDG ORDER BY DiscNo"); echo "<td><input type=\"checkbox\" onClick=\"toggle(this)\" /> Toggle All<br/></td>"; echo "<form action=\"post.php\" method=\"post \" />"; echo "<table> <tr> <th>Tag</th> <th>Description</th> <th>DiscNo</th> <th>View Songs</th> </tr>"; while($row = mysqli_fetch_array($result)) { echo "<tr>"; echo "<td><input type=\"checkbox\" name=". $row["DiscNo"] ." id=\"checkbox\"></td>"; echo "<td>" . $row['Description'] . "</td>"; echo "<td>" . $row['DiscNo'] . "</td>"; echo "<td>" . $row['TrackNo'] . "</td>"; echo "<td><a href=\"tracks.php?DiscNo=" . $row["DiscNo"] . "\">View Tracks</a></td>"; echo "</tr>"; } echo "</table>"; echo "<input type=\"submit\" name=\"submit\" value=\"Submit \" />"; echo "</form>"; mysqli_close($con); ?> Now i don't know if this is even in the right direction but ive been trying to pass the checkbox names to post.php and somehow retrieve them and run a function to add to db table. Not sure how to achieve this or even if this is the correct way to go about it .. So far when i hit submit the URL looks like this http://mydomain.com/post.php?SF001=on&SF161=on&SF179=on&submit=Submit+ Where SF001, SF161 & SF179 are the correct checkbox names of selected checkboxes. So it is possible to retrieve these in post.php and somehow add them to database ? Really appreciate any help .. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/285062-add-check-box-names-to-database-table/ Share on other sites More sharing options...
cyberRobot Posted January 3, 2014 Share Posted January 3, 2014 If you want to use the POST method, you need to remove the space from the method attribute: echo "<form action=\"post.php\" method=\"post\" />"; Quote Link to comment https://forums.phpfreaks.com/topic/285062-add-check-box-names-to-database-table/#findComment-1463703 Share on other sites More sharing options...
tascam424 Posted January 3, 2014 Author Share Posted January 3, 2014 Thanks for that !! Everything is starting to look fuzzy lol Quote Link to comment https://forums.phpfreaks.com/topic/285062-add-check-box-names-to-database-table/#findComment-1463704 Share on other sites More sharing options...
cyberRobot Posted January 3, 2014 Share Posted January 3, 2014 No problem; glad to help! For what it's worth, you could simplify the code a bit by changing the quotes around your strings. Instead of using double quotes around the <form> tag, for example, you could use single quotes. That way you don't need to escape the double quotes within the double-quoted string. echo '<form action="post.php" method="post" />'; For more information about displaying strings, see the following manual page: http://www.php.net/manual/en/language.types.string.php Quote Link to comment https://forums.phpfreaks.com/topic/285062-add-check-box-names-to-database-table/#findComment-1463707 Share on other sites More sharing options...
tascam424 Posted January 3, 2014 Author Share Posted January 3, 2014 Ahhh ok .. that makes things alot simpler, i was unaware ot that .. thank you very much !! Quote Link to comment https://forums.phpfreaks.com/topic/285062-add-check-box-names-to-database-table/#findComment-1463712 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.