0

Essentially I have a series of buttons that I'm using to filter a table of data.

I have gotten my first two buttons to work okay with the following:

<button onclick="filter('open');" class="open">Open</button>
<button onclick="filter('closed');"class="closed">Closed</button>

The function looks like this :

function filter(st, location) {
        if(st == "") {
            document.getElementById("exams").innerHTML = "";
            return;
        } else {
            if(window.XMLHttpRequest) {
                var xmlhttp = new XMLHttpRequest();
            } else {
                var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlhttp.onreadystatechange = function() {
                if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                    document.getElementById("exams").innerHTML = xmlhttp.responseText;
                }
            };
            xmlhttp.open("GET", "ajax.php?st="+st+"&location="+location, true);
}}

In the ajax.php file I am checking for the st parameter to set a variable that I use in my query:

if (isset($_GET["st"])) {
    $status = $_GET["st"];
} else {
    $status = null;
}

$query = "SELECT examID, `status`, site, first_name, last_name, exam_name, institution FROM exams WHERE status = '{$status}'";

Now I want to pass it another parameter based on another button click to further filter the data. I'm not sure what to put for the parameters though to get this to work.

<button onclick="filter();" class="new-york">New York</button>
3
  • Where do you want to pass another parameter? Commented May 23, 2016 at 3:45
  • If I have another button like <button onclick="filter();">New York</button> I want to pass the location New York to the second filter() function argument "location" Commented May 23, 2016 at 3:51
  • 1
    If your function is declared to use 2 arguments, don't pass one only... create a second function that receives location as parameter. Commented May 23, 2016 at 3:56

1 Answer 1

1

I think if I were to do such a thing, I may think about using a data attribute. This way you can pass multiple attributes as well as the action value. There is some expandability there:

<button class="mybutton" data-instructions='{"action":"open","param1":"something","param2":"something else"}'>Open</button>

On the ajax, send the data object as a $_POST (or $_GET), then you can just grab it on the PHP side with relative ease. If you do it this way, you just make one action that triggers on the mybutton class. There may be some automation that you could program in there.

Another way might be to use innerHTML to get the contents from inside the clicked element, then pass it on to the php normally.

Side note, as you have it, your sql is open to injection.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the options on this. I realized today that what I was trying to do didn't really make sense. What I decided to do instead was just create radio buttons and style them like buttons. That way I can have a submit button and do it as a normal post request through PHP.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.