简体   繁体   English

如何使我的jQuery数据库ajax​​请求更加有效和灵活?

[英]How can I make my jQuery database ajax request more efficient and flexible?

I have an jQuery ajax database query and Im wondering how I can maybe make it more flexible, or if anyone can shed some light on a better approach as I am just beginning to learn ajax and need a little advice. 我有一个jQuery ajax数据库查询,我想知道如何使它更灵活,或者在我刚刚开始学习ajax并需要一点建议的情况下,是否有人可以对更好的方法有所了解。

Heres what I have so far: 这是我到目前为止的内容:

        <form method="post" name="sc_ajax" onsubmit="checkDB(document.sc_ajax.sc_voucher_code.value); return false;">
     ...
        </form>

The form is set so on submit it runs the following ajax function (on same page): 设置表单后,在提交时它将运行以下ajax函数(在同一页面上):

function checkDB(code)
{

  $.ajax({
  type: "POST",
  url: "<?php bloginfo('template_url'); ?>/profile/check_code.php",
  data: 'code='+code,
  datatype: "html",
  success: function(result){

       if(result == 0)
        {
            $('#success').html( code + ' already exists and has not been redeemed...');
            // alert('success');//testing purposes
        }
        else if(result == 2)
        {
            $('#err').html(  code + ' already exists and has already been redeemed....');
            //alert('fail');//testing purposes
        }else if(result == 1){
            $('#err').html(  code + ' redeem code doesnt exist');      
        }

        alert(result);      
      }
  })

}

So this gets result from a seperate file called "check_code.php": 因此,这是从一个名为“ check_code.php”的单独文件获得的结果:

    $code = mysql_real_escape_string($_POST['code']);  

$resultcode = mysql_query('select * from wp_scloyalty where code = "'. $code .'"' ) or die(mysql_error());  


$redeemed=array();
while($info = mysql_fetch_array( $resultcode )) 
{ 

    $redeemed[$info["code"]] = $info["redeemed"];

} 

if(mysql_num_rows($resultcode)>0 && $redeemed[$code] == 0){
    //Exists so change redeemed code to 1 
    echo 0;   

}elseif(mysql_num_rows($resultcode)>0 && $redeemed[$code] == 1){
    //Exists but already redeemed, so throw error
    echo 2;

}else{  
    //Doesnt Exist
    echo 1;  
}  

The code works perfectly by the way... but my question is... I am currently running a bunch of "if statements" from the check_code.php file, but is there a better way to do this as I have been looking round for tips on using ajax and other peoples approaches seem a lot more confusing and cluttered with code. 顺便说一句,代码可以正常工作……但是我的问题是……我目前正在运行一堆来自check_code.php文件的“ if语句”,但是有更好的方法可以做到这一点,因为我一直在寻找有关使用Ajax和其他人的方法的技巧似乎更加混乱,而且代码混乱。 Am I doing something wrong here? 我在这里做错什么了吗?

Thanks people! 谢谢大家!

Number of rows should be calculated first because if the query returns zero rows, fetch array will raise an error. 应该首先计算行数,因为如果查询返回零行,则访存数组将引发错误。 As you can see, you're fetching result array more than once. 如您所见,您不止一次获取结果数组。 No need to do that. 没必要那样做。 Store it before you fetch the result array and use it for comparison later on. 在获取结果数组之前将其存储,并在以后用于比较。 Relating to your ajax, use cache:true in jQuery for speed up . 关于您的ajax,请在jQuery中使用cache:true加快速度。

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

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