Hi

I have a varible that is set at as a different value for each page this is called $mode.

Currently I have succesfully set up a drop down menu that can filter the table and refresh the results based on that filter chosen, yet I need to call this $mode variable into the query to be able to get the true results back that I require.

The php page to call the filter code is as follows

<?php
$q = intval($_GET['q']);

$con = mysqli_connect('x','x','x');
if (!$con) {
    die('Could not connect: ' . mysqli_error($con));
}

mysqli_select_db($con,"x");
$sql="SELECT * FROM tab1 WHERE info = '".$q."' and id='$mode' LIMIT 10";
$result = mysqli_query($con,$sql);

echo "blar blar blar

and another one that shows the results on the page

<script>
function showUser(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 (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
            }
        }
        xmlhttp.open("GET","info.php?q="+str,true);
        xmlhttp.send();
    }
}
</script>


<form>
<select name="Info" onchange="showUser(this.value)">
  <option value="">Select a mod:</option>
  <option value="1">this</option>
  <option value="2">that</option>
  <option value="3">other</option>
  </select>
</form>
<br>
<div id="txtHint"><b>NEw info shows here</b></div>

IN the page that these pages that I echo the above page into, it has the variable $mode
, which sucessfully is iised in other queries in the page but $q is passed from the drop down fine but id='$mode' is showing as blank

How do I get this $mode to show the true value in this instance please?

Thanks for your help

Hi

In info.php you are having 2 variable such as $q and $mode.

but you are getting a value for $q variable from dropdown value.

what abt $mode?

You've not defined $mode.

thanks for your replies, how would I define $mode also please?

Can anybody tell me how to define $mode please?

Anybody give me any idea at least?

I have tried in xmlhttp.open setting &$mode= but I can't get it to work

Member Avatar for diafol

The php script runs "as is", prompted by the Ajax call. So, $mode needs to be set somewhere in order for the query to run successfully. You could pass it via Ajax:

xmlhttp.open("GET","info.php?q="+str+"&mode="+mode,true);

And pick it up in the php thus...

$mode = $_GET['mode'];

BUT - you are using mysqli, so be kind to yourself and use parameter binding and prepared statements. Otherwise you will set yourself up for SQL injection - and yes - it's even more painful than it sounds.

WOW thanks for the advice, I will change i and also try what you have sent me, thanks again

BTW that works great thank you very much !!

commented: Thanks for coming back and letting us know +15
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.