View Full Version : Ajax: Coupon Code Enter Get Instant Result

04-07-2007, 10:10 AM
I've set up a table in my MySQL database which has coupon codes in it. When someone signs up on my website they enter a coupon code, I want to use Ajax to give them an answer straight away if there coupon is valid or not... Is this fairly complicated? Does anyone have an example of something similar to this?

04-07-2007, 10:31 AM
//mysql connection
$code = $_POST["coupon"];
$query = "SELECT * FROM mytable";
$result = mysql_query($query);
$found = false;
for ($i = 0;$i < mysql_num_rows($result);$i++) {
$coupon = mysql_result($result,$i,"mycolumn");
if ($code == $coupon) {
$found = true;
if ($found == true) {
echo "The code ".$code." was found.";

Hopefully you'll know what to edit, but that's basically how you do a mysql search.
There is other ways, using mysql_fetch_assoc, but that can be a little more complicated.

//EDIT: Sorry, I forgot you wanted AJAX. I missed it, but that's a PHP script

04-07-2007, 11:33 AM
The AJAX part is fairly simple once you get the PHP part working.