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?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.