Jump to content

Getting the "reg" value from selected "type" aircraft dropdown [Simbrief]


ARV187
 Share

Recommended Posts

Hi there,

 

Im trying autoselecting the registration code from DB after select aircraft in a dropdown menu. But I don't know why not work. Simbrief plan set an auto registration to selected airplane.

At the moment I coded this:

Input name values that simbrief form get are: "type" to aircraft, "reg" to registration.

 

		   <?php 
		   
		   require_once ("/Connections/db.php");
		   mysqli_select_db($db, $database_db);
		   $resultSet = "SELECT icao, registration FROM phpvms_aircraft";		   
		   ?>

			<select name="type" id="typo" onChange="upicao()">
			<?php 
			$resultSet = mysqli_query($db, $resultSet) or die(mysqli_error($db));
			while($rows = $resultSet->fetch_assoc())			
			{
				$icao = $rows['icao'];
                $reg = $rows['registration']; 
				echo "<option value=$icao>$icao $reg</option>";				
			}					
			?>
			</select>
			<?php
			$avion = "<input type=hidden id=typo readonly>";
			?>
				<script type="text/javascript">
		function upicao() {
				var select = document.getElementById('typo');				
				var option = select.options[select.selectedIndex];

				document.getElementById('typo').value = option.value;
				
			}
                  upicao();
		</script>
		
		<?php
		
		if (($avion)!=null) {
		require_once ("/Connections/db.php");
		   mysqli_select_db($db, $database_db);
		   $resultSet2 = "SELECT registration FROM phpvms_aircraft WHERE icao='" . $avion . "'";
		?>
		  
		<b>Registro</b> (opcional):&nbsp;
		<input name="reg" id="regist">
		<?php
		$resultSet2 = mysqli_query($db, $resultSet2) or die(mysqli_error($db));
		while($rows = $resultSet2->fetch_assoc())
			{
			$reg = $rows['registration'];
			echo "<option value=$reg>$reg</option>";
		}			
		}
		?>


Could someone help me with the code, it's been two weeks ...

Thank you!

Edited by ARV187
Link to comment
Share on other sites

11 minutes ago, ProAvia said:

phpVMS version?

PHP version?

MySQL/MariaDB version?

 

What modules are you using?

Are you using the FltBook module?

I'm so sorry, I completely forgot about that.


phpVMS version? Version 2.1.936

PHP version? 5.6

MySQL/MariaDB version? 5.7.35

 

What modules are you using? None.

Are you using the FltBook module? No.

All work well, only I want add that auto reg selection in the input
<input type="hidden" name="reg" value="<?php echo $schedule->registration?>">
For reference: https://github.com/vangelisb/Simbrief/tree/5022f0bcfa886767f197ae890bffcc29fb98ba6a/phpvms

Edited by ARV187
Link to comment
Share on other sites

  • Administrators

The issue could be that phpVMS 2.1.936 doesn't work well with PHP 5.6

Also, phpVMS 2.x and 5.5.x don't work well with MySQL above 5.7.4

 

Chances are you will find more issues, especially when attempting to import airports, schedules and aircraft.

 

2.1.936 is very old and no longer actively supported.

Link to comment
Share on other sites

Does the above code works ? I mean did you somehow tried to dump the form values at some point to see if it is working properly (assigning values to names etc).

 

echo '<option value="'.$reg.'">'.$reg.'</option>';

 

I think the html form tags should be like <option value="E-ABCD">E-ABCD</option>, I did not tried to use them like you did with <option value=E-ABCD>E-ABCD</option>

 

Same applies to your ICAO Type selection code too (if this is the case of course)

Edited by DisposableHero
Link to comment
Share on other sites

