How to write a database request with a choice through ajax to display information from tables associated with projects

<select name="category" id="category" class="form-control"> <option value="">Select Category</option> <?php $dbconfig = m ysqli_connect($dbhost,$dbuser,$dbpass,$dbname); $sql_query="SELECT * FROM category order by category_name asc" ; $result=mysqli_query($dbconfig,$sql_query); while($row = mysqli_fetch_array($result,MYSQLI_ASSOC)){ ?> <option value="<?php echo $row['category_id'];?>"> <?php echo $row[ 'category_name'];?> </option> <?php }?> </select> <select name="system" id="system" class="form-control"></select> <select name="projects" id="projects" class="form-control"></select> 

    1 answer 1

    Everything is simple, we create a button, hang a handler on it for a click -> then comes the request for ajaxtest.php and returns the necessary ajaxtest.php

     $('#superbutton').click(function() { $.get( "/ajaxtest.php", { category: $('#category option:selected').attr('value') }, onAjaxSuccess ); function onAjaxSuccess(data) { $('#projects').append(data); } }) 
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> <button id="superbutton">Поехали</button> 

    ajaxtest.php (correct as you need)

     <? $dbconfig = m ysqli_connect($dbhost,$dbuser,$dbpass,$dbname); $id = (int)$_GET['category']; $sql_query="SELECT * FROM projects WHERE `category`='$id'" ; $result=mysqli_query($dbconfig,$sql_query); while($row = mysqli_fetch_array($result,MYSQLI_ASSOC)){ ?> <option value="<?=$row['project_id'];?>"> <?=$row[ 'project_name'];?> </option> <?php }?>