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);
-
Does your request reach the server? Does the server log any error in error_log? Have you tried debuging with any tools?DKSan– DKSan2011年06月07日 12:39:48 +00:00Commented Jun 7, 2011 at 12:39
5 Answers 5
It's going to google as it should, but it isn't inserting into the database.
Yes, but make sure that the call to the php script actually works and that the script itself is free of errors, then look at the database.
Comments
your using mysql_fetch_array($result2); for update use mysql_query($result2)
and its nor $_GET['link'] its $_GET['click']
1 Comment
the first error I found is:
you want $_GET['link']) in your php code, but only send a click parameter in JS. So you should change your JS code:
xmlhttp.open("GET","submit.php?link="+str,true);
try to run the php script without ajax to eventually find more errors.
Comments
i suggest use jquery & .post() method. Syntax of this will be more readable by humans :)
// submit here ...
$.post(
"answer.php",
{
start: $("#ancor_id" ) . attr( "title" )
},
function(ret) {
if (!ret.success )
{
alert(1);
}
else
{
alert( "Update Ok" );
}
},
'json'
);
where answer.php
<?
header( "Content-Type: application/json" );
$arr_json = array();
$arr_json[ "success" ] = "true";
echo json_encode( $arr_json );
exit;
?>
1 Comment
Seems you forgot the ; mark here: $row = mysql_fetch_array( $result )