How to add dynamic SUB menu

hi programmers how u doing? i need help creating a sub menu but with a twist

MENU
Submenu- team1 on click will display dropdown list of team2 lists
team2 on click will display dropdown list of team1 lists

if a user select (team1) it should drop down list of [team2] and if the user select (team2) it should drop list of [team1].

the code below is only a menu without submenu need ur assistance, thanks alot

// team select

		$query = "SELECT a.id, a.team1, a.team2, b.auction, b.bidder, b.tagged, b.willwin FROM " . $DBPrefix . "auctions a
		LEFT JOIN " . $DBPrefix . "bids b ON (b.auction = a.id)
		WHERE a.id = :auc_id and a.id = a.id group by a.id";
		$params = array();
		$params[] = array(':auc_id', $id, 'int');
		$db->query($query, $params);

		$TPL_team_list = '<select name="willwin" class="form-control">' . "\n";
		while ($row = $db->fetch())
		{
			$TPL_team_list .= "\t" . '
			<option value="' . $row[''] . '" ' . $selected . '>' . $row[''] . '</option>
			<option value="' . $row['team1'] . '" ' . $selected . '>' . $row['team1'] . '</option>
			<option value="' . $row['team2'] . '" ' . $selected . '>' . $row['team2'] . '</option>
			' . "\n";
		}
		
		$TPL_team_list .= '</select>' . "\n";

{
	$template->assign_block_vars('tag_bidder', array(
	'TEAM' => $TPL_team_list,

			));
	$i++;
}

Tables : auctions

columns: id(int), team1, team2
3 chelsea ajax

Tables : bids
columns: auction(int) bidder willwin
3 9 chelsea
3 5 ajax
3 7 chelsea
3 2 ajax
3 6 chelsea
3 4 chelsea

am trying to make a dropdown that depends on other drop down

Surely it’s more a JavaScript / Ajax issue rather than specifically PHP? When your user selects one of the drop-downs, react to the event, call your PHP script to recover the contents of the other drop-down, then use JS to populate the menu with the data?

hi droopsnoot thanks for ur reply, have been over the code again n again cant even seem to get it to work, please help me out with the code to complete it, thanks

Hi, This will work!

Hope this solution will resolve above issue which is posted on website.

$query = "SELECT a.id, a.team1, a.team2, b.auction, b.bidder, b.tagged, b.willwin FROM " . $DBPrefix . "auctions a
		LEFT JOIN " . $DBPrefix . "bids b ON (b.auction = a.id)
		WHERE a.id = :auc_id and a.id = a.id group by a.id";
		$params = array();
		$params[] = array(':auc_id', $id, 'int');
		$db->query($query, $params);

		$TPL_team_list = '<select name="willwin" class="form-control">' . "\n";
		while ($row = $db->fetch())
		{
			$TPL_team_list .= "\t" . '
			<option value="' . $row[''] . '" ' . $selected . '>' . $row[''] . '</option>
			<option class='team1' value="' . $row['team1'] . '" ' . $selected . '>' . $row['team1'] . '</option>
			<option class='team2' value="' . $row['team2'] . '" ' . $selected . '>' . $row['team2'] . '</option>
			' . "\n";
		}
		
		$TPL_team_list .= '</select>' . "\n";

{
	$template->assign_block_vars('tag_bidder', array(
	'TEAM' => $TPL_team_list,

			));
	$i++;
}


// One of the solution is:
// using ajax
<script type="text/javascript">

	$(".team1").on('change',function () {
		$.ajax({
			url:'get-team?param='+'2',
			success(data){
				$('#team').html(data);
			}
		})
	})

	$(".team2").on('change',function () {
		$.ajax({
			url:'get-team?param='+'1',
			success(data){
				$('#team').html(data);
			}
		})
	})
</script>

// server side - get-team

