I have an anchor tag which i want to use to go to a certain page but at the same time i want to use the onclick function to insert into a databse.
here's what i got so far: html:
<script type="text/javascript">
function showUser(str)
{
if (str=="")
{
alert("txtHint");
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","submit.php?click="+str,true);
xmlhttp.send();
}
</script>
<a onclick="showUser('test')" href="http:www.google.com">click here</a>
and here's the php file:
mysql_connect("localhost", "username", "password") or die(mysql_error());
mysql_select_db("db_name") or die(mysql_error());
// Retrieve all the data from the "example" table
$result = mysql_query("
SELECT `clicked` FROM `links`
WHERE open = ".$_GET['link'])
or die(mysql_error());
$row = mysql_fetch_array( $result )
$x = $row['clicked'];
$y = $x++;
$result2 = mysql_query("
UPDATE `db_name`.`links`
SET `clicked` = $y
WHERE `links`.`open` = '".$_GET['link']."'
")
or die(mysql_error());
mysql_fetch_array($result2);
It's going to google as it should, but it isn't inserting into the database.
Any ideas?
thanks in advance,
Reece
edit- i have fixed all the errors in the php file thanks everyone, it now inserts properly when just visiting the php page with a click in the url.
BUT it still does not insert using the ajax. clearly there is something i have done wrong with the code.
any ideas?
thanks
edit2 solved-
for anyone thats interested, the problem with the ajax code was this line:
xmlhttp.open("GET","submit.php?click="+str,true);
it needed to be like this
xmlhttp.open("GET","/submit.php?click="+str,true);