Creating a Drop Down Menu in PHP from MySQL?

后端 未结 2 2028
面向向阳花
面向向阳花 2021-01-23 21:30

I\'m sort of experienced with PHP and MySQL so I kind of get the hang of things, however I\'m sort of trying to get something that may be over my level (not really sure the diff

相关标签:
2条回答
  • 2021-01-23 21:53

    Create a div and give it a id="result" and put an empty select tag in that div.

    Then using jQuery + Ajax, get the second dropdown using:

    $('select[name="user"]').change(function(){
       var user = $(this).val();
    
       $.ajax({
          type:'post',
          url:'getSecondDropDown.php',
          data:'user='+user,
          success:function(result){
             $('div#result').html(result);
          }
       });
    
    });
    

    In your 'getSecondDropDown.php' file, include this:

    <select name="task">
    <?php
    $user = $_POST['user'];
    $q = mysql_query("SELECT task FROM tablename WHERE user=".$user.") or die();
    while($r = mysql_fetch_array($q))
    { ?>
      <option value="<?php echo $r['task]; ?>"><?php echo $r['task]; ?></option>
    
    <?php
    }
    ?>
    </select>
    
    0 讨论(0)
  • 2021-01-23 22:00

    You'll need to use JavaScript to do it. Send an AJAX request to a page in the server which will fetch the details of a user for you, then return the results to JavaScript.

    Once you have the data in your JavaScript, use DOM manipulation to populate the second <select> with the results.

    0 讨论(0)
提交回复
热议问题