$suffix = $_GET['param'];
$query = "SELECT a.id, a.team".$suffix." as team_data, b.auction, b.bidder, b.tagged, b.willwin FROM " . $DBPrefix . "auctions a
LEFT JOIN " . $DBPrefix . "bids b ON (b.auction = a.id)
WHERE a.id = :auc_id and a.id = a.id group by a.id";
$params = array();
$params[] = array(':auc_id', $id, 'int');
$db->query($query, $params);

$TPL_team_list = '<select name="willwin" class="form-control">' . "\n";
while ($row = $db->fetch())
{
	$TPL_team_list .= "\t" . '
	<option value="' . $row[''] . '" ' . $selected . '>' . $row[''] . '</option>
	<option value="' . $row['team_data'] . '" ' . $selected . '>' . $row['team_data'] . '</option>
	' . "\n";
}

$TPL_team_list .= '</select>' . "\n";

return $TPL_team_list;

hi thanks for assistance, but it shows error,class option class team1 class team2 ts spring, but when i remove it i get blank page, so i checked for error again n found the return $TPL_team_list isnt returning instead it shows blank page so i removed it, it displays the page, shows the menu but no sub menu.

still showing error

Post the code as you have it now, in case there are any typos. Also what is the exact error message, as it appears?

ok posting

error 1. i changed class=‘team1’ to class=“team1” and class=‘team2’ to class=“team2” to stop that from showing unexpected (T_STRING) for both ‘team1’ and ‘team2’

error 2. return $TPL_team_list; makes the page goes blank but if i remove it i get the page back it displays the menu but on click no submenu displays, thanks for taking alook at it

$query = "SELECT a.id, a.team1, a.team2, b.auction, b.bidder, b.tagged, b.willwin FROM " . $DBPrefix . "auctions a
		LEFT JOIN " . $DBPrefix . "bids b ON (b.auction = a.id)
		WHERE a.id = :auc_id and a.id = a.id group by a.id";
		$params = array();
		$params[] = array(':auc_id', $id, 'int');
		$db->query($query, $params);

		$TPL_team_list = '<select name="willwin" class="form-control">' . "\n";
		while ($row = $db->fetch())
		{
			$TPL_team_list .= "\t" . '
			<option value="' . $row[''] . '" ' . $selected . '>' . $row[''] . '</option>
			<option class='team1' value="' . $row['team1'] . '" ' . $selected . '>' . $row['team1'] . '</option>
			<option class='team2' value="' . $row['team2'] . '" ' . $selected . '>' . $row['team2'] . '</option>
			' . "\n";
		}
		
		$TPL_team_list .= '</select>' . "\n";

{
	$template->assign_block_vars('tag_bidder', array(
	'TEAM' => $TPL_team_list,

			));
	$i++;
}
}?>
<script type="text/javascript">

$(".team1").on('change',function () {
$.ajax({
url:'get-team?param='+'2',
success(data){
$('#team').html(data);
}
})
})

$(".team2").on('change',function () {
$.ajax({
url:'get-team?param='+'1',
success(data){
$('#team').html(data);
}
})
})
</script>
<?php
$query = $_GET['param'];
$query = "SELECT a.id, a.team1, a.team2, b.auction, b.bidder, b.tagged, b.willwin FROM " . $DBPrefix . "auctions a
		LEFT JOIN " . $DBPrefix . "bids b ON (b.auction = a.id)
		WHERE a.id = :auc_id and a.id = a.id group by a.id";
		$params = array();
		$params[] = array(':auc_id', $id, 'int');
		$db->query($query, $params);

		$TPL_team_list = '<select name="willwin" class="form-control">' . "\n";
		while ($row = $db->fetch())
		{
			$TPL_team_list .= "\t" . '
			<option class="team1" value="' . $row[''] . '" ' . $selected . '>' . $row[''] . '</option>
			<option class="team2" value="' . $row['team_data'] . '" ' . $selected . '>' . $row['team_data'] . '</option>
			' . "\n";
		}
		
		$TPL_team_list .= '</select>' . "\n";
