Dynamic select field according to 2 other select fields selection
I am in PHPMaker 2024
in my add page (casesAdd.php) the 3 select field names are
x_drop_requested, x_drop_atoll and x_drop_selectedby
x_drop_selectedby should by dynamically changing according to the selection of x_drop_requested and x_drop_atoll.
i have tested the function with standalone pages with
casesAdd_page_script.php
<script src="jquery/jquery.min.js?v=24.5.7"></script>
<script>
$(document).ready(function() {
// Function to fetch options based on selected values
function fetchOptions(selectedRequested, selectedAtoll) {
$.ajax({
type: "POST",
url: "4mycust_selectedby.php",
data: {
selectedRequested: selectedRequested,
selectedAtoll: selectedAtoll
},
success: function(response) {
$("#x_drop_selectedby").empty().append(response);
console.log("Received options:", response);
},
error: function(xhr, status, error) {
console.error("Error:", error);
}
});
}
// Event listener for x_drop_requested change
$("#x_drop_requested").change(function() {
var selectedRequested = $(this).val();
var selectedAtoll = $("#x_drop_atoll").val();
// Make sure selectedRequested and selectedAtoll are not empty
if (selectedRequested && selectedAtoll) {
// Fetch options based on selected values
fetchOptions(selectedRequested, selectedAtoll);
}
});
// Event listener for x_drop_atoll change
$("#x_drop_atoll").change(function() {
var selectedRequested = $("#x_drop_requested").val();
var selectedAtoll = $(this).val();
// Make sure selectedRequested and selectedAtoll are not empty
if (selectedRequested && selectedAtoll) {
// Fetch options based on selected values
fetchOptions(selectedRequested, selectedAtoll);
}
});
});
</script>
<select id="x_drop_requested" name="x_drop_requested">
<option value="">Select Option</option>
<option value="z_gov">GOV</option>
<option value="z_councils">COUNCIL</option>
<option value="z_schools">SCHOOL</option>
<!-- Add other options here -->
</select>
<select id="x_drop_atoll" name="x_drop_atoll">
<option value="1">HA (Thiladhunmathi Uthuru)</option>
<option value="2">HDh (Thiladhunmathi Dhekunu)</option>
<!-- Add atoll options here -->
</select>
<select id="x_drop_selectedby" name="x_drop_selectedby">
<option value="">Select Selected By</option>
<!-- This will be populated dynamically based on selection -->
</select>
4mycust_selectedby.php
<?php
error_log("POST Data: " . print_r($_POST, true));
// Include the database connection file
require_once "1my_config.php";
// Check if the key exists in $_POST
$selectedRequested = isset($_POST['selectedRequested']) ? $_POST['selectedRequested'] : '';
$selectedAtoll = isset($_POST['selectedAtoll']) ? $_POST['selectedAtoll'] : '';
// Create a function to get the options based on the selected values
function getOptions($selectedRequested, $selectedAtoll) {
global $my_conn;
// Escape the values to prevent SQL injection
$selectedRequested = mysqli_real_escape_string($my_conn, $selectedRequested);
$selectedAtoll = mysqli_real_escape_string($my_conn, $selectedAtoll);
// Check if $selectedAtoll is not empty before executing the query
if (!empty($selectedRequested) && !empty($selectedAtoll)) {
// Determine the table name based on the selected option
switch ($selectedRequested) {
case 'z_gov':
$table = 'z_gov';
break;
case 'z_councils':
$table = 'z_councils';
break;
case 'z_schools':
$table = 'z_schools';
break;
case 'z_ngo':
$table = 'z_ngo';
break;
case 'z_combiz':
$table = 'z_combiz';
break;
case 'z_International':
$table = 'z_individual';
break;
case 'International':
$table = 'z_International';
break;
case 'z_resort_hotel':
$table = 'z_resort_hotel';
break;
case 'NILL':
// For NILL option, return an empty string
return '';
default:
// If none of the above options match, return an empty string
return '';
}
// Modify the SQL query to use the determined table
$sql = "SELECT name FROM $table WHERE id_atoll = '$selectedAtoll'";
error_log("SQL Query: " . $sql); // Log the SQL query
// Execute the SQL query
$result = mysqli_query($my_conn, $sql);
// Check for errors
if (!$result) {
error_log("Error in SQL query: " . mysqli_error($my_conn));
die("Error in SQL query: " . mysqli_error($my_conn));
}
// Check if any rows are returned
if (mysqli_num_rows($result) == 0) {
error_log("No options found for the given criteria.");
return '';
}
$options = ''; // Initialize options variable
while ($row = mysqli_fetch_assoc($result)) {
$option = htmlspecialchars($row['name']);
$options .= "<option value='$option'>$option</option>";
}
// Free result set
mysqli_free_result($result);
error_log("Retrieved Options: " . $options); // Log the retrieved options
// Echo options directly
echo $options;
}
}
// Call the function with the provided values
getOptions($selectedRequested, $selectedAtoll);
// Close the database connection
mysqli_close($my_conn);
?>
When i add the casesAdd_page_script just below the view/casesAdd.php and keep the 4mycust_selectedby.php in the root directory, i get the following error
POST http://localhost/j9ptl_cases2/api/lookup?q=&n=100&rnd=114003&start=-1 401 (Unauthorized)
Warning: Undefined array key "page" in D:\xampp8.2.12\htdocs\j9ptl_cases2\src\ApiPermissionMiddleware.php on line 97
Warning: Undefined array key "field" in D:\xampp8.2.12\htdocs\j9ptl_cases2\src\ApiPermissionMiddleware.php on line 98
please help