Stromgren 1 Posted February 28, 2012 Report Share Posted February 28, 2012 Hello I needed an interdependent form for a website. Since i don't know any JavaScript this has been pretty tricky and i still have some problems. This is the code I'm using: JavaScript: <script type='text/javascript'>var ss2Values = [ // 'Please choose a subject' ['Please choose a category'], // '3D Printer' ['Darwin', 'Huxley', 'Mendel', 'Printrbot', 'Prusa Mendel'], // 'Home' ['Bathroom', 'Bedroom', 'Decorations', 'Furniture', 'Home utilities', 'Household items', 'Kitchen', 'Livingroom', 'Outdoor'], // 'Mechanical' ['Gears', 'Nuts and bolts', 'Tracks and bogies'], // 'Fun' ['Games', 'Ornaments', 'Toys'], // 'Electrical' ['Games', 'Ornaments', 'Toys'], // 'Educational' ['Biological models', 'Chemical models', 'Mathematical', 'Physical models']];window.onload = function(){ var ss2 = new xSubSelect('sel20', 'sel21', null, ss2Values, ss2OnChange);}function ss1OnChange(s0, s1, s2){ alert( s0.options[s0.selectedIndex].value + ' / ' + s1.options[s1.selectedIndex].value + ' / ' + s2.options[s2.selectedIndex].value );}function ss2OnChange(s0, s1){ alert( s0.options[s0.selectedIndex].value + ' / ' + s1.options[s1.selectedIndex].value );}function xSubSelect(sSelId0, sSelId1, sSelId2, aValues, fnOnChange){ var s0 = document.getElementById(sSelId0); var s1 = document.getElementById(sSelId1); var s2 = sSelId2 ? document.getElementById(sSelId2) : null; if (s0 && s1) { s0.onchange = function() { var i, len, val; // clear existing options for s1 len = s1.options.length; for (i = 0; i < len; ++i) { s1.options[0] = null; } // insert new options for s1 len = aValues[s0.selectedIndex].length; for (i = 0; i < len; ++i) { val = aValues[s0.selectedIndex][i]; s1.options[i] = new Option(s2 ? val[0] : val); } // update s2 if (s2) { s1.onchange(); } }; if (s2) { s1.onchange = function() { var i, len; // clear existing options for s2 len = s2.options.length; for (i = 0; i < len; ++i) { s2.options[0] = null; } // insert new options for s2 len = aValues[s0.selectedIndex][s1.selectedIndex].length; for (i = 1; i < len; ++i) { s2.options[i - 1] = new Option(aValues[s0.selectedIndex][s1.selectedIndex][i]); } }; s2.onchange = function() { if (fnOnChange) { fnOnChange(s0, s1, s2); } }; } else { s1.onchange = function() { if (fnOnChange) { fnOnChange(s0, s1); } }; } s0.onchange(); // first init }}</script> <?php if (isset($_POST['submit'])) { $subject_id = $_POST['sel20']; $category_id = $_POST['sel21']; $output_form = 'no'; } else { $output_form = 'yes'; } if ($output_form == 'yes') { ?> <table> <tr> <td> <form name='form2' method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <label for="sel20">Subject and category:</label> <br /> <select id='sel20' name'sel20'> <option>Please choose a subject</option> <option value="3D Printer">3D Printer</option> <option value="Home">Home</option> <option Value="Mechanical">Mechanical</option> <option value="Fun">Fun</option> <option value="Electrical">Electric</option> <option value="Educational">Educational</option> </select> <br /> <select id='sel21' name='sel21'> </select> <p class="signup_mgrey_10px"><a href="request_category">My category is not there?</a></p> <input type="submit" name="submit" value="Upload" /> </form> </td> <td> <img src="../images/upload_guide.png" alt="Upload your 3D model" /> </td> </tr> </table> <?php } if ($output_form == 'no') { //Write data to databse $query = "INSERT INTO models (subject_id, category_id) VALUES ('$subject_id', '$category_id')"; mysqli_query($connection, $query) or die ('Data not inserted.'); } ?> The JavaScript part is from an example including 2 interdependent forms. So there might be code for both forms, since i don't know which parts i can delete. When i write to my database it only writes the category_id. The subject_id won't get set. I tried echoing out $subject_id right before writing the $query without any luck. Can anyone spot the problem? Thanks in advance. Quote Link to post Share on other sites
justsomeguy 1,135 Posted February 28, 2012 Report Share Posted February 28, 2012 Check the select tag, you missed an equal sign after "name". Quote Link to post Share on other sites
Stromgren 1 Posted February 28, 2012 Author Report Share Posted February 28, 2012 Man you're sharp.. Thanks a lot! Quote Link to post Share on other sites
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.