Something like this (a little bit cleaner of yours and tried to correct the form elements

 

<?php
  require_once ("/Connections/db.php");
  mysqli_select_db($db, $database_db);
  $resultSet = "SELECT icao, registration FROM phpvms_aircraft";
?>

<select name="type" id="typo" onChange="upicao()">
  <?php
    $resultSet = mysqli_query($db, $resultSet) or die(mysqli_error($db));
    while ($rows = $resultSet->fetch_assoc()) {
      $icao = $rows['icao'];
      $reg = $rows['registration'];
      echo '<option value="'.$icao.'">'.$icao.' '.$reg.'</option>';
    }
  ?>
</select>

<?php $avion = '<input type="hidden" id="typo" readonly>'; ?>

<script type="text/javascript">
  function upicao() {
    var select = document.getElementById('typo');
    var option = select.options[select.selectedIndex];
    document.getElementById('typo').value = option.value;
  }
</script>

<?php
  if (isset($avion)) {
    require_once ("/Connections/db.php");
    mysqli_select_db($db, $database_db);
    $resultSet2 = "SELECT registration FROM phpvms_aircraft WHERE icao='".$avion."'";
?>
  <b>Registro</b> (opcional):&nbsp;
  <input name="reg" id="regist">
<?php
    $resultSet2 = mysqli_query($db, $resultSet2) or die(mysqli_error($db));
    while ($rows = $resultSet2->fetch_assoc()) {
      $reg = $rows['registration'];
      echo '<option value="'.$reg.'">'.$reg.'</option>';
    }
  }
?>

 

Edited by DisposableHero
Link to comment
Share on other sites

Thank you @DisposableHero
I tried your modifications, but same result that mine.

If I edit:

$resultSet2 = "SELECT registration FROM phpvms_aircraft WHERE icao='".$avion."'";

To:

$resultSet2 = "SELECT registration FROM phpvms_aircraft WHERE icao='A20N'";

 

The line:

echo '<option value="'.$reg.'">'.$reg.'</option>';

Show: RG-101 (the right reg to A20N), but when I send the form (<form id="sbapiform"> in \lib\skins\bs4\schedule_briefing.tpl) the result from simbrief (SimBrief?ofp_id=fpNumber) is an auto reg because when you don't pass a "reg" simbrief autogenerate one.

 

However if I write in input type text:

<input name="reg" id="regist">

the result from simbrief is the correct value written in the input text field.

GqytYg5.png

So I see two issues:
1º Form isn't sending the value from:

echo '<option value="'.$reg.'">'.$reg.'</option>';

but this value is showed rightly in form page (<form id="sbapiform"> in \lib\skins\bs4\schedule_briefing.tpl).

2º I'm not sure if WHERE variable is working because when I use icao='".$avion."' not is showing nothing in the web page echo '<option value="'.$reg.'">'.$reg.'</option>'; However if I use WHERE icao='A20N' is showed in form web page.

$resultSet2 = "SELECT registration FROM phpvms_aircraft WHERE icao='".$avion."'";

 

The full original code is here Line 123 and below: https://github.com/vangelisb/Simbrief/tree/5022f0bcfa886767f197ae890bffcc29fb98ba6a/phpvms/copy included file to your template folder

L131 <?php $planedata=(OperationsData::getAircraftByReg($schedule->registration)) ;?>
         <td><input type="hidden" name="type" size="5" type="text" placeholder="ZZZZ" value="<?php echo $planedata->icao ;?>"></td>

L186 <input type="hidden" name="reg" value="<?php echo $schedule->registration?>">
 

Edited by ARV187
Link to comment
Share on other sites

6 hours ago, ARV187 said:

So I see two issues:
1º Form isn't sending the value from:

 



echo '<option value="'.$reg.'">'.$reg.'</option>';

 

but this value is showed rightly in form page (<form id="sbapiform"> in \lib\skins\bs4\schedule_briefing.tpl).

2º I'm not sure if WHERE variable is working because when I use icao='".$avion."' not is showing nothing in the web page echo '<option value="'.$reg.'">'.$reg.'</option>'; However if I use WHERE icao='A20N' is showed in form web page.

 



$resultSet2 = "SELECT registration FROM phpvms_aircraft WHERE icao='".$avion."'";

 

 

This is happening because you are mixing up two different languages here and expecting them to work together always.

 

PHP is a server side language, means that anything you write with it runs *BEFORE* you see the actual result/page. While on the other hand, JavaScript runs at client side, meaning that you will see it working *AFTER* the page is rendered/loaded.

 

So what is happening in your code;

 

1. When you want to use that page, php code runs *before* you see it and generates the $resultset. At this moment, also the second php code block runs for $resultset2 but returns nothing because $avion has no value yet, simply it is null.

2. Page loads, you see the dropdown menu filled with $resultset data. You select one of them, and at that moment your JavaScript upIcao() start working and assigns a value to typo (which is also the $avion)

 

Here at this exact moment, you expect for php code to pick/see that change and run again. But this can not happen, it is against the design logic of it. So for php $avion is still empty and will remain empty, no matter how many times you change your selection it will have no effect for php 'cause it worked and stopped before you even see the that dropdown items.

 

What you can do;

 

1. Alter your code to select all aircraft registrations you have with php, make it ready to be used when the page loads. Then filter those results with your Javascript upIcao() and show a filtered second dropdown according to the selection of first dropdown.

 

2. Or you can make use of JavaScript/AJAX calls, which basically enables the use of php along with JavaScript (simple terms: it will run a php/mysql code when you change the first dropdown and then display you the results). You can find some nice examples for that and implement your own code.

 

Hope this helps ;)

Edited by DisposableHero
Link to comment
Share on other sites

In basic terms, for php this is happening ;

 

