0
else if($('#operator').val() == "choose"){
                <?php  $terminal_query = "SELECT distinct Terminal from mytable";?>
                <?php  $terminal_result = $connect->query($terminal_query); ?>
                <?php  while ($terminal_row = mysqli_fetch_array($terminal_result)) 
                {
                ?>

                      $('#terminal').append('<option value='<?php echo $terminal_row["Terminal"]; ?>'>'<?php echo $terminal_row["Terminal"]; ?>'</option>');          
                <?php
                }
                 ?>

        }

I want to change MYSql query depends on the selected options

Rajesh
  • 24,354
  • 5
  • 48
  • 79
noli
  • 29
  • 1
  • 4
  • @Protectator I do not see anything wrong with the first line? – Nicholas Aug 28 '17 at 12:24
  • Adding the missing `)` will not fix this. You cannot mix PHP and JavaScript like that. PHP is executed on the server, once. The result is sent to the browser, and only then will JS code execute. What you want to do is commonly solved using AJAX; i.e. clicking a button or sending a form will run an AJAX request which returns data; this data is then used to alter the HTML. –  Aug 28 '17 at 12:24
  • What should i do so that i won't received an error of Uncaught SyntaxError: missing ) after argument list? Please answer. – noli Aug 28 '17 at 12:26
  • Actually,what i really want to do is to update select option based on another select option with database. So i tried with that solution but it won't work. I saw your link @rajesh but it won't solved my problem. – noli Aug 28 '17 at 12:34
  • @noli Currently, you have a syntactical mistake. So most of the users will ignore the post as its an obvious mistake. My suggestion is to first try and fix the syntax. Once thats done, go deeper and kill more demons. – Rajesh Aug 28 '17 at 12:37

1 Answers1

0

Like I explained in my comment, use AJAX to do this, more specifically, $.getJSON:

First create a basic API:

api.php

<?php
$action = $_GET['action'];
if ($action == "choose") $terminal_query = "SELECT distinct Terminal from mytable";
// other actions here

$terminal_result = $connect->query($terminal_query);
$response = [];
if ($terminal_result) {
    while ($terminal_row = mysqli_fetch_array($terminal_result)) $response[] = $terminal_row['Terminal'];
}
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=utf-8");
die(json_encode($data, JSON_UNESCAPED_UNICODE | JSON_NUMERIC_CHECK));

In this example I'm sending back JSON which looks like this:
[ "Terminal 1", "Terminal 2", "Terminal 3" ]

Now run a request for the data, and add it to your HTML document, like this:

else if ($('#operator').val() == "choose") {
    $.getJSON("api.php?action=choose", function(response) {
        for (var i in response) {
            $('#terminal').append('<option value="' + response[i] + '">' + response[i] + '</option>');
        }
    });
}
  • thank you for giving your time answering my question. I really appreciate your effort man. For the mean time, let me just think for the exact output what i'm trying to achieved. – noli Aug 28 '17 at 13:24
  • @noli If this helped you, could you mark it as answer? (if still possible) –  Aug 29 '17 at 08:27