Question about after loading the second listbox options

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
yclan
Forum Newbie
Posts: 3
Joined: Fri Jan 15, 2016 10:40 am

Question about after loading the second listbox options

Post by yclan »

Hi, I am new to PHP and am developing a site with two list boxes. Both list options are loaded from MySQL tables. I can get the second list loaded after an option is selected from the first list. However, when I try to use the selected option from the second list to retrieve the record from MySQL, the selected option doesn't seem to be captured, thus I have nothing to pass onto the database. Thanks a lot in advance!

Here's my codes.

Code: Select all

// Load Country list from the Database to the list
	$sql = "SELECT * FROM country ORDER BY name";
	$query = mysqli_query($dbCon, $sql);
	While ($row = mysqli_fetch_array($query)){
            $scountry=$row[1]. " - " . $row[0];
			$option_block .= '<option value="'.$scountry.'">'.$scountry.'</option>'; 
	}

// Check selected Country
	if (isset($_POST['aftercountrysummit']))
	{
		$vcountry = secure_input($dbCon, $_POST['scountry']); 
		if ($vcountry == " - - Please Select") 
		{
			echo "Please select from the Country list!"; 	
		} else {
			// Load the Institution List
			$sql = "SELECT * FROM partner WHERE Country = '$vcountry'";
			$query = mysqli_query($dbCon, $sql);
			$recordcount = mysqli_num_rows($query); 
			if ($recordcount>0)
			{
				echo "<br>Total number of institution partners in ".$vcountry." is  ".$recordcount. "<br>";			
				While ($row = mysqli_fetch_assoc($query))
				{
					$spartner=$row["Institution_Name"];
					$institution_block .= '<option value="'.$spartner.'">'.$spartner.'</option>';; 
				}
			}	else {
				echo "No institution partners in ". $vcountry; 
			}
		}
	}

// Check selected Institution
if (isset($_POST['searchrecord']))
	{
		$vinstitutionname = secure_input($dbCon, isset($_POST['spartner'])); 
		if ($vinstitutionname != "")
		{
			//Load the selected Institution details
			$sql = "SELECT * FROM partner WHERE Institution_Name = '$vinstitutionname'";
			$query = mysqli_query($dbCon, $sql);
			$recordcount = mysqli_num_rows($query); 
			echo "<br>Total number of records is ".$recordcount." Institution CODE is ". $row["Institution_Code"]; 

			if ($recordcount>0)
				{
					echo "Details: ".$row; 
				}
		} else {
			echo "<br> NO RECORDS!";	
		}
	}
// Load Institution list from the Database after the Country is selected
?>
</head>
<body>
<form name="searchcountry" id="searchcountry" method="post" action="<?PHP echo htmlspecialchars($_SERVER['PHP_SELF']); ?>">
	<label for="selectcountry">Country:</label>
	<select name="scountry" id="scountry"><?php echo "$option_block"; ?></select>
	<input type="submit" name="aftercountrysummit" id="aftercountrysubmit" value="Next">
</form>
<form name="searchinstitution" id="searchinstitution" method="post" action="<?PHP echo htmlspecialchars($_SERVER['PHP_SELF']); ?>">
	<label for="selectinstitution">Institution:</label>
	<select name="spartner" id="spartner" onChange="getDetails(this.value)" ><?php echo "$institution_block"; ?></select>
	<input type="submit" name="searchrecord" id="searchrecord" value="Search">
</form>
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Question about after loading the second listbox options

Post by Christopher »

yclan wrote:However, when I try to use the selected option from the second list to retrieve the record from MySQL, the selected option doesn't seem to be captured, thus I have nothing to pass onto the database. Thanks a lot in advance!
The first select looks like this:

Code: Select all

	<select name="scountry" id="scountry"><?php echo "$option_block"; ?></select>
But the second one has an onChange event handler:

Code: Select all

	<select name="spartner" id="spartner" onChange="getDetails(this.value)" ><?php echo "$institution_block"; ?></select>
I assume that you bind to the events elsewhere?
(#10850)
yclan
Forum Newbie
Posts: 3
Joined: Fri Jan 15, 2016 10:40 am

Re: Question about after loading the second listbox options

Post by yclan »

Christopher wrote:
yclan wrote:However, when I try to use the selected option from the second list to retrieve the record from MySQL, the selected option doesn't seem to be captured, thus I have nothing to pass onto the database. Thanks a lot in advance!
The first select looks like this:

Code: Select all

	<select name="scountry" id="scountry"><?php echo "$option_block"; ?></select>
But the second one has an onChange event handler:

Code: Select all

	<select name="spartner" id="spartner" onChange="getDetails(this.value)" ><?php echo "$institution_block"; ?></select>
I assume that you bind to the events elsewhere?
That's the issue, I am not sure what to put in "getDetails()" function in order to pass the variable back to HTML for further process.

I found that after I click on "Search" button on the second list form, it wiped out the option list and therefore nothing is selected in the second list. Something wrong in the second form submission.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Question about after loading the second listbox options

Post by Christopher »

Maybe just remove the onChange="getDetails()" and see if it works.
(#10850)
yclan
Forum Newbie
Posts: 3
Joined: Fri Jan 15, 2016 10:40 am

Re: Question about after loading the second listbox options

Post by yclan »

Christopher wrote:Maybe just remove the onChange="getDetails()" and see if it works.
I tried. The issue is there selected value in the second list doesn't get captured and submitted when the form is submitted.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Question about after loading the second listbox options

Post by Christopher »

Have you checked the value being returned?

Code: Select all

// Load Country list from the Database to the list
// Check selected Institution
if (isset($_POST['searchrecord']))
	{
		$vinstitutionname = secure_input($dbCon, isset($_POST['spartner'])); 
echo "_POST['spartner']=". $_POST['spartner'] . ", vinstitutionname=$vinstitutionname<br>"; 
(#10850)
Post Reply