Reputation: 57
I am trying to update a database from an onClick procedure inside an anchor.
<a name=“fs1” onClick="updateArea1();"></a>
I am struggling with the function for updateArea1 as I'm not really sure how I can pass the name "fs1" from the anchor and update the database from a script process.
I would essentially like the database to be updated like this: UPDATE row WHERE id = 1 WITH (name_from_anchor)
However... the variable does not HAVE to come from the name tag. It could be referenced in the parenthesis of the function like this if it's easier.
<a onClick="updateArea1(fs1);"></a>
Any help would be greatly appreciated.
-- UPDATED --
LAUNCH.PHP
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<meta name="apple-mobile-web-app-capable" content="yes" />
<script src="includes/scripts/jquery-2.0.3.min.js"></script>
<script>
function updateArea1(el) {
$.post("includes/db_update.php", $("#console").serialize());
}
</script>
</head>
<body>
<a name="fs1" onClick="updateArea1(this.name);"></a>
<a name="fs2" onClick="updateArea1(this.name);"></a>
</body>
</html>
DB_UPDATE.PHP
<?php
include 'db_connect.php';
$area1= mysqli_escape_String($con,$_POST[]);
$query = "UPDATE previewState SET selected='".$area1."' WHERE id='1';";
mysqli_query($con,$query);
mysqli_close($con);
?>
Upvotes: 1
Views: 16131
Reputation: 15626
It's pretty simple. Here's the demo
<a name="temp" onClick="updateArea1(this.name);">label</a>
function updateArea1(el){
alert( el)
}
Upvotes: 2
Reputation: 171679
If you pass this
as argument can do:
<a name="fs1" onClick="updateArea1(this);"></a>
updateArea1(el){
alert( el.name)
}
Upvotes: 3
Reputation: 2072
When you assign a function to onclick event, you can access the element that triggered the event using this (If the function is global or in window scope).
So just use this.name
within updateArea1 function.
function updateAreaa1(){
updateDb(this.name); //this will refer to the element that triggered the click event.
}
Hope this helps.
Upvotes: 1
Reputation: 67207
Since you have tagged this question with Jquery, why dont you use a click handler for that element like,
$('a').click(function(e){
e.preventDefault();
updateAreat1($(this).attr('name'));
})
Upvotes: 2