简体   繁体   English

我如何知道单击了哪个按钮?

[英]How can I tell which button was clicked?

I have two butons prev/next that call a getJSON . 我有两个按钮prev / next,它们调用getJSON I want to alter a query depnding on what button if any were clicked. 我想更改有关单击什么按钮的查询。 Here are my buttons and getjson statments 这是我的按钮和getjson语句

<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. 我想使用GET中的LoadNumber以下操作。

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. 在服务器端,使用$ _GET ['direction']来查看下一个还是下一个。

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! 顺便说一句,“ isset()”仅需2个“ s”;)希望对您有所帮助,如果我错过了要点,您可以发表评论!

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: 我对此可能是错的,但是我相信,如果您想在$ _GET超级全局变量中访问“ LoadNumber”,则需要像这样在$ .getJSON中传递数据参数的对象:

    $.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/ 我以前没有使用$ .getJSON(),但是从文档中使用过: 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. 它说$ .getJSON只是使用get request方法的$ .ajax的简写,因此,如果您使用所要查找的键传递对象,那么我的猜测是$ .getJSON会将其放入$ _GET中,抓。

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. 同样,Kolkin是正确的,该代码容易受到SQL注入的攻击,您永远不要将$ _GET,$ _ POST或$ _REQUEST中的数据直接插入SQL查询中而不进行过滤。 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. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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