Jump to content

Add check box names to database table


tascam424

Recommended Posts

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

Link to comment
Share on other sites

No problem; glad to help!  :happy-04:

 

 

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.