Jump to content

Populate a form based on dropdown selections.


glen-rogers

Recommended Posts

Hi

 

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 minut 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' />
   <?php
   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>";
   }
   ?>
</form>
<?php
   include '../inc/connect.php';
   if(isset($_POST['check'])){
   $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' );
   }
   else{
      echo 'No subcategories have been removed';
   }
   }
                   ?>

Can anyone offer advice on this?

I'm unsure how to do joins! I'll give it a try!

see http://www.phpfreaks.com/tutorial/data-joins-unions

 

 

I was thinking maybe I could have the dropdown of categories and just have subcategories with checkboxes below, that match the category chosen in the dropdown?

Yes, you could

Thanks for the link, I read it and bookedmarked it!

 

I came up with this code:

<form action='subcategoryremoveform2.php' method='post' enctype='multipart/form-data' 
name='remove_subcategory_form' id='remove_subcategory_form'>
               
<?php
   include 'admininc/categorydropdown.php';
?>
<b>Category</b>
<select name="cat">
   <?php echo $op;?>
</select>
<input type='submit' name='submit' value='Get Subcategories' />
<?php
   include '../inc/connect.php';
   if(isset($_POST['submit'])){
      $cat = intval($_POST['cat']);
      $data = mysql_query("SELECT * FROM subcategories WHERE cat_id IN ($cat) ")
              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>";
   }
      }
?>
<p><input type='submit' name='remove' value='Remove Subcategories' /></p>
</form>

It does the job, but is a bit rough I think. Like the second submit button is there even if there are no subcategories! Is there a way to have the form so that button is not there without any subcategories?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.