Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Feb 2012
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Problem with interdependent form

    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:
    Code:
    <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 Code:
    <?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.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,919
    Thanks
    79
    Thanked 4,423 Times in 4,388 Posts
    Ready to kick yourself?
    Code:
    <select id='sel20' name'sel20'>
    You are missing the = sign in there!!!

    Code:
    <select id='sel20' name='sel20'>
    With = sign missing, the value of that <select> is *NOT* sent to PHP. So of course you nave get ANY value at all for $subject_id = $_POST['sel20'];

    Not sure that's your only problem, but it's a start.

    Time to learn to DEBUG DEBUG DEBUG!!! If you can't debug, you can't program. Sorry, but that is a truism.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    New Coder
    Join Date
    Feb 2012
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    Ready to kick yourself?
    Code:
    <select id='sel20' name'sel20'>
    You are missing the = sign in there!!!

    Code:
    <select id='sel20' name='sel20'>
    With = sign missing, the value of that <select> is *NOT* sent to PHP. So of course you nave get ANY value at all for $subject_id = $_POST['sel20'];

    Not sure that's your only problem, but it's a start.

    Time to learn to DEBUG DEBUG DEBUG!!! If you can't debug, you can't program. Sorry, but that is a truism.
    Thanks a lot sir, you sure are sharp!

    I'll get right to the debugging chapter in my big PHP book.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •