AJAX, PHP, MySql subscription system?

Ali

New member
I'm trying to make a subscription system that people can subscribe to a band and the news about the band would be send to their email. I'm using PHP, MySql and AJAX method so when they subscribe, the writing "Subscribe to 'band'" would change to "Unsubscribe from 'band'", but there is a problem with the ajax. I already tried the php code, separately, and it works but when I mix it with AJAX it doesn't. here is the AJAX and PHP/HTML code:
<script type="text/javascript">
function subscribe(band)
{
var sub = "subscribe" + band;
if (window.XMLHttpRequest)
{
xmlhttp = new XMLHttpRequest();
}
else
{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET", "subscribe.php?band="+band, true);
xmlhttp.send(null);
xmlhttp.onreadystatechange = function()
{
if (xmlhttp.readystate == 4)
{
document.getElementById(sub).innerHTML = xmlhttp.responseText;
}
}
}
</script>
////////////////////////////////////////////////////////////
<?php
$news_tbl_name = "News";
//$date = time();
$date = date("Y/m/d", time());
mysql_select_db($db_name, $con) or die(mysql_error());
$sql = "SELECT * FROM $news_tbl_name ORDER BY date DESC";
$result = mysql_query($sql) or die (mysql_error());
$rowcount = mysql_num_rows($result);
echo '<table>';
while($row = mysql_fetch_assoc($result))
{
$band = $row['keyone'];
echo '
<tr>
<td><h2>' . $row['title'] . '</h2></td>
</tr>
<tr>
<td style="width:350px;"><p>' . $row['news'] . '</p></td>
</tr>
<tr>
<td>
<!--<a href="subscribe.php">--><div style="color:rgb(153,217,234); float:right; margin-right: 5px; cursor:pointer; display:block;" onclick="subscribe(' . $band . ')">
<span style="float:left;" id="subscribe' . $band . '">Subscribe to * </span><b>' . $band . '</b></div><!--</a>-->';
\ $i++;
if ($i == 10)
{
echo '<tr>
<td><a href="readnews.php" target="_self"><div style="padding:5px; float:right; color:white; display:block;">...</div></a></td>
</tr>';
break;
}
}
echo '</table>';
?>
/////////////////////////////////////////////////////

And here is the subscribe.php:

<?php
//subscribe.php
session_start();
include ('connect.php');
$email = $_SESSION['user'];

$_SESSION['user'] = $email;
$band = $_GET["band"];


mysql_select_db($db_name, $con);
$sql = "SELECT * FROM $band";
$result = mysql_query($sql);
if (!$result)
{
// The table does exist.
$sql = "CREATE TABLE $band (
`id` INT NOT NULL ,
`subscribers` VARCHAR( 64 ) NOT NULL ,
PRIMARY KEY ( `id` )";
mysql_query($sql) or die (mysql_error());
$sql = "INSERT INTO $band (subscribers) VALUES ('$email')";
mysql_query($sql) or die (mysql_error());
}
else if ($result)
{
//If the band's table exist, check and see if the user hasbeen subscribed, if so returne subscribed, if not, subscribe the user.
$sql = "SELECT subscribers FROM $band";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);

if ($email == $row['subscribers'])
{
// If email address exist, unsubscribe the user.
$sql = "DELETE FROM $band WHERE subscribers = '$email'";
mysql_query($sql) or die (mysql_error());
echo 'Subscribe to';
}
else
{
// If the email does not exist, insert the user.
$sql = "INSERT INTO $band (subscribers) VALUES ('$email')";
mysql_query($sql) or die (mysql_error());
echo 'Unsubscribe from' . $band;
}
}
?>
 
Back
Top