views:

84

answers:

9

Hello here!

I have a dynamic input in my php code like this one:

<input type="checkbox" id="advantage[]" name="advantage[]" value="Special Option" />

And I need to know if it's checked... I can have multiple checks in the same format in the code, my brain hurts because i can't find a solution!

Thanks 4 the help!

A: 

If you're trying to determine if it's checked after postback, checkbox value data is only passed to the server in $_POST when it is checked. Otherwise the value is empty or null.

If you're trying to determine if it's checked before postback, you will need to write some Javascript in PHP that will be processed client-side.

scrumpyjack
A: 

You can use :checked

http://api.jquery.com/category/forms/

DavidYell
A: 

If you want to do it with jQuery follow this link and you will be able to see how it is done:

JQuery HowTo: How to check if checkbox is checked using jQuery

Checked selector - jQuery

Ladislav
A: 

You can do like:

$(function(){
  $('input[type="checkbox"]').each(function(){
    if ($(this).is(':checked'))
    {
      alert('Checked');
    }
    else
    {
      alert('Not Checked');
    }
  });
});

Or even based on value if is is same across all checkboxes:

$(function(){
  $('input[value="Special Option"]').each(function(){
    if ($(this).is(':checked'))
    {
      alert('Checked');
    }
    else
    {
      alert('Not Checked');
    }
  });
});
Sarfraz
I don't think that's what the OP wants. I think he wants to see the state of a specific checkbox by only knowing its value. He doesn't want to know the state of all checkboxes on the page.
Felix
@Web Logic: I think by "same format" he meant same id/name. Also, it makes the most sense.
Felix
This solution worked! Thank you so much!
Metal Sonic
A: 

You can't/shouldn't have multiple checkboxes in this format because the id has to be unique.

var isChecked = $('#advantage\\[\\]').is(':checked');
Darin Dimitrov
Why has this been downvoted?
Darin Dimitrov
Bad solution, as the OP obviously has more than one `#advantage[]` elements (although he shouldn't).
Felix
He shouldn't have multiple `#advantage[]` in his document.
Darin Dimitrov
I agree with Darin, Id's are unique, and there should be only one instance of an Id in a given document
Ladislav
That's what I said myself, but people don't respect standards. And I think it's obvious he has more than one, since `[]` is used for array definitions.
Felix
A: 
var isSpecialOptionSet = $("input[value='Special Option']:checked").length ? true : false;
// isSpecialOptionSet will now be true/false according to the state of the checkbox.
Felix
Why the downvote?
Felix
The variable `isSpecialOptionSet` will always be equal to just one element's state overwriting previous values and ending at the last checkbox.
Web Logic
If my assumption that the value is unique is correct, then my answer is correct and this is the shortest, easiest way of doing what the OP wants.
Felix
@Felix: If you know php, `[]` is used to create an array, he has more than one there, so your answer is not correct. Although id should be used only once.
Web Logic
@Web Logic: are you confusing value and name here? You use [] to collect the values of the checked boxes into an array. It makes zero sense to use the same name-value pair for two checkboxes.
Tgr
@Web Logic: I'm sorry, but please read the f-ing answer before commenting. I know the ID is not unique. I said the ***value*** is *probably* unique. As **Tgr** said (thanks btw), I think you're confusing value with id.
Felix
A: 
var isChecked = $('input:checkbox[value="Special Option"]').is(":checked");
Álvaro G. Vicario
The variable `isChecked` will always be equal to just one element's state overwriting previous values and ending at the last checkbox.
Web Logic
Exactly. Is anything wrong with that?
Álvaro G. Vicario
@Web Logic: you should leave this answer. Really.
Felix
A: 

Client-side:

$('input[type=checkbox][name="advantage[]"][value="Special Option"]').is(':checked')

Server-side:

in_array('Special Option', $_POST['advantage'])

Note that you shouldn't use the same id multiple times.

Tgr
A: 

You set in HTML form "advantage[]" witch becomes in php an array. You need to tell what is the value of that specific part.

eg: "advantage['checkbox1']" then in php you will get

$_POST['advantage']['checkbox1'] will be equal to 'Special Option'; if that checkbox is checked.

on the other hand if you can't modify:

<?php
if(in_array('Special Option', $_POST['advantage'])){
       // checkbox is checked
       // it can only be identified by an id witch you will receive by
       // foreach($_POST['advantage'] as $id => $val);
}
?>
Mihai Iorga