display subcategories after selecting a specific category

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
learner001
Forum Newbie
Posts: 3
Joined: Thu Jun 12, 2014 3:39 am

display subcategories after selecting a specific category

Post by learner001 »

I have a form that has two parameters amenities and attributes. The amenities parameter has a dropdown menu that whose value gets populated from a separate table. The second parameter is attribute whose value will be entered by the user and will get saved in a database. View of database will be:

Code: Select all

ID  Attribute  Amenities
1       A             Aa
2       B             Bb
Code for this part is

Code: Select all

<form class="form-horizontal" role="form" action="admin_insert_attribute.php" enctype="multipart/form-data" method="post">    
    <div class="form-group">
        <label class="col-lg-3 control-label">Amenities:</label>
			<div class="col-lg-8">    
				<?php
					$con=mysqli_connect("abc","abc","abc","abc");
					// Check connection
					if (mysqli_connect_errno()) 
					{
					echo "Failed to connect to MySQL: " . mysqli_connect_error();
					}

					$result = mysqli_query($con,"SELECT amenities FROM amenities");
					echo "<select name='amenities'>";
					while($row = mysqli_fetch_array($result)) 
					{
					echo "<option value='" . $row['amenities'] . "'>" . $row['amenities'] . "</option>";
					}   
					echo "</select>";   
					mysqli_close($con);
				?>  
			</div>
    </div>    
    <div class="form-group">
        <label class="col-lg-3 control-label">Attribute:</label>
			<div class="col-lg-8">
				<input class="form-control" name="attribute" value="" type="" required>
			</div>
    </div>

    <div class="form-group">
        <label class="col-lg-3 control-label"></label>
			<div class="submit">
				<input class="btn btn-primary" value="Save " type="submit" name="submit">    
			</div>  
	</div>
</form>
Now I wish that when an amenity is selected from the dropdown menu all the attribute under that specific amenity should also get displayed in a tabular form on the same page. Code for entering and displaying the attribute is done on the same page.

Problem is that i am able to add the attributes but they are not getting displayed on the screen. Code that i used for displaying is

Code: Select all

<?php
	$con=mysqli_connect("abc","abc","abc","abc");
	// Check connection
	if (mysqli_connect_errno()) 
		{
			echo "Failed to connect to MySQL: " . mysqli_connect_error();
		}
	$result = mysqli_query($con,"SELECT * FROM attributes  WHERE amenities = '".$amenities."'");
	echo "<table class='table table-striped table-bordered table-hover'>
		<thead>
			<tr>
				<th>ID</th>
				<th>Attribute</th>
			</tr>
		</thead>";

		while($row = mysqli_fetch_array($result)) 
			{
				echo "<tbody data-link='row' class='rowlink'>";
				echo "<tr>";
				echo "<td>" . $row['id'] . "</td>";
				echo "<td>" . $row['attribute'] . "</td>";      
				echo "</tr>";
				echo "</tbody>";    
			}
	echo "</table>";
	mysqli_close($con);
?>
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: display subcategories after selecting a specific categor

Post by Celauran »

How are you calling this? AJAX call from change event listener? What if you take AJAX out of the equation? Does the script itself work? Produce the correct results?
phpdeveloper1
Forum Newbie
Posts: 19
Joined: Tue Aug 12, 2014 6:13 am
Location: Chennai, India

Re: display subcategories after selecting a specific categor

Post by phpdeveloper1 »

learner001 wrote:$result = mysqli_query($con,"SELECT * FROM attributes WHERE amenities = '".$amenities."'");
Where is $amenities being assigned ?
Chris, Php Developer and Programmer,
https://www.phpfreelanceprogrammer.com/
Post Reply