Jump to content

Recommended Posts

Firstly, hi, my first post in these forums.

 

Onto to my problem. I have a set of checkboxes and a button that selects them all. I am able to do this via Javascript.

 

Javascript:

function SelectAll2() {
boxnum = 0;
for (boxnum=0; boxnum <= 12; boxnum++){
  document.cform.criteria[boxnum].checked = true;
}
}

 

I also have a button that submits the values of all selected checkboxes to a php form. This I've also achieved.

 

Form with button:

<FORM action="http://localhost/mycode.php" name=optionsform METHOD="POST">
<input type="checkbox" name="criteria[]" value="check1">
<input type="checkbox" name="criteria[]" value="check2">

<INPUT TYPE=SUBMIT VALUE="SORT" >
</form>

 

 

 

The problem I am not able to do both of them with the same script. That is when selecting all boxes using javascript, I have to name the checkboxes as "criteria".

But for the php to work I have to name them as "criteria[]". I can change this to "critera[]" but then I have to use $_GET but I'd rather use $_POST.

 

So any suggestions on how I can work around this? Any help is appreciated, thanks.

Link to comment
https://forums.phpfreaks.com/topic/251563-checkbox-issue/
Share on other sites

The problem is that the form field is not called "criteria" anymore. You changed the name. HTML has no idea that the []s are special.

 

I've never tried it but I believe you can

document.cform["criteria[]"][boxnum].checked = true;

Link to comment
https://forums.phpfreaks.com/topic/251563-checkbox-issue/#findComment-1290123
Share on other sites

As you pointed out the name is changed (so that the php understands) and I realize. I'm wondering if there's a way that I can the javascript check the boxes while keeping the name as "criteria[]". So something like what you suggested, but unfortunately that doesn't work.

 

Or if I can keep the name as "criteria" but am able to post the values to the php.

 

I'm sorry if I'm not very clear but I kinda new to this stuff myself.

Link to comment
https://forums.phpfreaks.com/topic/251563-checkbox-issue/#findComment-1290190
Share on other sites

you can simply add an id to each checkbox and use document.getElementById

 

e.g.

<input type="checkbox" id="criteria_0" name.... />
<input type="checkbox" id="criteria_1" name.... />
...
for (boxnum=0; boxnum <= 12; boxnum++){
    document.getElementById('criteria_'+boxnum).checked = true;
}

Link to comment
https://forums.phpfreaks.com/topic/251563-checkbox-issue/#findComment-1290221
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.