-2

I've two ul categories/sub categories related to each other but something wrong i dunno it,i've done it before with dropdown list but not working with ul something wrong in this script i guess

HTML Code

<div class="card-body">
      <label for="address" class="form-label">Categories</label>
       <ul class="nav nav-pills nav-primary mb-3" role="tablist" id="cat_id" style="gap:5px ">
       <?php
        while ($row = $category->fetch_assoc()) {
echo   "<li class='nav-item waves-effect waves-light' id='" . $row['id'] . "'>
 <span class='nav-link category' data-bs-toggle='tab' role='tab' aria-selected='true'>"
. $row["name_e"] . "</span></li>"}
?>
</ul>
<div class="col-lg-4 col-md-4">
<label for="sub_cat_id">Sub Categories</label>
<ul class="nav nav-pills nav-primary mb-3" role="tablist" id="sub_cat_id" style="gap:5px ">
<li> </li>
</ul>
 </div>
 </div>

Ajax Script

    <script>
        $(document).ready(function() {
            $('#cat_id').on('change', function() {
                var catID = $(this).val();
                if (catID) {
                    $.ajax({
                        type: 'POST',
                        url: 'ajaxData.php',
                        data: 'id=' + catID,
                        success: function(html) {
                            $('#sub_cat_id').html(html);
                        }
                    });
                } else {}
            });

        });
    </script>

ajax Data.php File

if(!empty($_POST["id"])){
    $query = "SELECT * FROM obs_sub_categories WHERE categories_id = ".$_POST['id']."  ";
    $result = $website->query($query);
    if($result->num_rows > 0){
    while ($row = $result->fetch_assoc()) {
        echo   "<li class='nav-item waves-effect waves-light' id='" . $row['id'] . "'>
                <span class='nav-link category' data-bs-toggle='tab' role='tab' aria-selected='true'>"
            . $row["name_e"] .
            "</span>
                         </li>";
        }
    }
}


Karim Ali
  • 21
  • 1
  • 1
    `var catID = $(this).val();` - a `ul` element doesn't have any "value" that you could read. – CBroe May 12 '22 at 12:46
  • 1
    And it has no reason to fire a `change` event either. – CBroe May 12 '22 at 12:47
  • What happens if the value of `$_POST['id']` is `0; DROP TABLE obs_sub_categories` – zanderwar May 12 '22 at 12:50
  • **Warning:** You are wide open to [SQL Injections](https://php.net/manual/en/security.database.sql-injection.php) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/32391315) – Dharman May 12 '22 at 12:50
  • I'll try it thank you, I've entered value="= $row["id"] ?>" to the ul and i've tried it into the li and its not working alseo @CBroe Any hint – Karim Ali May 12 '22 at 13:11
  • You can not just add arbitrary attributes to elements that do not _have_ these attributes in the HTML standard, and then expect them to work. Use a custom data attributes - https://developer.mozilla.org/en-US/docs/Learn/HTML/Howto/Use_data_attributes – CBroe May 12 '22 at 13:14
  • There's no use associating a ` – brombeer May 12 '22 at 13:19

0 Answers0