0

I try to make a menu with dropdowns that are filled from php, but the 3rd dropdown does not work for me, here is the code and the alert, I will also pass some images of the table in php.

Fatal error: Uncaught TypeError: Cannot access offset of type string on string in C:\xampp\htdocs\asd\get_subcat2.php:14 Stack trace: #0 {main} thrown in C:\xampp\htdocs\asd\get_subcat2.php on line 14

##index.php##

<?php
include 'database.php';
$result = mysqli_query($conn,"SELECT * FROM empresas");
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <title>Category</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
    <form>
        <div class="form-group">
          <label for="sel1">Category</label>
          <select class="form-control" id="category">
          <option value="">Select Category</option>
            <?php
            while($row = mysqli_fetch_array($result)) {
            ?>
                <option value="<?php echo $row["Nombre_de_la_empresa"];?>"><?php echo $row["Nombre_de_la_empresa"];?></option>
            <?php
            }
            ?>
            
          </select>
        </div>


        
        <div class="form-group">
          <label for="sel2">Sub Category</label>
          <select class="form-control" id="sub_category">
          <option value="">Select Category</option>
           
            
          </select>

        </div>


        <div class="form-group">
          <label for="sel3">Sub Category2</label>
          <select class="form-control" id="sub_category2">
          <option value="">Select Category</option>
           
            
          </select>
<script>
$(document).ready(function() {
    $('#sub_category').on('change', function() {
            var category_id2 = this.value;
            $.ajax({
                url: "get_subcat2.php",
                type: "POST",
                data: {
                    category_id2: category_id2
                },
                cache: false,
                success: function(dataResult2){
                    $("#sub_category2").html(dataResult2);
                }
            });
        
        
    });
});
</script>



        </div>
    </form>
</div>
<script>
$(document).ready(function() {
    $('#category').on('change', function() {
            var category_id = this.value;
            $.ajax({
                url: "get_subcat.php",
                type: "POST",
                data: {
                    category_id: category_id
                },
                cache: false,
                success: function(dataResult){
                    $("#sub_category").html(dataResult);
                }
            });
        
        
    });
});
</script>



</body>
</html>

       
    
    
##  get_subcat.php ##

   
 

       <?php
            include 'database.php';
            $category_id=$_POST["category_id"];
            $result = mysqli_query($conn,"SELECT * FROM sucursales");
        ?>
        <option value="">Select SubCategory</option>
        <!-- <button onclick="cat()">ok </button>
        <script>
        function cat() {
        
        console.log($category_id)
        }
        </script> -->
        <?php
        while($row = mysqli_fetch_array($result)) {
        ?>
            <option value="<?php echo $row[$category_id];?>"><?php echo $row[$category_id];?></option>
        <?php
        }
        ?>     
    
    


    >

##Get_subcat2.php##
           <?php
        include 'database.php';
        $category_id2= NULL;
        $category_id2=$_POST["category_id2"];
        $category_id2 = strtolower($category_id2);
        $category_id2 = @ (ucwords ($category_id2));
        $result2 = mysqli_query($conn,"SELECT SUCURSALES FROM empleados");
        ?>
    <option value="">Select SubCategory</option>
    
    <?php
    while($row = mysqli_fetch_array($result2)) {
        ?>
            <option value="<?php echo $row[$category_id2];?>"><?php echo  $row[0]['NOMBRE COMPLETO'];?></option>
        <?php
    }
    ?> 
  • 1
    Please include the error message. Likely `NOMBRE COMPLETO` is undefined because you are not selecting that in your query. I also don't know how you know `$category_id2` relates to the row, I would select the id from the database, or use a `where` clause for an association. – user3783243 Apr 29 '22 at 10:47
  • 1
    `echo $row[$category_id2];` if you just want to echo the ID, use `echo $category_id2;`. If your rows have distinct IDs, select the ID column and use `echo $row['id'];`. Also, you have just repeated your code for `Select SubCategory` twice above. Also, your query `"SELECT SUCURSALES FROM empleados"` only ever selects one column called `SUCURSALES` (which is nowhere used). You may want to double-check that the code in your question reflects your actual situation. – Markus AO Apr 29 '22 at 11:01
  • Is`$row` multidimensional? Also don't use error suppression if getting an error address it. `ELECT SUCURSALES FROM empleados` is still your main issue. – user3783243 Apr 29 '22 at 11:04
  • i change it, like this: but it still dont work, dont know what to do – Abdiel Rivas Apr 29 '22 at 11:26

0 Answers0