I have a for with 2 dropdowns, 'category' and 'subcategory'
I'm trying to make my page that it shows records(products) in the table based on dropdown selection of the subcategoy dropdown.
So for example all records beloning to the subcat wheels have a subcat_id of 4. Sunbcategory table and products table both have a field subcat_id. This for the admin page so that i can remove, hide or show products.
Here my unworking code, the category and subcategory selects are filled fine............

                            <?php
                   include '../inc/categorydropdown.php';
                    ?>

            <p><label for="cat">Category</label>
                   <select name="cat" id="cat">
                      <?php echo $op;?>   
                   </select><br />
                   <label for="subcat">Subcategory</label>
                  <select name="subcat" id="subcat"> </select></p>
                  <?php
                     include '../inc/connect.php';
                     if(isset($_POST['subcat'])){
                     $subcat = intval($_POST['subcat']);
                     $q = "SELECT * FROM products WHERE subcat_id = $subcat";
                     $result = $link->query($q);
                     if($result){
                        while($row=mysqli_fetch_array($result)){
                           echo "<input type='checkbox' name='remove[{$row['id']}]'>",
                                "<label for='Remove'><span class='text'>Remove</span></label>";
                                if ($row['status'] ==  1){
                                   echo"<input type='checkbox' name='hide[{$row['id']}]'>",
                                   "<label for='hide'><span class='text'>Hide</span></label>";
                                }
                                if ($row['status'] == 2){
                                   echo"<input type='checkbox' name='show[{$row['id']}]'>",
                                   "<label for='show'><span class='text'>Show</span></label>";
                               }
                            echo "<br />",
                           "<img src='{$row['image']}' alt='{$row['name']}' />",
                           "<br />";
                        }
                    }
Member Avatar for diafol

Did you read my blog post (I sent the url to your last thread - but it was already solved)?

ANyway, you'll probably be looking to ajaxify this on category OR subcategory change.

Do you have the cat/subcat changes working yet? If so, collect the subcat change everytime (so if the cat changes - ensure that you get the default [or first] subcat value) and pass that to your server.

Get your products info from the DB table and place them into a variable which you can then echo to the appropriate place in your html.
Else if you're ajaxifying, use js to update your specific html tag.

All this mixing of html and inline procedural code is very messy and difficult to maintain.

Ill check it out now and see houw I get on ;)

I read your blog post, I couldnt really get my head to understand how to use it for what I'm doing.
I did manage to get my own code working though(eventually!)

My code now looks like this (whole form)

<form class='adminform1' action='productsremove.php' method='post' enctype='multipart/form-data' name='image_upload_form' id='image_upload_form'>
            <?php include '../inc/categorydropdown.php'; ?>
            <p><label for="cat">Category</label>
            <select name="cat" id="cat"> <?php echo $op;?> </select><br />
            <label for="subcat">Subcategory</label>
            <select name="subcat" id="subcat"> </select>
            <input type='submit' name='show' value='Show' /> </p>
            <?php
               include '../inc/connect.php';
                  if(isset($_POST['show'])){
                  $subcat = intval($_POST['subcat']);
                  $q = "SELECT * FROM products WHERE subcat = $subcat";
                  $result = $link->query($q);
                  if($result){
                      while($row=mysqli_fetch_array($result)){
                         echo "<input type='checkbox' name='remove[{$row['id']}]'>",
                              "<label for='Remove'><span class='text'>Remove</span></label>";
                              if ($row['status'] ==  1){
                                 echo"<input type='checkbox' name='hide[{$row['id']}]'>",
                                     "<label for='hide'><span class='text'>Hide</span></label>";
                              }
                              if ($row['status'] == 2){
                                 echo"<input type='checkbox' name='show[{$row['id']}]'>",
                                     "<label for='show'><span class='text'>Show</span></label>";
                              }
                          echo "<br />",
                               "<img src='{$row['image']}' alt='{$row['name']}' />",
                               "<br />";
                        }
                    }
                    else{
                       echo mysqli_error();
                    }
                    }
                   ?>
                   <input type='submit' name='submit' value='Submit' /> 
            </form>

I have another minor problem.
When the page load my dropdown boxe show 'category' -dogs 'subcategory'-collars

When i choose different cats and subcats it shows the products expected but the dropdown boxes revert back to their origional values, how can i make them not do this? So if im lookin and cat cats subcat bowls i want the values cats and bowls to stay in the dropdowns.................

Would that have something to do with categorydropdown.php

