Checkbox

Update data using checkbox

Page 1 of 1

13 Replies - 3495 Views - Last Post: 19 February 2009 - 10:12 PM Rate Topic: -----

#1 cubeslely  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 16-February 09

Checkbox

Posted 16 February 2009 - 01:38 AM

Hello, I'm a beginner in PHP..

I've got this problem..

My checkbox was displayed using a while loop together with some data, When i clicked a checkbox the code should update that data..
NOw my problem is when i click submit everything was updated not the one that i have checked. I don't know how to link my checkbox to other data or the conditions in mysql. My code goes like this...
while($sql3 = mysql_fetch_array($sql2) {
<td>
	  <input type="checkbox" name="APPROVED[]" value="" />	</td>
	<td><?php echo $sql3['USER_NAME']; ?></td>
	<td><?php echo $sql3['USER_ID']; ?></td>
	<td><?php echo $sql3['PASSWORD']; ?></td>
<?php	
if(isset($_POST['APPROVED'])){
$app = "Update approved_users set IS_APPROVED ='Y' where USER_ID = '".$sql3['USER_ID']."' ";
if(mysql_query($app))
{

echo "Account name can now be use!";
} 
else
{ 
echo $app;
die("Error: ". mysql_error());}

		  }	 ?>




Please help me...Thanks in Advance

Is This A Good Question/Topic? 0
  • +

Replies To: Checkbox

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3039
  • View blog
  • Posts: 10,616
  • Joined: 08-August 08

Re: Checkbox

Posted 16 February 2009 - 06:08 AM

Does this explain it?
http://www.dreaminco...wtopic86370.htm
Was This Post Helpful? 0
  • +
  • -

#3 cubeslely  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 16-February 09

Re: Checkbox

Posted 16 February 2009 - 08:28 PM

View PostCTphpnwb, on 16 Feb, 2009 - 05:08 AM, said:




Hello,

Thanks for your reply,

I try your code but i cant get it work. There was an error but I can't fix it.

[error]
Notice: Undefined index: Item_Zero in C:\wamp\www\Checkbox2.php on line 8

Notice: Undefined variable: checked in C:\wamp\www\Checkbox2.php on line 16

Notice: Undefined index: Item_One in C:\wamp\www\Checkbox2.php on line 8

Notice: Undefined variable: checked in C:\wamp\www\Checkbox2.php on line 16

Notice: Undefined index: Item_Two in C:\wamp\www\Checkbox2.php on line 8

Notice: Undefined variable: checked in C:\wamp\www\Checkbox2.php on line 16

Notice: Undefined index: Item_Three in C:\wamp\www\Checkbox2.php on line 8

Notice: Undefined variable: checked in C:\wamp\www\Checkbox2.php on line 16

Notice: Undefined index: Item_Four in C:\wamp\www\Checkbox2.php on line 8

Notice: Undefined variable: checked in C:\wamp\www\Checkbox2.php on line 16[/error]
Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3039
  • View blog
  • Posts: 10,616
  • Joined: 08-August 08

Re: Checkbox

Posted 16 February 2009 - 08:51 PM

It works for me, but I think I know what the problem is. When you copy/paste code from this site the tab characters get replaced by hidden characters. Try the attached file.

Attached File(s)


Was This Post Helpful? 0
  • +
  • -

#5 cubeslely  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 16-February 09

Re: Checkbox

Posted 16 February 2009 - 10:06 PM

View PostCTphpnwb, on 16 Feb, 2009 - 07:51 PM, said:

It works for me, but I think I know what the problem is. When you copy/paste code from this site the tab characters get replaced by hidden characters. Try the attached file.


Again Thank you..

But I have encountered the same error again..
But when i click the checkbox the error disappear,
I don't know how to fix but the problem is, the code didn't recognize the variable $checkbox unless it was POSTED or Submit...

I'm just a beginner so thanks for your help
Was This Post Helpful? 0
  • +
  • -

#6 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3039
  • View blog
  • Posts: 10,616
  • Joined: 08-August 08

Re: Checkbox

Posted 16 February 2009 - 11:26 PM

Post the code EXACTLY as you run it.
Was This Post Helpful? 0
  • +
  • -

#7 cubeslely  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 16-February 09

Re: Checkbox

Posted 16 February 2009 - 11:49 PM

This is the exact code.

<?php
$checkboxname = array("Item_Zero","Item_One","Item_Two","Item_Three","Item_Four"); // Array with check box names. Five items = five check boxes.
var_dump($_POST); // Here so that you can see the POST values, which depend on what is checked. Items unchecked will not be in POST.
$output = '';
$output .= '<form method="post" action=" '.$_SERVER['PHP_SELF'].'">
';	
foreach	($checkboxname as $checkbox) {
	if($_POST[$checkbox]=="on") 
		{
		$checked = "checked"; // if box is checked, set $checked to "checked"
		} else {
		unset($checked); // if box is unchecked, set $checked to null.
		}
	$selection = $checkbox.", or any text you like.
"; // The text that the user sees can be anything. Here were using the checkbox name so that you can see that it matches up.
	$output .= "<input type ='checkbox' ".$checked." name= ".$checkbox." >".$selection."<br>"; // Having set the variables, we set the checkbox html.
	} // This is the end of the loop. We either start over on the next box, or after the last one, we proceed to the submit button.
$output .= '<input type="submit" name="Submit" value="Click Here when boxes have been selected"></form><br>'; // This sets up the submit button.

echo $output;
?>



Was This Post Helpful? 0
  • +
  • -

#8 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3039
  • View blog
  • Posts: 10,616
  • Joined: 08-August 08

Re: Checkbox

Posted 17 February 2009 - 06:30 AM

Ah, I found the problem. It's your setup's error reporting level. Try putting this line at the beginning:
error_reporting(E_ALL ^ E_NOTICE);

By the way, are you not running Apache web server? This appears to be a problem with the default configuration of IIS servers. You may need to turn on register_globals, depending on your server version.

See these links:

http://bytes.com/gro...efined-variable

http://forums.devart...cript-5156.html

http://www.webmaster...orum88/4673.htm

http://www.php.net/register_globals
Was This Post Helpful? 0
  • +
  • -

#9 cubeslely  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 16-February 09

Re: Checkbox

Posted 17 February 2009 - 08:46 PM

Hello CTphpnwb,

Thank you!

I get it work. But it doesnt solve my problem, I cant relate it to my program..Sorry...

Now my problem with my code is i dont know how to link the checkbox to my data so that if i click that specific checkbox the data assigned to it was the only data that will be updated. This is my code,

<?php while($sql3 = mysql_fetch_array($sql2) and  $select3 = mysql_fetch_array($select2) ){
?>
<tr>
	<td>
	  <input type="checkbox" name=".$checkbox."/>	</td>
	<td><?php echo $sql3['USER_NAME']; ?></td>
	<td><?php echo $sql3['USER_ID']; ?></td>
	<td><?php echo $sql3['PASSWORD']; ?></td>
	<td><?php echo $select3['COMP'];	
	   $app = "Update approved_users set IS_APPROVED ='Y' where USER_ID = '".$sql3['USER_ID']."'";
					if(mysql_query($app))
					{
						echo $app;
						echo "Account name can now be use!";
					} 
					else
					{ 
						echo $app;
						die("Error: ". mysql_error());
					}


}




When I submit the button everything was Updated. What conditions should I use. I cant figure it out...
Was This Post Helpful? 0
  • +
  • -

#10 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3039
  • View blog
  • Posts: 10,616
  • Joined: 08-August 08

Re: Checkbox

Posted 17 February 2009 - 09:47 PM

1. Does your while loop work? If so, what are $sql3 and $select3?
Try something like this just before your code:
while($sql3 = mysql_fetch_array($sql2) and $select3 = mysql_fetch_array($select2) ){
	echo "sql3:<br>";
	var_dump($sql3);
	echo "<br>select3<br>";
	var_dump($select3);
	echo "<br><br>";
}


Does it output what you'd expect for those arrays?

2. Get rid of the table until you've got your code working. All those extra formatting tags '<td></td>' are distracting, and you don't need distractions.

3. Breaking in and out of html/php is also distracting and it's bad form. Look at how I did my checkbox example. It's all in php, and I output to html at the very end. You don't have to go that far, but as a rule you should try to only have one set of php tags in your code: beginning and end.

4. Plan your check list the way mine is done:
	A. Setup the form
	B. Start loop
	C. Test to see if the selection should be checked
	D. Create the appropriate "<input type='checkbox' ".$checked." name....
	E. Complete loop
	F. Create submit button
	G. Close form


5. I see no:
$checked = "checked";
or anything like it in your code. We'll get to that when you've done some of 1 through 4. ;)
Was This Post Helpful? 0
  • +
  • -

#11 cubeslely  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 16-February 09

Re: Checkbox

Posted 19 February 2009 - 01:22 AM

hello again CTphpnwb,

I did your step by step advice and this is my code.

<?php require("connect.php");
$checkboxname = array("Item_Zero","Item_One"); 
var_dump($_POST); 
$output = '';
$output .= '<form id="form1" name="form1" method="post" action=" '.$_SERVER['PHP_SELF'].'">';
$sql = "Select USER_NAME, USER_ID, PASSWORD from approved_users  where IS_APPROVED = 'N' ";
		$company = "Select CLIENT_ID FROM  approved_users  where IS_APPROVED = 'N'";
		if(!$sql2 = mysql_query($sql,$con) and !$company2 = mysql_query($company,$con))
		{
		die ("error1 :".mysql_error());
		}
 
foreach ($checkboxname as $checkbox){
	if($_POST[$checkbox]=="on")
		{
		$checked = "checked"; 
		} else {
		unset($checked); 
		}
		$selection = $checkbox.", or any text you like."; 

$output .=  "<input type='checkbox' ".$checked." name=".$checkbox."/>".$selection."<br>";
}
 while($sql3 = mysql_fetch_array($sql2) ){
 $output .=  $sql3['USER_NAME']."  ";
$output .=  $sql3['USER_ID']."  "; 
$output .=  $sql3['PASSWORD']."  "; 
//$output .=  $select3['COMP']."<br>";
}
	 

$output .= "<input type='submit' name='Approve' value='Approve' /></form>";
echo $output;
 mysql_close($con);
 ?>



My loops are working already and I eliminate everything you've said. Now
how should i link $checked = "checked" to my data.. I just incorporate your code to my program...

Thanks for your help.. :)
Was This Post Helpful? 0
  • +
  • -

