- Affiliates Page
- Number of affiliates
- Request Affiliate in AJAX
- Admin: Add affiliate
- Admin: Delete Affiliate
- Admin: Accept Affiliate
We need four pages set up for this to work first, so in notepad or dreamweaver or something similar, make the following pages.
- affiliates.php
- affrequest.php
- affrequest.js
- config.php
CREATE TABLE `affiliates` (
`id` INT(11) NOT NULL auto_increment PRIMARY KEY,
`sitename` VARCHAR(30) NOT NULL,
`siteurl` VARCHAR(200) NOT NULL,
`imgurl` VARCHAR(200) NOT NULL,
`email` VARCHAR(70) NOT NULL,
`approved` VARCHAR(3) NOT NULL default'0'
);
Now onto the pages... Read through the code, they include comments, comments are marked with a // infront of it.
affiliates.php
<html><head><title>Silver Affiliates v0.5</title><script src="affrequest.js" type="text/javascript"></script></head><body><? //Above is HTML, to get the title of the page and insert our javascript or in this case, AJAX. include('config.php'); //Above we include our config.php page, our database information. $selectaff = mysql_query("SELECT * FROM `affiliates` WHERE `approved` = '1' ORDER BY `id` ASC") or die(mysql_error()); //Above we select everything from the table affiliates where they are approved, our approved will be sorted with 0 for no and 1 for yes. $affnum = mysql_num_rows($selectaff); //How many rows have we selected? This is our number of affiliates. echo("<center>We have <strong>".$affnum."</strong> affiliates.</center>"); //We show how many affiliates we have. while($aff = mysql_fetch_object($selectaff)){ //We then use a while loop to get the rows from the table, we are using mysql_fetch_object for this. echo("<a href=\"".$aff->siteurl."\" title=\"".$aff->sitename."\" target=\"_blank\"><img src=\"".$aff->imgurl."\" width=\"88\" height=\"31\" alt=\"".$aff->sitename."\" /></a> "); //We make a link and show the 88x31 image, we have a space at the end so we have a space between our affiliates. } //Below is the form for our request affiliates. As you can see, there is an empty div at the top with the ID are, this is our AJAX response area. //The form is built using divs to style it so it looks neat, you can do this how you want, when using HTML in an echo, we use an escaping character for the " in elements, as you can see we use style=\". echo("<br /><br /> <div id=\"are\" style=\"background: #F4F5F7; border-bottom: 1px solid #A4C0D5;\"></div> Request Affiliation<br /> Please fill in all of the forms below to request for an affiliation.<br /> <div style=\"display: inline; float: left; width: 130px; font-weight: bold;\">Site Name</div> <div style=\"display: inline; float: left;\"><input type=\"text\" size=\"30\" id=\"affname\"></div> <div style=\"clear: both\"><!-- --></div><br /> <div style=\"display: inline; float: left; width: 130px; font-weight: bold;\">Site URL</div> <div style=\"display: inline; float: left;\"><input type=\"text\" size=\"30\" id=\"affurl\"></div> <div style=\"clear: both\"><!-- --></div><br /> <div style=\"display: inline; float: left; width: 130px; font-weight: bold;\">Banner URL</div> <div style=\"display: inline; float: left;\"><input type=\"text\" size=\"30\" id=\"affimg\"></div> <div style=\"clear: both\"><!-- --></div><br /> <div style=\"display: inline; float: left; width: 130px; font-weight: bold;\">Your Email</div> <div style=\"display: inline; float: left;\"><input type=\"text\" size=\"30\" id=\"affemail\"></div> <div style=\"clear: both\"><!-- --></div><br /> <div style=\"display: inline; float: left; width: 130px; font-weight: bold;\"> </div> <div style=\"display: inline; float: left;\"><input type=\"submit\" value=\"Submit\" onclick=\"java script:requestaff();\"></div> <div style=\"clear: both\"><!-- --></div> "); //You may have noticed that we haven't included a <form> tag, this is because AJAX doesn't need it, and we don't use name= in our input fields either, this is because AJAX also doesn't need it, instead, we have to use id=. //Also on the submit button, we have an onclick, this calls our javascript function to start our AJAX.?></body></html>
config.php
function makeInstance() {var http_request = false;//This fuctions will try an XML request which is for mozilla, safari, etc. If it can't do it then an activeX request which is for IE, this let's AJAX work.if (window.XMLHttpRequest) { // Mozilla, Safari,...http_request = new XMLHttpRequest();} else if (window.ActiveXObject) { // IEtry {http_request = new ActiveXObject("Msxml2.XMLHTTP");} catch (e) {try {http_request = new ActiveXObject("Microsoft.XMLHTTP");} catch (e) {}}}if (!http_request) {alert('Giving up <img src='http://www.pixel2life.com/forums/public/style_emoticons/<#EMO_DIR#>/sad.gif' class='bbc_emoticon' alt=':)' /> Cannot create an XMLHTTP instance');return false;}//If we can't make it, then we say we can't and stop.return http_request;}ajax = makeInstance();function requestaff() {var affname = document.getElementById("affname").value;var affurl = document.getElementById("affurl").value;var affimg = document.getElementById("affimg").value;var affemail = document.getElementById("affemail").value;var affcode = document.getElementById("affcode").value;var affcodeh = document.getElementById("affcodeh").value;//Above we make all the fields into javascript variables to use later.ajax.onreadystatechange = handleResponse; //Is the page ready?ajax.open('POST', 'http://www.domain.com/affrequest.php', true);//Above we open up our page, which needs to be a full url in POST method.ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');//Set header type as a form.ajax.send('affname=' + affname + '&affurl=' + affurl + '&affimg=' + affimg + '&addemail=' + affemail + '&affcode=' + affcode + '&affcodeh=' + affcodeh);//Above we send the variables in the url as post method.}function handleResponse() {if(ajax.readyState < 4) {document.getElementById("are").innerHTML = "Transferring Data...";//Is the page ready? If not, put in transferring data on the page.}if(ajax.readyState == 4) {if(ajax.status == 200) {document.getElementById("are").innerHTML = ajax.responseText;} else {document.getElementById("are").innerHTML = ajax.status;}}}
affrequest.php
Well that concludes part 1, you can actually have your system like this, since you can control everything in the database center such as PHPMyAdmin.