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