A select-box limitation has stopped working as expected (follow up on earlier question https://www.experts-exchange.com/questions/29069379/Select-boxe-and-MySQL.html)

Hi,

I have this page: https://kroweb.dk/gfdev/arkivalier/ft_6/?sogn=&query_start=&mytype=&amt=

In this thread: https://www.experts-exchange.com/questions/29069379/Select-boxe-and-MySQL.html here on EE we created a php function which limited the years in the "Vælg et År" slelct-box according to the choices made in the select-boxes "Type" and "Amt".

When we closed the question it worked nicely, but for some reason it now only works on the "Type" select-box. The "Amt" select-box does not anymore affect the listed years in the "Vælg et år" select-box at all.

This is the code od the limitation file:
<?php

// Turn on error reporting
error_reporting(E_ALL);
ini_set('display_errors', 1);

require_once('ft_data_connection.php');
 

$mysqli->set_charset("utf8");


$amt = trim($_POST['amt']);
$type = trim($_POST['type']);
$aar = trim($_POST['aar']);
 
$amt = isset($_POST['amt']) ? $_POST['amt'] : false;
$type = isset($_POST['type']) ? $_POST['type'] : false;
$aar = isset($_POST['aar']) ? $_POST['aar'] : false;
 


// We add 1=1 so we can do the AND condition with new criteria without having
// to trim off the last one
$query = "SELECT DISTINCT Aar AS field FROM ft_children WHERE 1=1";
$filter = '';
if ($type) {
  $filter .= " AND `Type` = '{$type}'";
}
if ($amt) {
  $filter .= " AND `Amt` = '{$amt}'";
}

// Repeat for any other conditions you want to add
$query .= $filter;

// ADD THE ORDER BY HERE. YOU COULD DO IT AS PART
// OF THE PREVIOUS LINE I DO IT HERE SO THAT YOU
// HAVE THE OPTION OF TURNING IT ON OR OFF
$query .= " ORDER By Aar ASC";

$result = $mysqli->query($query);
if ($result) {
  echo "<option> </option>";
  while($row = $result->fetch_object()) {
    echo <<< OPTION
<option value="{$row->field}">{$row->field}</option>
OPTION;
  }
}
// WHILE DEBUGGING ADD THIS
// else {
//  echo "DB Error: {$mysqli->error}<br>QUERY: {$query}<br>";
// }

Open in new window

Peter KromanSales ExecutiveAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
Your form is issuing a GET but the script is looking for POST
In the GET you have
mytype=Landdistrikt&amt=Bornholm&Sog=Søg

Open in new window

But you are looking for type and amt respectively
0
Peter KromanSales ExecutiveAuthor Commented:
Yes. The function should work on one or both of the select boxes, depending on the users choice.

When changing the code like this, no difference can be seen. It is still Type which is working. Amt is not working and they are also not working when a choice is made in both boxes.

<?php

// Turn on error reporting
error_reporting(E_ALL);
ini_set('display_errors', 1);

require_once('ft_data_connection.php');
 

$mysqli->set_charset("utf8");


$amt = trim($_GET['amt']);
$type = trim($_GET['mytype']);
$aar = trim($_GET['aar']);
 
$amt = isset($_GET['amt']) ? $_GET['amt'] : false;
$type = isset($_GET['type']) ? $_GET['mytype'] : false;
$aar = isset($_GET['aar']) ? $_GET['aar'] : false;
 


// We add 1=1 so we can do the AND condition with new criteria without having
// to trim off the last one
$query = "SELECT DISTINCT Aar AS field FROM ft_children WHERE 1=1";
$filter = '';
if ($type) {
  $filter .= " AND `Type` = '{$mytype}'";
}
if ($amt) {
  $filter .= " AND `Amt` = '{$amt}'";
}

// Repeat for any other conditions you want to add
$query .= $filter;

// ADD THE ORDER BY HERE. YOU COULD DO IT AS PART
// OF THE PREVIOUS LINE I DO IT HERE SO THAT YOU
// HAVE THE OPTION OF TURNING IT ON OR OFF
$query .= " ORDER By Aar ASC";

$result = $mysqli->query($query);
if ($result) {
  echo "<option> </option>";
  while($row = $result->fetch_object()) {
    echo <<< OPTION
<option value="{$row->field}">{$row->field}</option>
OPTION;
  }
}
// WHILE DEBUGGING ADD THIS
// else {
//  echo "DB Error: {$mysqli->error}<br>QUERY: {$query}<br>";
// }

Open in new window

0
Julian HansenCommented:
Selecting Diverse and Bornholm seems to filter the results correctly
ss156.jpg
0
Amazon Web Services

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

Peter KromanSales ExecutiveAuthor Commented:
Yes - this filtering is OK. But he filtering in question is the filtering in the selectbox "Vælg et år" which is the problem :)
0
Peter KromanSales ExecutiveAuthor Commented:
It should filter the years displayed in the dropdown according to the choices made by the user in the two other select boxes. And that is not happening :)
0
Julian HansenCommented:
Ok but I thought we are dealing with AMT and TYPE?
0
Peter KromanSales ExecutiveAuthor Commented:
We are :)
Problem is that choice made in the Amt and Type should limit the displayed years in the "Vælg et år" select-box to the years the choises made in one, or both, of the "Amt" and/or "Type" select-boxes.
0
Peter KromanSales ExecutiveAuthor Commented:
By the way - the select-box "Type" is only working correctly in cooperation with the select-box "Vælg et år" with this code.
But still the select-box "Amt" is not working at all in this sence.

<?php

// Turn on error reporting
error_reporting(E_ALL);
ini_set('display_errors', 1);

require_once('ft_data_connection.php');
 

$mysqli->set_charset("utf8");


$amt = trim($_POST['amt']);
$type = trim($_POST['type']);
$aar = trim($_POST['aar']);
 
$amt = isset($_POST['amt']) ? $_POST['amt'] : false;
$type = isset($_POST['type']) ? $_POST['type'] : false;
$aar = isset($_POST['aar']) ? $_POST['aar'] : false;
 


// We add 1=1 so we can do the AND condition with new criteria without having
// to trim off the last one
$query = "SELECT DISTINCT Aar AS field FROM ft_children WHERE 1=1";
$filter = '';
if ($type) {
  $filter .= " AND `Type` = '{$type}'";
}
if ($amt) {
  $filter .= " AND `Amt` = '{$amt}'";
}

// Repeat for any other conditions you want to add
$query .= $filter;

// ADD THE ORDER BY HERE. YOU COULD DO IT AS PART
// OF THE PREVIOUS LINE I DO IT HERE SO THAT YOU
// HAVE THE OPTION OF TURNING IT ON OR OFF
$query .= " ORDER By Aar ASC";

$result = $mysqli->query($query);
if ($result) {
  echo "<option> </option>";
  while($row = $result->fetch_object()) {
    echo <<< OPTION
<option value="{$row->field}">{$row->field}</option>
OPTION;
  }
}
// WHILE DEBUGGING ADD THIS
// else {
//  echo "DB Error: {$mysqli->error}<br>QUERY: {$query}<br>";
// }

Open in new window

0
Julian HansenCommented:
Sorry Peter I misunderstood the question

The problem is you have two controls on your page with id="amt"
<input name="amt" id="amt" type="text">

Open in new window

AND
<select id="amt" name="amt">

Open in new window

So when you do $('#amt').val() you are not getting the right control.

There is another problem - line 15 is bombing because there is no parameter aar and you are specifically indexing $_POST['aar']

In fact lines 13-15 are superfluous and don't do anything except potentially break the code - as parameter collection is done safely on lines 17-19
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Peter KromanSales ExecutiveAuthor Commented:
Thanks Julian,

I will look into it during the day, and get back. I have to rush out now :)
0
Peter KromanSales ExecutiveAuthor Commented:
Hi Julian,

I changed one of the id="amt" to id="amt2" in the HTML and the corresponding script, and I commented out the lines that you pointed out was not needed. I still got the same missing result.

Well, afterwards I found another reason in the script handling this limitation which have two nested functions. One expected a $.post in the limit.php file, and the other expected a $.get. When changing that so that they both expect a $.post in the script, everything works nicely again.
0
Julian HansenCommented:
Glad to hear it.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
SQL

From novice to tech pro — start learning today.