-1

I am not sure if I am going about this correctly. I have a set of checkbox inputs. If someone selects the last check box all_users_check, I want a new form to appear where I will be listing all of the users in a drop down (haven't added the drop down yet). I thought I could do this by using the name of the input, but I am mistaken apparently as I am getting this error..

How else could I structure what I am doing so that if someone checks that option the new form displays?

<div class="user_dropdown">
<form action="">
  <input type="checkbox" name="spectator_check" value=""> Spectators<br>
  <input type="checkbox" name="member_check" value="" checked> Team Members<br>
  <input type="checkbox" name="commissioner_check" value="" checked> Commissioner(s)<br>
   <label for="all_users_check">
  <input type="checkbox" name="all_users_check" value="" checked> Individual User<br>
  </label>
</form>
</div>

<script>
$(".user_dropdown").hide();
$(".all_users_check").click(function() {
    if($(this).is(":checked")) {
        $(".user_dropdown").show();
    } else {
        $(".user_dropdown").hide();
    }
});
</script>

This is how the page looks on load. Those fields are already checked for some reason.

enter image description here

Ralph
  • 307
  • 1
  • 2
  • 14
  • 1
    Shouldn't it be `isset($_GET['all_users_check']`? You also have no submit button, are you going to process this with AJAX? – chris85 Sep 11 '15 at 16:10
  • I wasn't trying to submit anything with this yet. I just want what is inside of the isset to show up if that checkbox is checked. I do not want the user to have to hit a submit button for the future drop down box to show up. Is AJAX the only way I can make this show with just clicking the checkbox input? – Ralph Sep 11 '15 at 16:11
  • 1
    PHP is server side. Once the page is loaded it is not accessible. An AJAX request will be required, or you could do this with JS/jquery. – chris85 Sep 11 '15 at 16:14
  • The data will already be there though. I am just wanting to basically hide it until the user clicks that check mark. Unless this cannot be done. I don't want to have the user click a submit button just to move to the next step. – Ralph Sep 11 '15 at 16:15
  • Yes, so JS will be needed, not PHP. JS runs in the browser, PHP on the server. Start here, https://api.jquery.com/category/events/form-events/. – chris85 Sep 11 '15 at 16:17
  • Yes, I know that, I just though with the isset statement I could check this without submission. I was mistaken. Thanks for clarifying.....Does anyone know how I could do this with JS? – Ralph Sep 11 '15 at 16:18
  • @chris85 I updated my question. I am trying this with jquery and it hides the div, but doesn't display it when clicked. Also side note, for some reason whenever I load my page, half of the inputs are already checked? This was happening before as well. – Ralph Sep 11 '15 at 16:27
  • Sorry, didn't mean to do the chat. Thanks that helped. I can't believe I didn't catch that. Would you know why half of my checkboxex are loading checked? – Ralph Sep 11 '15 at 16:32
  • Answer posted, if questions post. – chris85 Sep 11 '15 at 16:35

3 Answers3

2

isset is a language construct and can't accept anything other than a variable as indicated by this warning on the linked to manual page:

Warning isset() only works with variables as passing anything else will result in a parse error.

You are not passing in a variable to the isset function, you are passing in a constant value, basically an array with a single string all_users_check. This is not a variable because you are not assigning it to a variable name. Try this instead:

if(isset($_POST['all_users_check']))

Here the variable being passed in is the superglobal $_POST, and you are checking to see if the index all_users_check is set inside of that array.

Update

To check if an input is empty or not via javascript, take a look at this question.

Community
  • 1
  • 1
Jeff Lambert
  • 24,395
  • 4
  • 69
  • 96
2

Issues in your code.

  1. .all_users_check that is looking for a class. Your element doesn't have a class so this isn't found. You can use a different selector to use the name attribute, https://api.jquery.com/attribute-equals-selector/.
  2. This $(".user_dropdown").hide(); hides your whole form. You might want to move around your divs, or remove that altogether.
  3. The checked attribute checks the field it is on. https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input

Use the checked attribute to indicate whether this item is selected

<div class="user_dropdown">
<form>
  <input type="checkbox" name="spectator_check" value=""> Spectators<br>
  <input type="checkbox" name="member_check" value=""> Team Members<br>
  <input type="checkbox" name="commissioner_check" value=""> Commissioner(s)<br>
   <label for="all_users_check">
  <input type="checkbox" name="all_users_check" value=""> Individual User<br>
  </label>
</form>
</div>

<script>
//$(".user_dropdown").hide();
$("input[name='all_users_check']").click(function() {
    if($(this).is(":checked")) {
        $(".user_dropdown").show();
    } else {
        $(".user_dropdown").hide();
    }
});
</script>
chris85
  • 23,846
  • 7
  • 34
  • 51
  • I was literally just about to click submit with the exact same solution when the notification appeared! – Andrew Willis Sep 11 '15 at 16:41
  • @chris85 The show hide is doing what I want. My only concern now is that when I load the page, some of the check box fields are already checked. I want it to load where nothing is checked. I put a pic in my question to illustrate. – Ralph Sep 11 '15 at 17:58
  • Did you try the code I posted, or update your code to remove the `checked` attribute? – chris85 Sep 11 '15 at 17:59
  • The checkbox issue with the inputs already being checked was happening before I added the jquery. I want that div to stay hidden until I click the checkbox, so how I had it works better. – Ralph Sep 11 '15 at 18:14
  • Yea, my comment and answer aren't referring to jquery with the `checked` attribute. Look at my HTML and yours ` Commissioner(s)
    ` <-- see it `value="" **checked**`.
    – chris85 Sep 11 '15 at 18:22
  • Oh, duh. Yep, that did it. Not sure how those got in there to be honest. Thanks for the help!! – Ralph Sep 11 '15 at 18:27
0

Try using this script, you have set the state of check boxes as checked by default.

<div class="user_dropdown">
<form action="">
  <input type="checkbox" name="spectator_check" value=""> Spectators<br>
  <input type="checkbox" name="member_check" value=""> Team Members<br><!-- removed 'checked' from this line -->
  <input type="checkbox" name="commissioner_check" value=""> Commissioner(s)<br><!-- removed 'checked' from this line -->
   <label for="all_users_check">
  <input type="checkbox" name="all_users_check" value="" > Individual User<br> <!-- removed 'checked' from this line -->
  </label>
</form>
</div>

<script>
$(".user_dropdown").hide();
$(".all_users_check").click(function() {
    if($(this).is(":checked")) {
        $(".user_dropdown").show();
    } else {
        $(".user_dropdown").hide();
    }
});
</script>

For the other issue of showing the hidden section again, try whether class all_users_check is visible to click.

jqheart
  • 767
  • 1
  • 5
  • 15
  • This doesn't throw any errors and hides what is in the isset, but if I check the `all_users_check` it doesn't do anything. – Ralph Sep 11 '15 at 16:13
  • @Ralph, if you have clicked on all_user_check and did not do any form submission, then you cannot find any difference. I think you are expecting a client side solution based on the checked state of the checkbox. – jqheart Sep 11 '15 at 16:16
  • @Ralph, check the solution above – jqheart Sep 11 '15 at 16:33
  • There is no `all_users_check` class. – chris85 Sep 11 '15 at 16:40