我有一个“小”问题,因为我不知道什么是处理ajax jquery数据库请求的最佳方法?
到目前为止,我已经这样做:
对于永远(单个)数据库请求,我创建了一个新的.php文件,例如:
newbill.php
newcosumer.php
listallbills.php
updatecostumer.php
等等,然后通过以下方式提取数据:
$.ajax({
type: "POST",
url: "ajax/newbill.php",
data: "..."
success: function(msg){
...
}
});
..我不喜欢的是所有.php文件只有一个!数据库请求!?
必须有更好的方法来解决这个问题?
最佳答案
我会以这种方式做。
为每个实体创建一个php文件(例如:Consumerajax.php,billajax.php等),然后为每个数据库查询分别提供函数。从javascript调用时,传递一个querystring变量以确定要执行的函数在ajax服务器页面中
例如:在Consumerajax.php中
<?php
$mode= $_GET["mode"] ;
if($mode=="getconsumerdet")
{
$consumerId=$_GET["cid"];
GetConsumerDetails($consumerId)
}
else if($mode=="updateconsumer")
{
$consumerId=$_GET["cid"];
$name=$_GET["name"];
UpdateConsumerInfo($consumerId, $name)
}
function GetConsumerDetails($consumerId)
{
// execute query the table here and return the result from here
echo"Have the data and return"
}
function UpdateConsumerInfo($consumerId,$name)
{
//execute query to update
echo "updated";
}
?>
从你的代息电话
$.ajax({ type: "POST", url: "ajax/consumerajax.php?mode=getconsumerdet&cid=34", data: "..." success: function(msg){ });
要么
$.ajax({ type: "POST", url: "ajax/consumerajax.php?mode=updateconsumer&cid=34&name=shyju", data: "..." success: function(msg){ });
关于jquery - 处理ajax jquery数据库查询的最佳实践?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3872280/