#12 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3039
  • View blog
  • Posts: 10,616
  • Joined: 08-August 08

Re: Checkbox

Posted 19 February 2009 - 07:55 AM

I don't know what you're doing here, so I commented it out in my version:
/*		if(!$sql2 = mysql_query($sql,$con) and !$company2 = mysql_query($company,$con))
		{
		die ("error1 :".mysql_error());
		} */

You had a '/' in this line that was causing a problem:
$output .= "<input type='checkbox' ".$checked." name=".$checkbox.">".$selection."<br>";


Was This Post Helpful? 0
  • +
  • -

#13 cubeslely  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 16-February 09

Re: Checkbox

Posted 19 February 2009 - 09:08 PM

hello,

Your help is very much welcome,
Thanks ...

What i want to happen is that in this snippet is to update the one that i have check. Since it is place inside the while loop everything was updated. Can you please help with the conditions. Thank you..
if(isset($selection)){
$app = "Update approved_users set IS_APPROVED ='Y' where USER_NAME = '".$selection."' ";
mysql_query($app,$con);
}



I'm getting impatient with this, I can't get it work.This is the exact code...

<?php require("connect.php");
$i=0;
$checkboxname; 

var_dump($_POST); 
$output = '';
$output .= '<form id="form1" name="form1" method="post" action=" '.$_SERVER['PHP_SELF'].'">';
$sql = "Select USER_NAME, USER_ID, PASSWORD from approved_users  where IS_APPROVED = 'N' ";
		$company = "Select CLIENT_ID FROM  approved_users  where IS_APPROVED = 'N'";
		if(!$sql2 = mysql_query($sql,$con) and !$company2 = mysql_query($company,$con))
		{
		die ("error1 :".mysql_error());
		}
  while($sql3 = mysql_fetch_array($sql2) ){
 $output .=  $checkboxname[$i]=$sql3['USER_NAME']."  ";
$output .=  $sql3['USER_ID']."  "; 
$output .=  $sql3['PASSWORD']."  "; 
//$output .=  $select3['COMP']."<br>";

foreach ($checkboxname as $checkbox){
			$selection = $checkbox; 
$output .=  "<input type='checkbox' name=".$checkbox."/>".$selection."<br>";

if(isset($selection)){
$app = "Update approved_users set IS_APPROVED ='Y' where USER_NAME = '".$selection."' ";
mysql_query($app,$con);
}

   }
	}
$output .= "<input type='submit' name='Approve' value='Approve' /></form>";
 


	echo $output; 
	
 mysql_close($con);
 ?>


Was This Post Helpful? 0
  • +
  • -

#14 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3039
  • View blog
  • Posts: 10,616
  • Joined: 08-August 08

Re: Checkbox

Posted 19 February 2009 - 10:12 PM

What is this line?
$checkboxname;

Why are you setting up two queries inside a form?

What is this line??
$output .= $checkboxname[$i]=$sql3['USER_NAME']." ";
That's like saying a .= b = c
It doesn't make sense even if you're trying to make $output .= $checkboxname[$i].$sql3['USER_NAME']." ";

Why do you then add $sql3['USER_ID']." " and $sql3['PASSWORD']." " to $output??

If you set $selection = $checkbox;

then $selection will be set, so the if statement that follows will always be true, but $selection won't be a USER_NAME. It will be $checkbox.

I think you need to study my example some more. Make changes to it and see what happens. Try it on your own when you understand the example better.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1