简体   繁体   中英

How can I tell which button was clicked?

I have two butons prev/next that call a getJSON . I want to alter a query depnding on what button if any were clicked. Here are my buttons and getjson statments

<input class="buttonsr" type="button" onClick="next()" name="NextLoad" value="Next Load"><input class="buttonsr" type="button" onClick="prev()"name="PrevLoad" value="Prev. Load">
$.getJSON("loadloads.php", document.getElementById('LoadNumber').value, jsonhandler)

I'd like to do something like below using the LoadNumber from the GET.

if isset($_GET['prev']){
    $find = 'Where L.$_GET['LoadNumber'] > (max)LoadNumber FROM tblLoads'
}
if isset($_GET['next']){
    $find = 'Where L.$_GET['LoadNumber'] < (min)LoadNumber FROM tblLoads'
}
else{
    $find = 'L.LoadNumber = (SELECT MAX(LoadNumber) FROM tblLoads)'
};

You should use the same function and just pass a parameters to the server. Something like

<input class="buttonsr" type="button" onClick="page('next')" name="NextLoad" value="Next    Load">

function page(direction) {
   $.getJSON("loadloads.php?direction="+direction,document.getElementById('LoadNumber').value,jsonhandler)
}

On the server side, use the $_GET['direction'] to see if you're going NEXT or PREVIOUS.

if ($_GET['direction'] == 'next') { 
   // GO NEXT
} else {
   // GO PREV
}

And by the way, "isset()" only takes 2 "s" ;) Hope this helped, you can comment if i missed the point!

I could be wrong on this, but I believe if you want to access "LoadNumber" in the $_GET super global then you need to pass in an object for the data parameter in $.getJSON like this:

    $.getJSON("loadloads.php", { LoadNumber:$('#LoadNumber').val() }, jsonhandler);
    // NOTE: I used jquery shorthand instead of document.getElementById() to get the value
    // simply to shorten it up.

I have not used $.getJSON() before but from the documentation: http://api.jquery.com/jQuery.getJSON/
it says that $.getJSON is just shorthand for $.ajax using a get request method so if you pass in an object with the key of what you are looking for then my guess is $.getJSON will put that into $_GET for you to grab.

Also Kolkin is correct this code is vulnerable to SQL injection, you should NEVER insert data from $_GET, $_POST or $_REQUEST directly into your SQL query without filtering it. You are potentially opening your code up to malicious injections that could either wipe out data in your DB or access sensitive data and return it.

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM