...

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



tomyknoker
04-07-2007, 12:10 PM
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?

david_kw
04-16-2007, 06:36 PM
To answer how complicated it is depends what you already know how to do. There are many different technologies included in that request. Do you already know how to use a server-side language like PHP or Java with MySQL?

david_kw

tomyknoker
04-17-2007, 03:00 AM
To answer how complicated it is depends what you already know how to do. There are many different technologies included in that request. Do you already know how to use a server-side language like PHP or Java with MySQL?

david_kwWell I have can handle the php/mysql side, I can check if the code is there and then once they have entered it remove it from the database... I just want to set up some AJAX so that initially when they type it in they get instant feedback...

david_kw
04-17-2007, 06:40 AM
Ok, the problem isn't too bad then. It's even easier if you are using some javascript library like Prototype, but I'm going to assume you aren't. Let me see if I can put together an example with stubs for the mysql part.

david_kw

david_kw
04-17-2007, 07:27 AM
Ok here is a link to the example

http://www.exfer.net/test/ajax_example.html

The html/javascript code is here.

ajax_example.html


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Ajax Example</title>
<script type="text/javascript">
/* <![CDATA[ */
/**** AJAX code by david_kw based mainly on the work of Nicholas Zakas */
exfer = {};
exfer.ajax = {};
exfer.ajax.Http = function (method, url, params, onsuccess) {
var o = null;

method = method.toLowerCase();

if (typeof XMLHttpRequest != "undefined") {
o = new XMLHttpRequest();
} else if (typeof ActiveXObject != "undefined") {
if (exfer.ajax.Http.activexIndex < 0) {
for (var i = 0; i < exfer.ajax.Http.activexVers.length; i++) {
try {
o = new ActiveXObject(exfer.ajax.Http.activexVers[i]);
exfer.ajax.Http.activexIndex = i;
} catch (e) {
o = null;
}
}
} else {
o = new ActiveXObject(exfer.ajax.Http.activexVers[exfer.ajax.Http.activexIndex]);
}
}

if (!o) {
throw new Error("Error creating Ajax HTTP object");
}

if (method == "get") {
o.open(method, url + "?" + params, true);
} else if (method == "post") {
o.open(method, url, true);
} else {
throw new Error ("Method not supported by Ajax object");
}

o.onreadystatechange = function () {
if (o.readyState == 4) {
if (o.status == 200) {
onsuccess(o.responseText);
} else {
alert("Error: " + o.statusText);
}
}
}

if (method == "get") {
o.send(null);
} else {
o.send(params);
}

this.method = method;
this.url = url;
this.params = params;
this.request = o;
};
exfer.ajax.Http.activexIndex = -1;
exfer.ajax.Http.activexVers = [ "MSXML2.XmlHttp.6.0", "MSXML2.XmlHttp.3.0" ];

function showIt(s) {
var adiv = document.getElementById("adiv");
adiv.innerHTML = s;
}

function checkIt(v) {
var xhr = new exfer.ajax.Http("get", "ajax_example.php", "value="+encodeURIComponent(v),
function (responseText) {
if (responseText == "true") {
showIt("This value is validated");
} else if (responseText == "false") {
showIt("This value is incorrect");
} else {
alert("Incorrect Ajax return value");
}
});
}
/* ]]> */
</script>
</head>
<body>
<div>
<p>A correct input is "pizzapie"</p>
<input type="text" id="t" />
<button onclick="checkIt(document.getElementById('t').value);">Check it</button>
<br />
<div id="adiv"> </div>
</div>
</body>
</html>


and the php (which is stubbed to be simple) is here

ajax_example.php


<?php
function check_value_in_database($s) {
return ($s == "pizzapie");
}

header("Content-Type: text/plain");

if (!isset($_GET["value"])) {
echo "invalid";
} elseif (check_value_in_database($_GET["value"])) {
echo "true";
} else {
echo "false";
}
?>


One thing to note is that the exfer.ajax.Http() function has been modified for this example which means it has had minimal testing (and I do mean minimal). It should be enough to help give you direction though.

Good luck! And let me know if you have any questions. :)

david_kw

tomyknoker
04-17-2007, 04:34 PM
david thanks alot looks great! i'll give it a go and see how far i can get :)

tomyknoker
04-17-2007, 09:16 PM
Ok a tiny bit confused as too what to change, I'm assuming I just update the php page first to test?

Currently I have this on one of my pages... But don't know from this what I should add to ajax_example.php...


if ($_SESSION['txtboxEmpCode'] != "") {
$code = $_SESSION['txtboxEmpCode'];

$apCode = mysql_query("SELECT * FROM `tblcodes` WHERE `Code`='$code'");
if (mysql_num_rows($apCode) == 1) {
$Member = 'A';

david_kw
04-17-2007, 09:29 PM
I guess I'm not sure exact what I'm looking at. I was thinking you'd just have to change the function check_value_in_database() to see if the code is valid. Based on your last post, I'd probably do something like the code below assuming the database is already connected.



<?php

function check_value_in_database($code) {
$apCode = mysql_query("SELECT * FROM `tblcodes` WHERE `Code`='$code'");
return (mysql_num_rows($apCode) >= 1);
}

header("Content-Type: text/plain");

if (!isset($_GET["value"])) {
echo "invalid";
} elseif (check_value_in_database($_GET["value"])) {
echo "true";
} else {
echo "false";
}

?>


I'm not sure I understand what is stored in $_SESSION['txtboxEmpCode'] that you are checking. My example code creates and expects the URL to look something like this

http://localhost/ajax_example.php?value=Coupon53423

david_kw

baludealsguy
08-04-2009, 02:44 PM
:thumbsup:Calfinder.com (http://deals365.us/index.php) : Use Coupons, Promo Codes, Coupon Codes, money off Coupon Codes, Special Discount offers and Free Shipping Coupons, Promotional Codes, online Coupons, Voucher Codes, Deals, Promotions, - Coupon Codes, Bargains, great discounts and Clearance sale, Rebates, Promo Code, Discounts,free shipping codes, Promotional Coupons, Online Coupon Codes, Vouchers, Discount Code, Rebate Coupon Codes, Special Discount Coupon Offers and more at Deals365.us (http://deals365.us/index.php):thumbsup:

franklin33
07-18-2014, 01:47 PM
Couponcodes365.us (http://couponcodes365.us) is the best online coupon store for USA retailers for Best E- cigars coupons and promo codes are available at mrqe.us (http://mrqe.us)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum