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 KromanSenior Proposal SpecialistAsked:
Who is Participating?
 
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
 
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 KromanSenior Proposal SpecialistAuthor 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
Julian HansenCommented:
Selecting Diverse and Bornholm seems to filter the results correctly
ss156.jpg
0
 
Peter KromanSenior Proposal SpecialistAuthor 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 KromanSenior Proposal SpecialistAuthor 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 KromanSenior Proposal SpecialistAuthor 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 KromanSenior Proposal SpecialistAuthor 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
 
Peter KromanSenior Proposal SpecialistAuthor Commented:
Thanks Julian,

I will look into it during the day, and get back. I have to rush out now :)
0
 
Peter KromanSenior Proposal SpecialistAuthor 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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.