return $TPL_team_list;

What happens if, instead of

return $TPL_team_list;

you have

echo $TPL_team_list;

hi, it shows the page but no submenu

In your get-team.php, I don’t see anywhere that you connect to the database.

ETA:

Or am I not understanding the layout of your code correctly? The way I read the post by @harshal2 above, the code that follows the line

// server side - get-team

should be in a separate PHP file with that name, and is called by the line(s)

url:'get-team?param='+'1',

Therefore surely it will need the database connection, as well as variables such as $DBPrefix and $id to be defined somewhere.

I’m not big on Ajax, so I might be reading this wrongly.

i dont see any get-team.php




 

MENU
Submenu- team1 on click will display dropdown list of team2 lists
team2 on click will display dropdown list of team1 lists

if a user select (team1) it should drop down list of [team2] and if the user select (team2) it should drop list of [team1].

the code below is only a menu without submenu need ur assistance, thanks alot

// team select

		$query = "SELECT a.id, a.team1, a.team2, b.auction, b.bidder, b.tagged, b.willwin FROM " . $DBPrefix . "auctions a
		LEFT JOIN " . $DBPrefix . "bids b ON (b.auction = a.id)
		WHERE a.id = :auc_id and a.id = a.id group by a.id";
		$params = array();
		$params[] = array(':auc_id', $id, 'int');
		$db->query($query, $params);

		$TPL_team_list = '<select name="willwin" class="form-control">' . "\n";
		while ($row = $db->fetch())
		{
			$TPL_team_list .= "\t" . '
			<option value="' . $row[''] . '" ' . $selected . '>' . $row[''] . '</option>
			<option value="' . $row['team1'] . '" ' . $selected . '>' . $row['team1'] . '</option>
			<option value="' . $row['team2'] . '" ' . $selected . '>' . $row['team2'] . '</option>
			' . "\n";
		}
		
		$TPL_team_list .= '</select>' . "\n";

{
	$template->assign_block_vars('tag_bidder', array(
	'TEAM' => $TPL_team_list,

			));
	$i++;
}
Tables : auctions

columns: id(int), team1, team2
3	chelsea ajax

Tables : bids	
columns: auction(int)	bidder willwin
3	9	chelsea
3	5	ajax
3	7	chelsea
3 2	ajax
3 6	chelsea
3 4	chelsea




created
2d
last reply
2m11
replies

skyhighweb
1d1 
am trying to make a dropdown that depends on other drop down




 





$query = "SELECT a.id, a.team1, a.team2, b.auction, b.bidder, b.tagged, b.willwin FROM " . $DBPrefix . "auctions a
		LEFT JOIN " . $DBPrefix . "bids b ON (b.auction = a.id)
		WHERE a.id = :auc_id and a.id = a.id group by a.id";
		$params = array();
		$params[] = array(':auc_id', $id, 'int');
		$db->query($query, $params);

		$TPL_team_list = '<select name="willwin" class="form-control">' . "\n";
		while ($row = $db->fetch())
		{
			$TPL_team_list .= "\t" . '
			<option value="' . $row[''] . '" ' . $selected . '>' . $row[''] . '</option>
			<option class='team1' value="' . $row['team1'] . '" ' . $selected . '>' . $row['team1'] . '</option>
			<option class='team2' value="' . $row['team2'] . '" ' . $selected . '>' . $row['team2'] . '</option>
			' . "\n";
		}
		
		$TPL_team_list .= '</select>' . "\n";

{
	$template->assign_block_vars('tag_bidder', array(
	'TEAM' => $TPL_team_list,

			));
	$i++;
}

that was my original script before the alteration

any hope

I just don’t see that it should all be in the same file. I might be wrong, hopefully someone else will comment.

okay will try n seperate them n see

yeah did seperation nothing changed

am trying something hopefully that works also have posted an issue about it