how to send checkbox value 1 or 0 in database? [duplicate] - php

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 7 years ago.
<input type="checkbox" name="chkbx" value="true" checked>
$data['active'] = $this->input->post('active');
if($data['active'] == true){
echo $data['active'] = 1 ;
}
else{
echo $data['active'] = 0 ;
}
It always put 0 value in database. I can't understand.
please help me to solve this problem.

Checkboxes are posted only when they are checked.
Use isset()
Use proper checkbox name.
$data['active'] = $this->input->post('chkbx');
if (isset($data['active'])) {
echo $data['active'] = 1;
}
else {
echo $data['active'] = 0;
}

Your checkbox name name="chkbx" and $this->input->post('active') is different. Use the same and check.

According to docs "The main advantage of using the provided functions rather than fetching an item directly ($_POST['something']) is that the functions will check to see if the item is set and return false (boolean) if not".
try using
if($data['active'] !== false){
// Check box value is set check its value and insert in DB here
}
else{
// Check box not set you can have your fall back method
}

Related

undefined index in php page [duplicate]

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 5 years ago.
I am getting error message :
undefined index:
in my code. Below is the function on the line
if($user['flagged'] == 1){
$flag = "Your last payment has been flagged as NOT RECEIVED.";
}
it says Undefined index: flagged
How can this be fixed
Just check if msg index of global array $_GET is set. For example
if(isset($_GET['msg']) && $_GET['msg']==="success")
I think it will solve your problem.
use isset() to check it first
if(isset($_GET['msg']) && $_GET['msg']==="success"){
$log_prompt = '<span style="color:red">You Have successfully registered. Login Now!</span>';
}
if the first condition isset($_GET['msg']) returned false, it will escape the second part of the if condition and will not print your message.
read more about Logical Operators
When you load the page the URL must me something like this:
localhost/your_page.php?msg=something_value
But your app mustn't return error when in URL there isn't ?msg=value... You have to use isset()
if(isset($_GET['msg']) && $_GET['msg'] == "success")
=== is recommended
if(isset($_GET['msg']) && $_GET['msg'] === "success")
More info: http://php.net/manual/en/language.operators.logical.php

Undefined index variable in $_REQUEST in PHP, getting a boolean value when using ISSET() [duplicate]

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 7 years ago.
I have used $value = (isset($_REQUEST['value'])) to define the index variable. However, $value variable shows as a type Boolean and value of 0 or 1 in the debugger, which results in not getting the correct results when the $value is used with a (if) statement.
$page_limit = (isset($_REQUEST["list_page"]));
$viewdate = (isset($_REQUEST["viewdate"]));
How do I correct the following so that the index is defined?
if($_REQUEST["viewdate"] == '') {
$viewdate = 'All';
} else {
$viewdate = $_REQUEST["viewdate"];
}
$targetpage = "newindex.php?viewdate=".$_REQUEST["viewdate"]."&search=Search";
$page = (isset($_REQUEST['page']));
if($page_limit == '') {
The code above works, without the isset() function, but displays Notice - E messages
You should use isset to test whether a variable has been set, then assign the value. Like so:
$something = null;
if (isset($_REQUEST['something'])) {
$something = $_REQUEST['something'];
...
}
// Later in the code
if ($something !== null) {
// Do stuff

How to Avoid Notice: Undefined index in php and set default value [duplicate]

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 7 years ago.
I want a default value and get rid off the notice. Below is my code
<?php
$color = $_POST["color"]; //initialize variable
if (!isset($color)){ // if nothing selected set default to Blue
$color = 'Blue';
}
echo $color;
?>
So when I run the code and nothing is selected and the blue value (as it is set for default) does show up but the notice message also shows up. I want to get rid of the notice message completely. I have tried array_key_exists as well. Still can not get rid of the notice.
below is the notice I'm getting.
Notice: Undefined index: color in C:\xampp\htdocs\test3\Assignment_1_Q2.php on line 17
Thank you in advance.
Try this, First check the value with isset function.
<?php
$color = ""; //initialize variable
if(isset($_POST["color"]))
{
$color = $_POST["color"];
}
else
{
// if nothing selected set default to Blue
$color = 'Blue';
}
echo $color;
?>

Undefined index on page load [duplicate]

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 8 years ago.
the message is
Notice: Undefined index: flag in C:\xampp\htdocs\myfiles\mobile tracking\index.php on line 63
my code is
<?php
$stat=$_REQUEST['flag'];
if($stat=="FAILED")
{
echo "Username/password doesnot exists";
}
?>
The global variable $_REQUEST['flag'] is probably having value NULL. This is the reason you are getting this error. Well, try using isset(). to check whether the variable is having any value or not.
You should check if the $_REQUEST['flag'] variable has been set:
<?php
$stat= ( isset($_REQUEST['flag']) ? $_REQUEST['flag'] : null) ;
if($stat=="FAILED")
{
echo "Username/password doesnot exists";
}
?>
You received a notice because you didn't initialized the values of the array. Use this construction to prevent them.
if (! array_key_exists('flag', $_REQUEST)) {
$_REQUEST['flag'] = whatever value goes here;
}

PHP Undefined Index [duplicate]

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 4 years ago.
I have this code and it's giving me an undefined error if country is not a variable in the URL. I've never had this problem before so it's obviously something to do with server settings but I'd rather not change those because this is probably done for security purposes. I've searched Google and can't find the workaround, although I'm sure it's blindingly obvious!
$country = $_GET['country'];
if(!$country){
$incoming = $outgoing = $sms = $free = "---";
} else {
get_rates($country);
}
you should use the following approach:
if (!isset($_GET['country'])) {
$incoming = $outgoing = $sms = $free = "---";
} else {
get_rates($_GET['country']);
}
isset allows you to check if the variable exists (if not we give it the value false).
$country = isset($_GET['country'])? $_GET['country'] : false;
"Undefined index" means that element doesn't exist in the array. Your server is probably set to report these errors, while your system so far wasn't. Check if the element exists before accessing it.
$country = null;
if (!empty($_GET['country']) {
$country = $_GET['country'];
}
You should probably check to see if $_GET['country'] is set. I would wrap the entire code block you posted in an if statement that checks if the country field is set, using the isset function.
Just change:
$country = $_GET['country'];
To this:
$country = #$_GET['country'];
'#' is an operator which will remove errors output.

Categories