$resultSet = "SELECT icao, registration FROM phpvms_aircraft"; > Returns a nice result :)

 

$resultSet2 = "SELECT registration FROM phpvms_aircraft WHERE icao='<input type="hidden" id="typo" readonly>'"; > Returns nothing :(

 

Because $avion = '<input type="hidden" id="typo" readonly>'

 

Edited by DisposableHero
Link to comment
Share on other sites

Not much :) The basics of server side / client side limitations still apply but we are much flexible there at server side (with php + laravel)

 

I do not know if it is possible with php 5.x series but you may consider using JSON objects in that page too. Build the resultset once with php, convert it to a json object, then use it with javascript so show a dropdown for ICAO Types and then another one for Registrations ;)

Link to comment
Share on other sites

Hi, I'm trying the solution via json aproach, but I get this error:
 

SyntaxError: JSON.parse: expected property name or '}' at line 1 column 1 of the JSON data

 

My code:
 

<?php 
$resultSet = "SELECT icao, registration FROM phpvms_aircraft";	
$resultSet = mysqli_query($xxxx, $resultSet) or die(mysqli_error($xxxx));
while($rows = $resultSet->fetch_assoc())			
{
	echo $icao = $rows['icao'] . ' ';
    echo $reg = $rows['registration'] . '<br>';
	$dropdown = array('icao','registration');
}

//Print out the array in a JSON format.
header('Content-Type: application/json');
echo json_encode($dropdown);//Print out the array in a JSON format.
?>


I'm using this example: https://thisinterestsme.com/select-options-with-ajax/

 

Do you see what I am doing wrong at first glance?

P.S: Fixed, I still don't know what was wrong.
I'm now in dropdown step (select aircraft, extract icao & registration in separate inputs).

Edited by ARV187
Link to comment
Share on other sites

I think you can filter the json in your second dropdown, so a second call to database would not be needed / required at all. Once you create the json object from mysql results, what you need to to is ;

 

1. Show only the ICAO values of that json object in your first dropdown (group them by icao, so there will be only one B738)

 

2. And when user selects an ICAO type, use onChange() with JavaScript to show/display your second dropdown while passing the user's ICAO Type selection, here you need to show Registrations only which have the selected ICAO type by using the same json object.

 

At this point you will have two values in your hand; ICAO, from the first dropdown and REGISTRATION from second one. Then you can pass those values to your SimBrief form.

 

Hope you can manage to finish this and have a working solution.

 

Since you are not using live database calls, you may not need Ajax at all and all this can be done with plain JavaScript. (even you do not need JQuery to do all the above)

Edited by DisposableHero
Link to comment
Share on other sites

Posted (edited)

Thanks to @DisposableHero and @Nabeelfor the help

Code from DisposableHero:

 

<?php 
		   
   require_once ("xxx/db.php");
   mysqli_select_db($db, $database_db);
   $resultSet = "SELECT icao, registration FROM phpvms_aircraft WHERE `enabled`='1' ORDER BY `icao` ASC";		   
?>

<select id="dropdown" onChange="ChangeFormValues()">
<?php
	$resultSet = mysqli_query($db, $resultSet) or die(mysqli_error($db));
	while ($rows = $resultSet->fetch_assoc()) {
	  $icao = $rows['icao'];
	  $reg = $rows['registration'];
	  // Build your dropdown with php, just put a seperator between them like #
	  echo '<option value="'.$icao.'#'.$reg.'">'.$icao.' '.$reg.'</option>';
	}
?>
</select>

<input type="text" id="icao" name="type" maxlength="4" placeholder="ICAO Type" readonly>
<input type="text" id="reg" name="reg" maxlength="7" placeholder="Registration" readonly>


<script selectIcao="text/javascript">
function ChangeFormValues() {
	// Get the selection, and then split it
	var selected = document.getElementById('dropdown').value.split("#") ;
	// Now set your form field values according to selection
	document.getElementById('icao').value = selected[0] ;
	document.getElementById('reg').value = selected[1] ;
}
</script>

imagen.thumb.png.60df9be183bf38153a89eb502252e651.png

Edited by ARV187
Tip from DisposableHero "You can remove the name from the select 'cause you do not need to send it to SimBrief."
Link to comment
Share on other sites

Great to see it working :)

 

You can remove the name from the select 'cause you do not need to send it to SimBrief.

 

<select id="dropdown" onChange="ChangeFormValues()">

 

When you submit a form, everything you gave a name will be passed with its value (if it is enabled of course). We can not disable the dropdown here 'cause we need it, but we can avoid sending it to SimBrief by removing the name="" tag ;)

 

Just a little trick for you.

Edited by DisposableHero
  • Thanks 1
Link to comment
Share on other sites

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.

Loading...
 Share

×
×
  • Create New...