hostfreak Posted July 29, 2007 Share Posted July 29, 2007 Hello, I want to have a couple checkbox inputs that when 'onchage' add the value of the checkbox to an array, that will be assigned to a variable. Is this possible? If so, any suggestions on places to start would be appreciated, thanks. Quote Link to comment Share on other sites More sharing options...
Barand Posted July 29, 2007 Share Posted July 29, 2007 Where you say "will be assigned to a variable", is that a javascript variable or a PHP variable when the form is submitted? Quote Link to comment Share on other sites More sharing options...
hostfreak Posted July 29, 2007 Author Share Posted July 29, 2007 It will be a php variable. And I was hoping before the form was actually submitted. Quote Link to comment Share on other sites More sharing options...
Barand Posted July 29, 2007 Share Posted July 29, 2007 The checkboxes and onchange events are on the client PC. PHP runs on the server. PHP variables don't exist until the form is submitted by the client and the data processed on the server. Quote Link to comment Share on other sites More sharing options...
Barand Posted July 29, 2007 Share Posted July 29, 2007 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> Quote Link to comment Share on other sites More sharing options...
hostfreak Posted July 29, 2007 Author Share Posted July 29, 2007 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? Quote Link to comment Share on other sites More sharing options...
Barand Posted July 29, 2007 Share Posted July 29, 2007 If, in my above example, the c/boxes have years as values, then on submission you could <?php $yrs = join (',', $_GET['cbox']) $sql = "SELECT ..... FROM mytablename WHERE YEAR(datecolname) IN ($yrs)"; Quote Link to comment Share on other sites More sharing options...
hostfreak Posted July 29, 2007 Author Share Posted July 29, 2007 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. Quote Link to comment Share on other sites More sharing options...
Barand Posted July 29, 2007 Share Posted July 29, 2007 If you want to show all the year/months prior to submission then, since you need to query the database (as above), you are going to need AJAX to display the months available once the years are selected. Quote Link to comment Share on other sites More sharing options...
hostfreak Posted July 29, 2007 Author Share Posted July 29, 2007 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. Quote Link to comment Share on other sites More sharing options...
hostfreak Posted July 29, 2007 Author Share Posted July 29, 2007 Is it even possible to store them in the php array? Quote Link to comment Share on other sites More sharing options...
corbin Posted July 29, 2007 Share Posted July 29, 2007 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) Quote Link to comment Share on other sites More sharing options...
hostfreak Posted July 29, 2007 Author Share Posted July 29, 2007 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.