Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7508673
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T22:42:54+00:00 2026-05-29T22:42:54+00:00

I have an jQuery ajax database query and Im wondering how I can maybe

  • 0

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.

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):

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”:

    $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. Am I doing something wrong here?

Thanks people!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T22:42:55+00:00Added an answer on May 29, 2026 at 10:42 pm

    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 .

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written some jQuery ajax code where I am sending a request to
I have this jquery.ajax form which populates data from the database and auto fills
I have a jQuery AJAX call with type:'GET' like this: $.ajax({type:'GET',url:'/createUser',data:userId=12345&userName=test, success:function(data){ alert('successful'); }
I have a jquery-ajax function that sends data to a php script and the
A situation I ran across this week: we have a jQuery Ajax call that
I have done jQuery and Ajax, but I am not able to get the
I have a simple jquery/ajax request to the server which returns the structure and
Let's say I have the following jQuery AJAX call: $.ajax({ type: POST, url: MyUrl,
I have a jQuery script: $.ajax({ url: /scripts/secure/development/ajax.asp, type: POST, dataType: text, data: cmd=addresses,
I have written a simple jQuery.ajax function which loads a user control from the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.