2

The problem I am having is understanding how to use the jQuery ajax capabilities to return results for multiple scenarios of a database query.

Here is the code I have so far... this is the ajax function:

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.');
       // alert('success');//testing purposes
       }
       else
       {
        $('#err').html(  code + ' isnt in the database!!.');
        //alert('fail');//testing purposes
       }

alert(result);     


      }
  })

}

And this uses the following form to run the function on submit:

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

Which runs the database query in check_code.php:

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

$resultcode = mysql_query('select * from wp_scloyalty where code = "'. $code .'"' ) or die(mysql_error());  
if(mysql_num_rows($resultcode)>0){
    //Exists  
    echo 0;  
}else{  
    //Doesnt Exist
    echo 1;  
}  

What I have so far works in that I get a message saying whether the entered code exists in the database.

My question is how would I go about doing more with the query. For example having more "if statements" to test more columns in the database? I want to say "if the code is already in the database, do something different if the 'redeemed' column is set to 1 (default 0)"?

Currently I can only output things based on the function(result) from the ajax reply, but is there a more efficient way to do what Ive done? Or a more efficient way to make the current set up more flexible?

A bit of a long question, sorry, but I feel I need a few things cleared up,

Thanks guys :)

5
  • Do you want your PHP code to do more based on the DB result, or the Javascript code to do more? Commented Feb 16, 2012 at 19:08
  • Well thats what I am unsure about... I have been lookign around for other peoples approaches on using ajax and I have just decided to use 1 php file to run if statements to return a number, and throw an error or something based on that number. Is this the best/most common approach to work with data from a query using ajax? Commented Feb 16, 2012 at 19:21
  • I guess it depends what you want your app to do. What you have is a perfectly reasonable attempt at seeing if a code exists. An extension of the PHP could output the column values if it finds the code in the database, for instance. Commented Feb 16, 2012 at 19:28
  • For one, you can use this wonderful form plugin. This will autmatically post your form without you having to pick out individual fields. Commented Feb 16, 2012 at 19:36
  • @PaulGrime yeah thanks for that, ill try it out. @ juzerali, I heard about that jQuery plugin but I wanted to learn more about ajax with jQuery from a lower level so using this simple example is a good start me thinks. Thanks though, ill be using it int the future Commented Feb 16, 2012 at 21:41

1 Answer 1

1

It seems to me what you are really wanting is a way to send the data back to the client in a way javascript can interpret. For that I would recommend sending JSON back to the client using json_encode in PHP.

I would follow these links if you want to learn how to do ajax properly. You are almost there.

http://www.w3schools.com/json/default.asp Learn JSON

Use json_encode on the server side.

http://php.net/manual/en/function.json-encode.php

Sign up to request clarification or add additional context in comments.

1 Comment

Brilliant mate, thanks. After trying something out its nice to be pointed int eh right direction with something like this :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.