I have a 'categories' mysql table and a 'subcategories' table. I need to make a form so the site owner can remove subcategories as needed.
I was think of having a form with 2 dropdowns on it, the first to choose the category, the second to choose the subcategory. Then based on that selection another form is filled with the chosen subcategories?
I'm unsure how to go about this though..

At the minute I have a form that is populated with all the subcategories, but there is no reference to which category they belong to.

This is the file

                <form action='subcategoryremoveform.php' method='post' enctype='multipart/form-data' name='remove_subcategory_form' id='remove_subcategory_form'>
               <input type='submit' name='submit' value='Remove Subcategories' />
                     include '../inc/connect.php';
                     $data = mysql_query("SELECT * FROM subcategories")
                     or die(mysql_error());
                     while($info = mysql_fetch_array( $data )) { 
                        echo "<p>";
                        echo "<input type='checkbox' name='check[{$info['subcat_id']}]' />";
                        echo $info['subcategory'];
                        echo "</span>";
                        echo "</p>";
                      include '../inc/connect.php';
                         $chk = (array) $_POST['check'];
                         $p = implode(',',array_keys($chk)); 
                         if ($sql = mysql_query("DELETE FROM subcategories WHERE subcat_id IN ($p)")){
                            header( 'Location: subcategoryremoveform.php' );
                            echo 'No subcategories have been removed';

Can anyone offer advice on this?


Recommended Answers

All 4 Replies

Member Avatar

You need to be careful here. As mysql deosn't insist on constraints, you could have orphaned FK fields if you store subcat ids in other tables. ANyway

Personally, I'd just have the one dropdown (cat) and have a dynamic html table listing the various subcats (each with a delete button) which changes with dropdown change - ie. sends an ajax request for current subcat data.

The delete button should not just remove the subcat from the html table but also delete the subcat from the DB (another ajax call).

I'd use jquery for this - it'd reasonably straightforward.

Hint - you'd probably need to use '.on' syntax for delete button event listener, as the buttons are dynamically drawn and don't exist when the page is originally loaded.

Thanks Al.
I don't even know where to begin with what you said!

Is there any way I could have the dropdown of categories and just have subcategories with checkboxes below, that match the category chosen in the dropdown?

Member Avatar

Sorry. Got ahead of myself.

Is there any way I could have the dropdown of categories and just have subcategories with checkboxes below, that match the category chosen in the dropdown?

Of course, this is similar to what I was suggesting, but may be an improvement, as you could select many different subcats and delete tham all at once.

Again, you could decide to use javascript or just a relatively straight php solution.

The straight php solution (with a small amount of trivial js), is very easy to implement, BUT, you get a page refresh every time you change the dropdown. OR, you could have a dropdown with a 'change' submit button to manually refresh - without any js at all.

The submit button (or automated js-initiated refresh) sends the dropdown value to the server (a form handler file would be best - but you could send it to the same page - definitely easier).

The server then picks up dropdown value, runs an sql query, creates a html table enclosed in form tags, listing your checkboxes and a final submit button.

That submit button then sends the data so that the values of those checked checkboxes can be deleted from the db. Example:

echo "<input type='checkbox' name='delrecord[]' value='{$row['id']}' />"; 

You can then collect the records to delete with this sort of thing:

    $where = ' WHERE `id` = IN (' . implode(",", $_POST['delrecord']) . ')';

    //build your delete query with the $where var and run it

there was similar problem with yours. In ASP.NET there was called Auto PostBack. the counter part in PHP is called AJAX. Asynchronous Javascript And Xml. since there is the Javascript, it is coded in the html level. for tutorial on AJAX, look for w3schools.com I found it very basic but useful.

Be a part of the DaniWeb community

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