include 'connect.php';
   //populate form dropdown box
   $op = '';
   $r = "SELECT cat_id, category FROM categories ORDER BY cat_id";
   $result = $link->query($r);
   if (mysqli_num_rows($result)){
      while ($d = mysqli_fetch_assoc($result)){
         $op .= "\n\t<option value='{$d['cat_id']}'>{$d['category']}</option>";
      }
   }
Member Avatar for diafol

To keep the same values, you can use the POST values to make a 'selected' attribute in the dropdowns, e.g.

$cat = 0;
$subcat = 0;
if(isset($_POST['cat'])) $cat = (int) $_POST['cat'];
if(isset($_POST['subcat'])) $subcat = (int) $_POST['subcat'];

 while ($d = mysqli_fetch_assoc($result)){
     $sel = ($cat == $d['cat_id']) ? " selected='selected' " : '';
     $op .= "\n\t<option value='{$d['cat_id']}'$sel>{$d['category']}</option>";
  }

etc. That's assuming you don't have id = 0 in your tables :)
This way, when you load the page, you get the default values as the first options.

Thanks, this keeps my cat selction, but the subcat reverts back to default for that cat. I would need to do similar in my subcat.php file that fills the subcat dropdown?

Member Avatar for diafol

Thanks, this keeps my cat selction, but the subcat reverts back to default for that cat. I would need to do similar in my subcat.php file that fills the subcat dropdown?

What do you think? :rolleyes: :)

I cant get this working in the subcat file! Its not displayin any subcats now.

Heres the file

if(isset($_POST['cat'])){
    $cat = intval($_POST['cat']);
    $query = "SELECT subcat_id, subcategory FROM `subcategories` WHERE cat_id = $cat";
    $result = $link->query($query) or die('error');
    $subcatOps = '';
    if(mysqli_num_rows($result)){
      $subcat = 0; 
      if(isset($_POST['subcat'])) $subcat = (int) $_POST['subcat'];
      while ($d = mysqli_fetch_assoc($result)){
         $sel = ($subcat == $d['subcat_id']) ? " selected='selected' " : '';
         $subcatOps .= "\n\t<option value='{$d['subcat_id']}'$sel>{$d['subcategory']}</option>";
      }
    }
}

Any advice on this?
Thanks

Member Avatar for diafol

THrow some echoes into the routine to see if you're getting what you expect.

Ive tried that, but as this code is being sent to some ajax the echos dont seem to do anything!

Member Avatar for diafol

Ah, I didn't realise that it was an ajax call.
have you got a

$subcat=0

at the start of the script?

At the start of my ajax script?

Member Avatar for diafol

Darn sorry - didn;t see it - you've already got it on line 7.

You're not actually outputting anything with that code, do you have an echo later on?
Ajax won't update your subcat unless there's output of some description.

Yes, sorry I must have cut it of!
I echo $subcatOps

Member Avatar for diafol

OK can I suggest that instead of updating your select dropdown, that you place the output to your console, something like:

.success(function(data){
    console.log(data);
});

So you can see all the output. This means you can see all the echo statements or error statements from the php. Are you familiar with the console?

If not, then using Chrome - right click on the screen, 'Inspect Element' and choose 'Console'
This will give you a real time log of what js is outputting or whether there are js errors.

Im not sure if ive done this right.

 //change subcat dropdown
            function getSubCat(){
               cat = $('#cat').val();
               $.post("../inc/subcat.php", { 'cat': cat }).success(function(data){
    console.log(data);
}
                  $('#subcat').html(data);
               });
            }

Then in the console it says

Uncaught SyntaxError: Unexpected identifier productsremove.php:53
Uncaught TypeError: Cannot call method 'appendChild' of null
Uncaught TypeError: Cannot read property 'compareDocumentPosition' of null
Resource interpreted as Script but transferred with MIME type text/html: "http://telemetry.pricepeep.net/te.aspx?callback=jQuery18309547556999605149_…tnerId=pricepeep&action=about+to+call+server&sampleRate=10&_=1375870845146". jquery.min.js:2
Uncaught SyntaxError: Unexpected token <

Having taken this out

$('subcat').html(data)

I now get this in the console

<option value='9'>wheel</option>
    <option value='10'>cage</option>
    <option value='12'>bottle</option> productsremove.php:51
Uncaught TypeError: Cannot call method 'appendChild' of null
Uncaught TypeError: Cannot read property 'compareDocumentPosition' of null

The options are correct for the chosen category but i havent a clue what the errors mean/...........

Member Avatar for diafol

sorry on vacation with limited mobile access. wont be back for 3 weeks

no problem al.
have a good time.............

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.