1

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);
Sergey K.
25.5k14 gold badges112 silver badges176 bronze badges
asked Jun 7, 2011 at 12:29
1
  • Does your request reach the server? Does the server log any error in error_log? Have you tried debuging with any tools? Commented Jun 7, 2011 at 12:39

5 Answers 5

2

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.

answered Jun 7, 2011 at 12:39
Sign up to request clarification or add additional context in comments.

Comments

1

your using mysql_fetch_array($result2); for update use mysql_query($result2)

and its nor $_GET['link'] its $_GET['click']

answered Jun 7, 2011 at 12:44

1 Comment

he is actually updating with mysql_query(). your code would raise an error.
0

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.

answered Jun 7, 2011 at 12:43

Comments

0

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;
?>
answered Jun 7, 2011 at 13:22

1 Comment

can you elaborate on this please?
0

Seems you forgot the ; mark here: $row = mysql_fetch_array( $result )

answered Jun 7, 2011 at 12:34

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.