0

href の値を取得して mysql に投稿する方法は? たとえば、私はこれを持っています:

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

どうすれば"test/test.doc"phpまたはjavascript経由で取得して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;   
}
4

1 に答える 1

0

javascript 変数には、test/test.doc という値が含まれている場合があります。この値を db に書き込むには、非同期 ajax-post を php ファイルに実行し、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
    );
});

コピー元: PHP を使用した jQuery Ajax POST の例

于 2013-08-23T10:31:47.493 に答える