0

How to get values of a href and post it in mysql? For instance, I have this:

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

How can i get the "test/test.doc" via php or javascript and put it in mysql table?

Many thanks.

below is my script:

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

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

});
});
</script>

My html:

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

Here's my 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;   
}
Chris
  • 8,268
  • 3
  • 33
  • 46
Julietta
  • 23
  • 9

1 Answers1

0

In the javascript variable you might have the value test/test.doc. To write this value into a db you can do an asynchronous ajax-post to a php file and submit your 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
    );
});

copied from: jQuery Ajax POST example with PHP

Community
  • 1
  • 1
zuluk
  • 1,557
  • 8
  • 29
  • 49