0

I'll try and explain as best as I can. I'm trying to create two drop down menu's that count and show different products with a AND function in the SQL query. See below for my code. At this point it's working but I would like to have the second options be queued dynamically with the option that is selected from the second drop down menu name="categorie". At this point I hard coded the second option in my query to test if it's working:

$query = "SELECT COUNT(".$q.") c FROM MirrorWebProductsExpanded WHERE Subcategorie = 'Citygames'";

What I would like is to have the WHERE Subcategorie = be something like WHERE Subcategorie =".$y." with the options selected from the drop down menu name="categorie". I think the problem is with the $_SESSION['categorie']; that this session is not saved and run through the AJAX script. Any ideas on how to get this to work?

HTML

<div class="row">
    <form class="header-search-form">
        <select name="thema" onchange="sortboottochten(this.value)" class="selectpicker">
            <option value="t1" data-icon="glyphicon-option-vertical" class="special">&nbsp;&nbsp;Kies een thema</option>
                <optgroup label="Kies een thema">
                    <option value="Actief_Avontuur" data-icon="glyphicon-sort-by-alphabet" class="special">&nbsp;&nbsp;Actief, sportief en avontuurlijk</option>
                    <option value="Creatief" data-icon="glyphicon-sort-by-alphabet-alt" class="special">&nbsp;&nbsp;Creatief</option>
                    <option value="Culinair" data-icon="glyphicon-sort-by-attributes" class="special">&nbsp;&nbsp;Culinair</option>
                    <option value="Fun_Entertainment" data-icon="glyphicon-sort-by-attributes-alt" class="special">&nbsp;&nbsp;Fun en entertainment</option>
                    <option value="Kunst_Cultuur" data-icon="glyphicon-sort-by-attributes" class="special">&nbsp;&nbsp;Kunst en cultuur</option>
                    <option value="Muziek_Zang_Dans" data-icon="glyphicon-sort-by-attributes" class="special">&nbsp;&nbsp;Muziek, zang en dans</option>
                    <option value="Teambuilding" data-icon="glyphicon-sort-by-attributes" class="special">&nbsp;&nbsp;Teambuilding</option>
                    <option value="Vrijgezellen" data-icon="glyphicon-sort-by-attributes" class="special">&nbsp;&nbsp;Vrijgezellen</option>
                </optgroup>
        </select>
    </form>
</div>
</div>
<div class="col-xs-3 col-sm-3 col-md-3 col-lg-4">
    <form class="header-search-form">
        <select name="categorie" onchange="sortboottochten(this.value)" class="selectpicker">
            <option value="CAST(Ranking AS int) ASC" data-icon="glyphicon-option-vertical" class="special">&nbsp;&nbsp;Kies een categorie</option>
                <optgroup label="Sorteren op...">
                    <option value="Citygames" data-icon="glyphicon-sort-by-alphabet" class="special">&nbsp;&nbsp;Naam (A-Z)</option>
                    <option value="" data-icon="glyphicon-sort-by-alphabet-alt" class="special">&nbsp;&nbsp;Naam (Z-A)</option>
                    <option value="" data-icon="glyphicon-sort-by-attributes" class="special">&nbsp;&nbsp;Prijs (laag - hoog)</option>
                    <option value="" data-icon="glyphicon-sort-by-attributes-alt" class="special">&nbsp;&nbsp;Prijs (hoog - laag)</option>
                </optgroup>
        </select>
    </form>
</div>
<div class="col-xs-3 col-sm-3 col-md-3 col-lg-4">
    <div class="row">
        <div id="txtHint" class="btn review-button">Bekijk alle <?php echo $row['c'];?> uitjes &nbsp;&nbsp;<i class="fa fa-chevron-right" aria-hidden="true" style="font-size:1rem;color:#fff;"></i></div>
        <div class="load-screen-2">
            <i class="fa fa-spinner fa-pulse fa-3x fa-fw"></i><span>Bezig met sorteren...</span>
        </div>
    </div>
</div>

SCRIPT

function sortboottochten(str) {
    if (str == "") {
        document.getElementById("txtHint").innerHTML = "";
        return;
    } else {
        if (window.XMLHttpRequest) {
            // code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        } else {
            // code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function() {
            if (this.readyState == 4 && this.status == 200) {
                $('#txtHint').hide().fadeIn(2100);
                document.getElementById("txtHint").innerHTML = this.responseText;
                setTimeout(function() {$('.load-screen-2').hide().fadeOut(2000);}, 2000);
                console.log('Loading Done');
            }
        };

        $('.load-screen-2').show().fadeIn(2000);
        xmlhttp.open("GET","/header-search.php?q="+str,true);
        xmlhttp.send();
        console.log('Loading Start');

    }
}

header-search.php

<?php
    session_start();
    $servername = "localhost";
    $username = "";
    $password = "";
    $dbname = "";

    // Create connection
    $conn = mysqli_connect($servername, $username, $password, $dbname);

    // Check connection
    if (!$conn) {
        die("Connection failed: " . mysqli_connect_error());
    }
$q = $_GET['q'];
$_SESSION['thema'] = $q;
$_SESSION['categorie'] = $y;

$query = "SELECT COUNT(".$q.") c FROM MirrorWebProductsExpanded WHERE Subcategorie = 'Citygames'";
$result = mysqli_query($conn,$query);
$row = mysqli_fetch_assoc($result);
echo "<a href='http://mysite.nl/zoeken/'>Bekijk alle " . $row['c'] .  " uitjes</a>";
?>
Jay-oh
  • 426
  • 2
  • 6
  • 28
  • why dont you use jquery's ajax function ? mixing jquery and plain js is not a good practice IMHO. as to your question, if I understood correctly, you want to have the second dropdown change its values when the first dropdowns selected item changes. if so, check this http://stackoverflow.com/questions/22750823/populate-html-php-dropdown-based-on-first-dropdown-selection – Mightee Apr 13 '17 at 09:52
  • @Mightee 1: Thanks for the comment. But, I don't want to change the value of the dropdown. I would like to change the SLQ query when selecting the second option by adding a value to the SQL query. – Jay-oh Apr 13 '17 at 11:11
  • then just use another Ajax query and send additional parameter something like action=filter and check it in PHP. from your code I see that you are new to programming. search for better code practices. search for how to show and filter data with php – Mightee Apr 13 '17 at 17:14

0 Answers0