如何获取href的值并将其发布在mysql中?

时间:2013-08-23 10:15:05

标签: mysql href

如何获取href的值并将其发布到mysql中?例如,我有这个:

<a href="test/test.doc" name="test">testing</a>

如何通过php或javascript获取"test/test.doc"并将其放入mysql表中?

非常感谢。

下面是我的剧本:

<script language="javascript" type="text/javascript">
$(document).ready(function()
{
$('.linktest').click(function(){    

var linkhref=$('a',this).attr('href');
$.post("testing.php", { href: linkhref } ); 

});
});
</script>

我的HTML:

<p><a href="test/test.doc" class="linktest" name="test">test  insert</a></p>

这是我的test.php:

if(empty($_POST['name']) || empty($_POST['href']) )
{
    $sql="INSERT INTO increment (name, href, count) VALUES ('$name','$href','$count' )";
    $result=mysql_query($sql,$db);
    $row=mysql_fetch_array($result);
    $count=$row['count'];
    $count+=1;   
}

1 个答案:

答案 0 :(得分:0)

在javascript变量中,您可能具有值test / test.doc。要将此值写入db,您可以对php文件执行异步ajax-post并提交href-value。

// fire off the request to /test.php
request = $.ajax({
    url: "/test.php",
    type: "post",
    data: serializedData //your href-string
});

// callback handler that will be called on success
request.done(function (response, textStatus, jqXHR){
    // log a message to the console
    console.log("Hooray, it worked!");
});

// callback handler that will be called on failure
request.fail(function (jqXHR, textStatus, errorThrown){
    // log the error to the console
    console.error(
        "The following error occured: "+
        textStatus, errorThrown
    );
});

复制自:jQuery Ajax POST example with PHP