-1

I have a selectbox where i can choose my clients and each client could have 1 or more sites, so when you change your client selection the sites selectbox should also change, but teh main problem is that i can't fill the sites selectbox with options.

I thought it should be the best way to return an array or something.

Mansour Baitar
  • 55
  • 1
  • 11
  • Please have a read about SQL injection and ensure that the code you end up using isn't vulnerable to it (this is) – scrowler Oct 17 '18 at 13:26
  • Why are you building the options with JavaScript? – epascarello Oct 17 '18 at 13:26
  • my guess it is a dupe of https://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron – epascarello Oct 17 '18 at 13:27
  • is it possible to inject the db right now? How can you do that, because you have no server creds and i know i should use "mysqli_real_escape_string" – Mansour Baitar Oct 17 '18 at 13:28
  • @epascarello because, my supervisor told me to do it that way, i new to php so i believed him xd – Mansour Baitar Oct 17 '18 at 13:29
  • @MansourBaitar just do a Google Search for `how to prevent SQL injection in PHP`. You'll find ample tutorials, code snippets etc – Rotimi Oct 17 '18 at 13:30
  • `dataType: 'array'`? That's a new one. You should check the jQuery documentation. And use prepared statements in php. – jeroen Oct 17 '18 at 13:30
  • whoops..... "why" was supposed to be "where".... "Where are you building the options with JavaScript?" I see the Ajax call, I just do not see where you are using the data that is returned. – epascarello Oct 17 '18 at 13:36
  • @epascarello Well I'm doing alert(output) in the succes function, just to see if he fetched the data from de DB – Mansour Baitar Oct 17 '18 at 13:39
  • SO is the alert firing? – epascarello Oct 17 '18 at 13:57
  • Are you trying to get client names from data base then populate those names as drop down options and when a client get selected you want to call all the sites which are under that client and then populate those in to another drop down. Is this what you want to do ? @MansourBaitar – S4NDM4N Oct 17 '18 at 14:01
  • @epascarello it kinda does, The alert msg is `output xdConnected successfullyArray ` – Mansour Baitar Oct 17 '18 at 14:03
  • @Sand Yes that is what I'm trying to do. – Mansour Baitar Oct 17 '18 at 14:03
  • okay so your PHP code should just return the array of data. There is no need to return the other stuff. – epascarello Oct 17 '18 at 14:08
  • @epascarello what do you mean with other stuff? – Mansour Baitar Oct 17 '18 at 14:25

2 Answers2

0

Try adding something like this to your ajax success:

var dropdown = document.getElementById('SiteDropDown');
options.forEach(function(element))
{
    dropdown.options[dropdown.options.length] = new Option(element, '', false, false);
});  

Essentially creating an array with the information you want to be displayed, is a good idea. But you should try to also work with a value on the select. So when you select an an option the system will know it's id. -> dropdown.options[dropdown.options.length] = new Option(element, 'ID of Array Element', false, false);

  • when I add your code to my success, and i choose a client i see in the console that their is an error "Uncaught ReferenceError: option is not defined" – Mansour Baitar Oct 17 '18 at 13:58
0

I took a little bit of time but wrote something that might work and simple. Also if you're using AJAX you really don't need to have form tags. You can create form with dives and use a JQuery function to fire AJAX to send data to the database.

Main PHP to populate your first drop down.

<?php
  include_once("../Services/Mysql.php");
  $result = $conn->query("SELECT klant_firmanaam, id From connectprolivedesk.klanten where reseller_klantnr = '5375' order by klant_firmanaam");
?>

<html>
 <head>
   <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
    <div class="form-group">
        <select name="klantenDropDown" id="klantenDropDown" class="form-control">
            <option value="" selected hidden disabled>Select Klant</option>
                <?php
                    if($result->num_rows > 0){
                        while($row = $result -> fetch_assoc()){
                            echo "<option value=".$row["id"]." >".$row["klant_firmanaam"]."</option>";
                        }
                    }
                ?>
        </select>
    </div>
        <div class="form-group">
            <select name="SiteDropDown" id="SiteDropDown" class="form-control" >
            <option value="" selected disabled hidden>Select Site</option>
            </select>
        </div>
 </div>
</body>

This PHP will receive the AJAX post variable and echo out the options which will populate the second drop down.

include_once("../Services/Mysql.php");

$result = $conn->prepare("SELECT site_naam FROM connectprolivedesk.sites WHERE klant_id = ?");
$result -> bind_param("s",$_POST["klantId"]);
$result -> execute();

while($row = $result -> fetchAll()){
   echo "<option value=".$row['sId'].">".$row['site_name']."</option>:
}

This code is for your AJAX which will send the your set kalntId to the above second PHP onChange of your first drop down. The ajax will send the selected clients ID to the PHP so you can sort out the sites only for that client. Then will capture the echoed output and insert those as option in to the second drop down.

<script>
    $(document).ready(function(){
        $('#klantenDropDown').on('change', function(){
            $.ajax({
                url: 'getSites.php',
                type: 'post', //You can use get or post
                data: {
                    'klantId':$(this).val()
                },
           dataType:'text',
           complete:function(data){
            if(data){
                    $('#SiteDropDown').html(data.responseText);
                }
            }
        }
    }
</script>

Hope this helps you or any other.

P.S: I'm not a big mysqli_* user if there's any errors in my code please let know. Also I used prepared statement where the second PHP binds the value which is much safer and prevents SQL injection.

S4NDM4N
  • 904
  • 2
  • 11
  • 26
  • Thanks for the reply. This would prob work, but apprently you can't use prepared statements with fetch_assoc – Mansour Baitar Oct 18 '18 at 08:18
  • i see this error: `Fatal error: Call to undefined method mysqli_stmt::fetch_assoc()` – Mansour Baitar Oct 18 '18 at 08:28
  • Chang the `fetch_assoc()` to `fetchAll()`. And when you get an error like the one you have just google it the answers are out there. – S4NDM4N Oct 18 '18 at 08:33
  • Thank you for marking my answer and glad it worked out for you. Also your question add the code you're working on or it might get down voted a lot because the question get considered as low quality. @MansourBaitar – S4NDM4N Oct 18 '18 at 10:36