Jump to content

[SOLVED] retrieving checkboxes checked


kaozdragon

Recommended Posts

not quite too sure how to explain this but..i'll try the best i can with what i'm doing.

 

i'm making a database (obviously) where i want the user to be able to register whether they want notifications with a checkbox.  so how i did that is i made a checkbox that if it's checked it will return the value "Yes" to the db.  now i want the user to be able to change that when they want.  how do i make another page to retreive the checkbox already checked and if they want to update it, they can uncheck it and update it.

 

thanks in advance and i hope this is clear enough.

Link to comment
Share on other sites

You'd just need to create a query that updates that from Yes to No (assuming that's the choices).

 

 

// set a variable for their id that's passed through the URL

$id = $_GET['id'];

$sql = "UPDATE tablename SET columnname WHERE id='$id'

 

This assumes that you provide them a simple form that passes their id through.

Link to comment
Share on other sites

<input type="checkbox" name="name" value="1">

 

If the box is checked, the value is 1, if it's not checked it will have no value.

 

If the value is stored in your database, just select it, say if the value in the database equaled one then the box would be checked:

 

$sql = "SELECT * FROM `tbl_name` WHERE `value`= 'value'";
$res = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_assoc($res);

if($row[name] == 1){
$check = " CHECKED";
}else {
$check = "";
}

echo "<input type=\"checkbox\" name=\"name\" value=\"1\"$check>\n";

 

Then passing it:

 

$name = $_POST['name'];

if($name){
  $sql = "SELECT * FROM `tbl_name` WHERE `value` ='value'";
  $res = mysql_query($sql) or die(mysql_error());
  $row = mysql_fetch_assoc($res);
    if($name == $row[name]){
    //no need to update
    }else {
    //update query
    }
}else {
//no update
}

Link to comment
Share on other sites

Also if you were to have multiple checkboxes [say you were deleting pieces of mail]

 

<input type="checkbox" name="mail[]" value="1">
<input type="checkbox" name="mail[]" value="2">
<input type="checkbox" name="mail[]" value="3">
<input type="checkbox" name="mail[]" value="4">
<input type="checkbox" name="mail[]" value="5">

 

Then:

 

$mail = $_POST[mail];

if($mail){
echo "You selected:<br>\n";
  foreach($mail AS $mailid){
  echo $mailid . "<br>\n";
  }
}else {
echo "No mail selected\n";
}

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.