Form with checkboxes...[topic solved]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
SirChick
Forum Contributor
Posts: 125
Joined: Tue Jul 31, 2007 11:55 am

Form with checkboxes...[topic solved]

Post by SirChick »

I have a form with 3 check boxes but in my php process im getting this:
Notice: Undefined index: CheckBox1 in C:\xampp\htdocs\lettermanager.php on line 8

Notice: Undefined index: CheckBox2 in C:\xampp\htdocs\lettermanager.php on line 9

Notice: Undefined index: CheckBox3 in C:\xampp\htdocs\lettermanager.php on line 10

This is the check box in the form:

Code: Select all

<input type="checkbox" id="Checkbox1" name="Checkbox1" value="" style="position:absolute;left:100px;top:88px;z-index:0">
<input type="checkbox" id="Checkbox2" name="Checkbox2" value="" style="position:absolute;left:100px;top:255px;z-index:1">
<input type="checkbox" id="Checkbox3" name="Checkbox3" value="" style="position:absolute;left:100px;top:427px;z-index:2">
Then my php process is:

Code: Select all

$MessageOne = $_POST['CheckBox1'];
$MessageTwo = $_POST['CheckBox2'];
$MessageThree = $_POST['CheckBox3'];
These are the 3 lines causing the problem but i do not see why =/
Last edited by SirChick on Wed Oct 17, 2007 12:02 pm, edited 1 time in total.
User avatar
Josh1billion
Forum Contributor
Posts: 316
Joined: Tue Sep 11, 2007 3:25 pm

Post by Josh1billion »

CheckBox1, CheckBox2, and CheckBox3 will only be set if the value is checked, otherwise the PHP form handler won't even know the checkboxes existed.

So change your PHP processing code to this instead:

Code: Select all

if (isset($_POST['CheckBox1']))
  $MessageOne = true;
else
  $messageOne = false;

if (isset($_POST['CheckBox2']))
  $MessageTwo = true;
else
  $messageTwo = false;

if (isset($_POST['CheckBox3']))
  $MessageThree = true;
else
  $messageThree = false;
And those values will be true or false depending on whether the checkbox was ticked by the user.
SirChick
Forum Contributor
Posts: 125
Joined: Tue Jul 31, 2007 11:55 am

Post by SirChick »

Do i still need the:

Code: Select all

$MessageOne = $_POST['CheckBox1'];
$MessageTwo = $_POST['CheckBox2'];
$MessageThree = $_POST['CheckBox3'];
Cos at the moment i got this:
Notice: Undefined variable: MessageOne in C:\xampp\htdocs\lettermanager.php on line 24

Notice: Undefined variable: MessageTwo in C:\xampp\htdocs\lettermanager.php on line 29

Notice: Undefined variable: MessageThree in C:\xampp\htdocs\lettermanager.php on line 35
User avatar
Josh1billion
Forum Contributor
Posts: 316
Joined: Tue Sep 11, 2007 3:25 pm

Post by Josh1billion »

Nope, replace that part with the code I gave you.
SirChick
Forum Contributor
Posts: 125
Joined: Tue Jul 31, 2007 11:55 am

Post by SirChick »

Ok done that but my code isn't working .. well its not a php problem but an sql problem... i think:

Code: Select all

$DeleteMessageOne ="UPDATE messages SET Status = '1' WHERE Reciever= '{$_SESSION['Current_User']}' AND MessageID='$MessageIDOne'";
$result = mysql_query($DeleteMessageOne) or die(mysql_error());
No error is produced but it doesnt set status to 1.

If i set this:

Code: Select all

if (isset($_POST['CheckBox1']))
  $MessageOne = true;
It doesnt equal true when i echo it =/

Could it be my form ?

Code: Select all

<form name="" method="POST" action="lettermanager.php" id="Form1" onsubmit="return ValidateForm1(this)">
<input type="checkbox" id="Checkbox1" name="Checkbox1" value="" style="position:absolute;left:100px;top:88px;z-index:0">
<input type="checkbox" id="Checkbox2" name="Checkbox2" value="" style="position:absolute;left:100px;top:255px;z-index:1">
<input type="checkbox" id="Checkbox3" name="Checkbox3" value="" style="position:absolute;left:100px;top:427px;z-index:2">
<input type="submit" id="Button1" name="Button1" value="Delete Selected" style="position:absolute;left:30px;top:478px;width:119px;height:24px;z-index:3">

</form>
User avatar
Josh1billion
Forum Contributor
Posts: 316
Joined: Tue Sep 11, 2007 3:25 pm

Post by Josh1billion »

In your SQL, you have $MessageIDOne, but in your PHP, you have $MessageOne. If you're passing that variable to an SQL query, you'll probably want to set the values to something like 1 instead of true and 0 instead of false.

And yep, the echoing thing is a problem with your form. You have "Messagebox1" in your form but "MessageBox1" in the PHP code. The B is capital in the PHP code and lowercase in the form. Same with MessageBox2 and MessageBox3.
SirChick
Forum Contributor
Posts: 125
Joined: Tue Jul 31, 2007 11:55 am

Post by SirChick »

$MessageOne is to set true or false..

then if true do the sql.. and $MessageIDOne is carried over from a session so that the sql knows which ID it is meant to be updating...

Code: Select all

<form name="" method="POST" action="lettermanager.php" id="Form1" onsubmit="return ValidateForm1(this)">
<input type="checkbox" id="Checkbox1" name="Checkbox1" value="" style="position:absolute;left:100px;top:88px;z-index:0">
<input type="checkbox" id="Checkbox2" name="Checkbox2" value="" style="position:absolute;left:100px;top:255px;z-index:1">
<input type="checkbox" id="Checkbox3" name="Checkbox3" value="" style="position:absolute;left:100px;top:427px;z-index:2">
<input type="submit" id="Button1" name="Button1" value="Delete Selected" style="position:absolute;left:30px;top:478px;width:119px;height:24px;z-index:3">

</form>
Then i have:

Code: Select all

//created on the same page as the form is on
$MessageIDOne = $_SESSION['MessageOneID'];

if (isset($_POST['CheckBox1']))
  $MessageOne = true;
else
  $MessageOne = false;


if ($MessageOne == true) {
$DeleteMessageOne ="UPDATE messages SET Status = '1' WHERE Reciever= '{$_SESSION[Current_User]}' AND MessageID='$MessageIDOne'";
$result = mysql_query($DeleteMessageOne) or die(mysql_error());
}
User avatar
Josh1billion
Forum Contributor
Posts: 316
Joined: Tue Sep 11, 2007 3:25 pm

Post by Josh1billion »

Like I said at the end of my last post, change the b's in "Checkbox" in the form to capital B's like they are in "CheckBox" in the PHP code.
SirChick
Forum Contributor
Posts: 125
Joined: Tue Jul 31, 2007 11:55 am

Post by SirChick »

Oh right you said messagebox on the first time and confused me lol :P
User avatar
Josh1billion
Forum Contributor
Posts: 316
Joined: Tue Sep 11, 2007 3:25 pm

Post by Josh1billion »

Oops, lol.. I'm mostly a C++ programmer, so I've used MessageBoxes a lot more than I've used Checkboxes. :P

Is it working now though?
SirChick
Forum Contributor
Posts: 125
Joined: Tue Jul 31, 2007 11:55 am

Post by SirChick »

yup thanks.
User avatar
Josh1billion
Forum Contributor
Posts: 316
Joined: Tue Sep 11, 2007 3:25 pm

Post by Josh1billion »

You're welcome. :)
Post Reply