Welcome Guest, Not a member yet? Register   Sign In
How to know if checkbox is checked and which dropdown value is selected
#1

[eluser]bleu[/eluser]
In my form I want to run certain code when certain things are checked or selected therefore , how to know if checkbox is checked when form is submitted using codeigniter $this->input->post() and how to know what dropdown value is selected using post.
#2

[eluser]PhilTem[/eluser]
Do you want to run them live (i.e. as soon as the user selects any checkbox or changes any dropdown's value)? Or run them after form dispatch?
In the first case it'll be jQuery to refer to, in the second case you'll just have to do something like this in pseudocode

Code:
if ( SomeCheckboxSelected )
{
  Do some actions
}

if ( DropdownHasValue5 )
{
  Do some other fancy action
}
#3

[eluser]Samus[/eluser]
Give your checkbox a default value, like '1' or 'true' or whatever.

If the checkbox is checked the value will be submitted in the post, if it is unchecked nothing will come in the post for that field.

e.g

Code:
<?= form_checkbox('check', 1); ?>

produces: (assuming your using form helper)

Code:
<input type="checkbox" name="check" value="1" />

If the checkbox is checked:
$_POST['check'] = 1;
else
$_POST['check'] would be null

So all you have to do is

Code:
if($this->input->post('check')) :
// do something
else :
// do something else
endif;
#4

[eluser]CroNiX[/eluser]
Code:
if ($this->input->post('name-of-checkbox') === FALSE)
{
  //the checkbox was NOT submitted, meaning it wasn't checked
}
#5

[eluser]bleu[/eluser]
[quote author="CroNiX" date="1333497200"]
Code:
if ($this->input->post('name-of-checkbox') === FALSE)
{
  //the checkbox was NOT submitted, meaning it wasn't checked
}
[/quote]

Should this work
Code:
if ($this->input->post('checkbox', TRUE) === true) {echo 'hi';}

where my checkbox has no value but it is checked


The above does not output hi

also
Code:
if($this->input->cookie('viewlogin', TRUE) === true){echo "hi";}
does not work

how will I check for cookies in an if condition

Is there a list as to how to check for get,post,cookie in if conditions and elsewhere
#6

[eluser]CroNiX[/eluser]
No, that won't work using === TRUE, because it will be whatever value your checkbox field is if it WAS sent. If it WASN'T sent, input() will return boolean FALSE. Remember, checkboxes don't get submitted if it isn't checked in accordance with the HTML specs because it's not considered a "successful control".

Code:
<input type="checkbox" name="name-of-checkbox" value="red" />

$checkbox = $this->input->post('name-of-checkbox', TRUE);
if ($checkbox === FALSE)
{
  //The checkbox was NOT submitted, meaning it wasn't checked
  //Handle it if need be  
}
else
{
  //It was submitted
  //Output the value of the checkbox
  echo $checkbox;  //outputs "red"
}
#7

[eluser]InsiteFX[/eluser]
Checkbox trick!

Give them both the same name...
Code:
<input type="hidden" name="checkbox1" value="0" />
<input type="checkbox" name="checkbox1" value="1" />
#8

[eluser]bleu[/eluser]
[quote author="CroNiX" date="1334768664"]No, that won't work using === TRUE, because it will be whatever value your checkbox field is if it WAS sent. If it WASN'T sent, input() will return boolean FALSE. Remember, checkboxes don't get submitted if it isn't checked in accordance with the HTML specs because it's not considered a "successful control".

Code:
<input type="checkbox" name="name-of-checkbox" value="red" />

$checkbox = $this->input->post('name-of-checkbox', TRUE);
if ($checkbox === FALSE)
{
  //The checkbox was NOT submitted, meaning it wasn't checked
  //Handle it if need be  
}
else
{
  //It was submitted
  //Output the value of the checkbox
  echo $checkbox;  //outputs "red"
}
[/quote]


Then


I believe this is also okay
Code:
$checkbox = $this->input->post('name-of-checkbox', TRUE);
if (empty($checkbox))
{
  //The checkbox was NOT submitted, meaning it wasn't checked
  //Handle it if need be  
}
#9

[eluser]CroNiX[/eluser]
@bleu, If you used empty() then your value couldn't be a 0, which is a legitimate value.




Theme © iAndrew 2016 - Forum software by © MyBB