Jump to content

How do I put checkbox data in to database and take it from it?


username01

Recommended Posts

Hi. I want to put data from these checkboxes (example):

Jan (value = jan)

Chris (value = chris)

Tom (value = 50)

Jon (unchecked)

Put janchris50 to the database after finding correct row with user ID, which is user_id for this example.

 

Then, the user goes to another page and he should see this:

Jan (checked)

Chris (checked)

Tom (checked)

Jon (unchecked)

In other words - page should get the information from the database and check the checkboxes that appears to be checked in the database.

 

By the way, it should rewrite the row each time, as the user can modify it.

 

Thanks.

Link to comment
Share on other sites

I need to write data from checked checkboxes into a database table for specific user, which is currently logged in.

And after that, when the user enters the same page next time, look up the database and make the checkboxes he checked appear as checked.

My current code contains not much for the checkboxes, as I have no idea how to do it:

if($session->logged_in){
echo "<div align='center' id='wrap'><form action='process.php' method='post'><fieldset>";
echo "<legend>Choices</legend>";
echo "<p><input class='input' type='checkbox' title='Select anything you want' name='one' id='one' value='a'><label for='one'> Selection #1</label></p>"; // Contains ~10 of these, of course different names.
echo "<input type='hidden' name='subprivilegies' value='1'/><br>";
echo "<input type='submit' value='Submit'/>";
echo "</fieldset></form></div>";

It's basically only html code for the checkboxes...

Link to comment
Share on other sites

Well, you can make your checkboxes all part of an array by naming them the same thing and then adding a bracket set at the end

name="mycheckbox[]" 

but then you will have a separate field in your database for each checkbox.

 

You can name all of the checkboxes the same

name="mycheckbox"

and then save all the values to the one field in the database - then when you go to retrieve the checkbox data you will need a foreach to loop through each needed checkbox and check if its in the array value for your field to determine if it needs to be "Checked" or not...

 

<?php 
					    // set checked field(s) for checkboxes
					    $amounts = array(50,75,100,125,150,200,250,300,500,750,1000); 
					    foreach ($amounts as $amount) {
					      echo '<span class="cb"><input type="checkbox" name="checkbox[]" value="'.$amount.'" style="border:0;" ';
					      if (is_array($row['checkbox']) && in_array($amount,$row['checkbox']))
					        echo 'checked="checked" '; 
					      elseif ( !$edit ) 
					        echo 'disabled="disabled"'; 
					      echo ' /> $'.$amount.'</span>'."\n";
					    } 
					?> 

This example is with generic dollar amounts as the checkbox values -

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.