Jump to content

Checkbox onchange to add value to array?


hostfreak

Recommended Posts

If you give the c/boxes a name ending with "[]" they are place in an array for you without the need for onchange events  eg

 

<?php
/**
* processing on server
*/
if (isset($_GET['sub']))
{
    $cbox = $_GET['cbox'] ;     // $cbox is now ab array of the CHECKED checkbox values
    
    // view the array
    echo '<pre>', print_r($cbox, true), '</pre>';
}

?>

<form>
<input type="checkbox" name="cbox[]" value="1">CB 1<br/>
<input type="checkbox" name="cbox[]" value="2">CB 2<br/>
<input type="checkbox" name="cbox[]" value="3">CB 3<br/>
<input type="checkbox" name="cbox[]" value="4">CB 4<br/>
<input type="checkbox" name="cbox[]" value="5">CB 5<br/>
<input type="submit" name="sub" value="Submit">
</form>

Link to comment
Share on other sites

Let me explain what exactly I am trying to do, sorry for not doing so before hand.

 

I have a page that allows users to "Browse Work Orders". The page first consists of a drop down of all Equipment Numbers that contain a work order.

 

Then, after selecting a equipment number, another drop down will reveal to allow the specific user to view: 1) All work orders for that equipment number or 2)(if available) My work orders for that equipment number.

 

After one is clicked, check boxes will appear (which is the step I'm trying to solve now), showing the years that equipment number has work order(s) in.

 

I want upon, what I assume to be correct, onchange of the check boxes to show available months for the year checked. I thought if I could somehow store the checked check boxes value in an array, then run a query be able to show the months based off the years in the array.

 

Not sure if that is possible?

Link to comment
Share on other sites

The year(s) they check will need to then show month checkboxes based of the month(s) that are available to the year(s) checked (all according to the work orders stored for the selected equipment number). Of course there will be duplicate months, but if stored in an array that can be taken care of easily. From there, they will then check the months, then submit. I am trying to get it to where they can narrow the search result down to specific results; equipment_number > category > Year(s) > Month(s). All before submitting and viewing the results.

Link to comment
Share on other sites

Yeah, that is exactly what I want to do. I don't even know where to start on the AJAX. Any suggestions?

 

I *think* I know the concept: When the Year check boxes "onchange" (are checked), I need to call the server and query all months for the years checked (AJAX). Then store them in a php array, from there I can use 'array_unique' to weed out duplicate months.

Link to comment
Share on other sites

What you're gonna wanna do is:

 

Client clicks a year, and an AJAX request is made to the server.

 

The server returns a list of months, and then JS formats them into the page.

 

I think you can see the basic concept in:

 

http://members.aol.com/barryaandrew/xmlhttp/article.html

 

(yeah, that's out of Barand's signature ;p, and is also written by him if I'm not mistaken)

Link to comment
Share on other sites

Oh, never noticed that in his signature. I am going to give it a read and see what I can gather. Hopefully I will be able to come up with what I need. I'm not going to lie, I am not very Javascript / AJAX savvy so I'll probably be back with some more questions. Thanks.

Link to comment
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.