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.
我有一个锚标记,我想用它去到一个特定的页面,但同时我想要使用onclick函数来插入一个数据库。
here's what i got so far: html:
这是我到目前为止得到的: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:
下面是php文件:
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.
编辑-我已经修正了php文件中所有的错误,感谢所有人,当访问php页面时,只需点击url就可以正确地插入。
BUT it still does not insert using the ajax. clearly there is something i have done wrong with the code.
但是它仍然不使用ajax进行插入。很明显,我在代码上做错了什么。
any ideas?
什么好主意吗?
thanks
谢谢
edit2 solved-
edit2解决-
for anyone thats interested, the problem with the ajax code was this line:
对于任何感兴趣的人来说,ajax代码的问题是:
xmlhttp.open("GET","submit.php?click="+str,true);
it needed to be like this
它需要像这样
xmlhttp.open("GET","/submit.php?click="+str,true);
5 个解决方案
#1
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:
我发现的第一个错误是:在php代码中需要$_GET['link']),但只在JS中发送一个单击参数。所以你应该改变你的JS代码:
xmlhttp.open("GET","submit.php?link="+str,true);
try to run the php script without ajax to eventually find more errors.
尝试在不使用ajax的情况下运行php脚本,最终找到更多错误。
#2
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.
是的,但是请确保对php脚本的调用确实有效,并且脚本本身没有错误,然后查看数据库。
#3
1
your using mysql_fetch_array($result2); for update use mysql_query($result2)
你使用mysql_fetch_array($ result2);更新使用mysql_query(result2美元)
and its nor $_GET['link'] its $_GET['click']
它的nor $_GET['link']它的$_GET['click']
#4
0
i suggest use jquery & .post() method. Syntax of this will be more readable by humans :)
我建议使用jquery & .post()方法。这样的语法将更容易被人类理解:)
// 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
在answer.php
<?
header( "Content-Type: application/json" );
$arr_json = array();
$arr_json[ "success" ] = "true";
echo json_encode( $arr_json );
exit;
?>
#5
0
Seems you forgot the ;
mark here: $row = mysql_fetch_array( $result )
好像你忘了;这里标记:$row = mysql_fetch_array($result)
#1
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:
我发现的第一个错误是:在php代码中需要$_GET['link']),但只在JS中发送一个单击参数。所以你应该改变你的JS代码:
xmlhttp.open("GET","submit.php?link="+str,true);
try to run the php script without ajax to eventually find more errors.
尝试在不使用ajax的情况下运行php脚本,最终找到更多错误。
#2
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.
是的,但是请确保对php脚本的调用确实有效,并且脚本本身没有错误,然后查看数据库。
#3
1
your using mysql_fetch_array($result2); for update use mysql_query($result2)
你使用mysql_fetch_array($ result2);更新使用mysql_query(result2美元)
and its nor $_GET['link'] its $_GET['click']
它的nor $_GET['link']它的$_GET['click']
#4
0
i suggest use jquery & .post() method. Syntax of this will be more readable by humans :)
我建议使用jquery & .post()方法。这样的语法将更容易被人类理解:)
// 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
在answer.php
<?
header( "Content-Type: application/json" );
$arr_json = array();
$arr_json[ "success" ] = "true";
echo json_encode( $arr_json );
exit;
?>
#5
0
Seems you forgot the ;
mark here: $row = mysql_fetch_array( $result )
好像你忘了;这里标记:$row = mysql_fetch_array($result)