I'm setting up a discount code database for a client. If a customer enters a valid sale code, this AJAX script should look up the code in the database and verify if it is 1) valid and 2) not expired. I do this by checking a variable $discountcode in the script, against the database of valid codes.
I'm new to AJAX, and I'm modifying a script I borrowed. (This is supposed to be a learning exercise for me.) The original script used a <select> to submit the information to the database. I'm wanting to use a text field and a submit button instead. What's happening is that the parameter is not being passed to my PHP script, which checks the database and returns a response message to the visitor. I'm sure it's in the way I'm submitting the form data--I've tried using onchange, onsubmit and others in various form elements below and I'm still not getting that parameter to the script. Here is the last frustrating attempt:
<form onsubmit="showCustomer(this.value);return false;">
<input type="text" name="discountcode" id="discountcode" >
<input type="submit" name="button" value="Validate">
</form>
The Javascript that goes with it:
var xmlHttp
function showCustomer(str)
{
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
{
alert ("Your browser does not support AJAX!");
return;
}
var url="getdiscountcode.php";
url=url+"?q="+str;
url=url+"&sid="+Math.random();
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET",url,true);
// xmlHttp.onreadystatechange = handleResponse;
xmlHttp.send(null);
}
function stateChanged()
{
if (xmlHttp.readyState==4)
{
document.getElementById("txtHint").innerHTML=xmlHttp.responseText;
}
}
function GetXmlHttpObject()
{
var xmlHttp=null;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}
Finally, the "meat" of my PHP script that is looking for my $discountcode variable (and this is just a rough "test" version for now as it's just on my local machine):
$lookupcode = $discountcode;
if ($lookupcode):
$connected = mysql_connect("localhost", "root", "password");
mysql_select_db("test");
$discountcode_table = "catalog_discountcode";
$query = "SELECT discount_id FROM $discountcode_table WHERE discount_code = '$lookupcode' ";
$result = mysql_query($query);
if ($result):
$resultcount = mysql_num_rows($result);
if ($resultcount > 0):
$mssg = "Code validated! Click the button below to apply to your order.<br><br><button>Apply Discount</button>";
else:
$mssg = "Expired or invalid code. Please check and re-enter.";
endif;
else:
$mssg = "Database error. " . mysql_error();
endif;
else:
$mssg = "<span style=\"color: #FF0000;\">The discountcode variable is empty.<br><br>discountcode={$discountcode}, q={$q}</span>";
endif;
print $mssg;
I figure it's a case of either the form data not getting submitted to the PHP script, or something totally stupid I did in my PHP that I've been staring at for hours and I'm not seeing my simple, stupid error. 😉 I get my own error message saying "The discountcode variable is empty" when I try submitting this form, so at least I know I'm reaching the PHP script OK.
TIA if you can help!