Check Box help

A

Anonymous

Guest
I know this is real simple, but I am a noob and just can't figure it. I have a checkbox on a form
<b>Active : </b><input type="checkbox" name="active" value="ON"> to show if a member is active or not. I have a field in a mysql table called "active". I want to store a "Y" if the box is ticked and an "N" if it is not ticked.

All my attempts at storing the Y or N have failed so far. Any hints would be greatly appreciated.
 
Try this out (Example):
PHP:
<?
if($active == "ON"){
mysql_query("UPDATE table SET active = 'Y' where userid='whatever'");
}
else{
mysql_query("UPDATE table SET active = 'N' where userid='whatever'");
}
?>
 
Thanks so much .. I was close, I was forgetting the double = sign
 
What I ended up doing with the check box was:-

if ($_POST['active'] == "ON") {
$a = "Y";
} else {
$a = "N";
}

as this is for the first addition of the member information rather than an update the form submit then did this:-

$query = "INSERT INTO members (member_name, active, join_date, email1, email2 ) VALUES ('$m', '$a', NOW(), '$e1', '$e2' )";
$result = @mysql_query ($query); // run the query

I get an error returned when the check box is not ticked, error says "Undefined index: active" but the data has been added to the table correctly. If the check boxes are ticked there are no errors. Any ideas why I get this error?
 
Yeh, of course!
We cannot do
PHP:
if ($_POST['active'] == "ON")
, it will be always true even if the checkbox isn´t checked. 'ON' is its value and doesn´t tell us if it´s checked or not :p

hunn.... I don´t know how can we verify if some checkbox is checked or not with php! You can use an 'radio button' instead. It´s better for checking! Or use javascript for this one!

Anyway, that error is only a developement warning and isn´t dangerous like call of uninitialised var or index.

Changing error_reporting = E_ALL to error_reporting = E_ALL & ~E_NOTICE in the php.ini will prevent some debugging informations like notices to be diaplayed.

Just a word of caution, i know/think you don´t have access to the php.ini :p
 
gesf said:
Anyway, that error is only a developement warning and isn´t dangerous like call of uninitialised var or index.

Changing error_reporting = E_ALL to error_reporting = E_ALL & ~E_NOTICE in the php.ini will prevent some debugging informations like notices to be diaplayed.

Development environments should always be set to E_ALL. Notices don't prevent your code from running, but they do alert you to weaknesses/inconsistencies/ineffieciencies in your code, such as uninitialized variables, etc. Just because they don't break your code doesn't mean you shouldn't fix them.
 
The error is being reported back to me from my own config file that reorts the error back to me. If I rem this out then I get no errors.

But the code works anyway, it does set "Y" if checked and "N" if not checked, so I think I will leave it as is.

Thanks for your help.
 
Taipan said:
The error is being reported back to me from my own config file that reorts the error back to me. If I rem this out then I get no errors.

Wait, the errors are coming from a different file? Could you post the code of that file, and the exact error including the line number?
 
This is a file for error reporting:-
Code:
<?php # Script 12.3 - config.inc

// this script sets the error reporting and logging for the site.

//error_reporting(0); // Production Level
error_reporting(E_ALL); // Development level

// Use my own error_handling function.
function my_error_handler ($e_number, $e_message) {

        $message = 'An error occured in script ' . __FILE__ . ' on line ' . __LINE__ . ": $e_message";
        //error_log ($message, 1, 'taipan@heng.net.au'); // Production (send email)
        echo '<font color="red" size="+1">', $message, '</font>'; // Development (print error in red)
}
set_error_handler('my_error_handler');

And this is the error I get:-
An error occured in script /home/virtual/site20/fst/home/#*&%/public_html/admin/includes/config.inc on line 11: Undefined index: active

Thanks.
 
You don´t need javascript anymore :p
In the link i´ve posted you can find the equivalent php code by swirlee!
 
Back
Top