2013-08-23 57 views
0

如何獲得一個href的值並將其張貼在mysql中?舉例來說,我有這樣的:如何獲取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; 
} 

回答

0

在JavaScript變量,你可能具有價值的測試/ Test.doc的。要將此值寫入數據庫,您可以對php文件執行異步ajax-post並提交href值。

// 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

+0

這是否意味着沒有從我的test.php的變化?我會試試這個。謝啦! (^ - ^)/ – Julietta

相關問題