Log in

View Full Version : If Else Input checked?



mikster
08-30-2011, 07:57 PM
Hi,

How would I write a php if else statement to check to see if the INPUT checkbox is clicked then Do this.

I have a category input check box which I want to expand into a sub category once someone clicks on this check box.

Sorry about the bad english LOL...i hope you understand what I mean.

Thanks in advance.

JShor
08-30-2011, 08:42 PM
The PHP solution assumes that you submitted a form with a checkbox. If you want live results (as soon as it's clicked, an event is triggered), you need to use JavaScript.

PHP solution:


<?php

if(isset($_POST['chkbx'])) {
echo "Checkbox was set";
}

?>
<input type="checkbox" name="chkbx" value="1" />


JavaScript solution:


<script type="text/javascript">
function isChecked(obj) {
if(obj.checked === true) {
// Perform event.
}
}
</script>

<input type="checkbox" name="chkbx" value="1" onclick="isChecked(this)" />

mikster
08-30-2011, 11:22 PM
Hi thanks for the help...one question.....what if i have multiple checkbox would I have to give a unique name to the functions for each??

JShor
08-31-2011, 01:00 AM
No. You can have as many checkboxes as you want.

This would be perfectly valid:


<script type="text/javascript">
function isChecked(obj) {
if(obj.checked === true) {
// Perform event.
}
}
</script>

<input type="checkbox" name="chkbx" value="1" onclick="isChecked(this)" />
<input type="checkbox" name="chkbx2" value="2" onclick="isChecked(this)" />
<input type="checkbox" name="chkbx3" value="3" onclick="isChecked(this)" />


Unless you wanted to execute different events for each different checkbox. Then you would need to write new functions, obviously.

mikster
08-31-2011, 02:44 AM
Hi Jshor,

I'm anaylizing your code and it looks like what it does is that when ever there's a TRUE to checkbox it "Performs event". Sorry for not making it clearer in the beginning...I don't think it will work for what i want. I have let's say 2 categories with 2 sub categories from the main ones. SO

Category 1
CARS
-BMW

Category 2
PLANES
-F1 Tiger

From what I see in your code it performs 1 event when = TRUE....I need a code or a modified one from yours to do multiple events depending on which checkbox is clicked. :D

Thanks alot again Jshor







No. You can have as many checkboxes as you want.

This would be perfectly valid:


<script type="text/javascript">
function isChecked(obj) {
if(obj.checked === true) {
// Perform event.
}
}
</script>

<input type="checkbox" name="chkbx" value="1" onclick="isChecked(this)" />
<input type="checkbox" name="chkbx2" value="2" onclick="isChecked(this)" />
<input type="checkbox" name="chkbx3" value="3" onclick="isChecked(this)" />


Unless you wanted to execute different events for each different checkbox. Then you would need to write new functions, obviously.