1

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);

?>
7
  • 1
    show your updateArea1() code Commented Dec 29, 2013 at 16:46
  • What exactly is your question? Where are you struggling? Commented Dec 29, 2013 at 16:49
  • Using names to refer elements is deprecated. Use ids Commented Dec 29, 2013 at 16:49
  • @aliasm2k since when is that deprecated? Commented Dec 29, 2013 at 16:54
  • Since when id's got introduced Commented Dec 29, 2013 at 16:56

4 Answers 4

3

If you pass this as argument can do:

<a name="fs1" onClick="updateArea1(this);"></a>

updateArea1(el){
  alert( el.name)
}
Sign up to request clarification or add additional context in comments.

4 Comments

thanks @putvande - Can you also please tell me how I would complete the function to update the db? Basically the part that does this (UPDATE row WHERE id = 1 WITH name)
you would need to send name to server with ajax
thanks @charlietfl - I updated my code above. I am referencing another PHP file to update the DB but I'm not sure I'm going about it correctly.
no...you have no form to serialize, want to do something like:$.post(url,{name: el.name}) then receive in php $_POST['name']
2

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'));
})

Comments

2

It's pretty simple. Here's the demo

 <a name="temp"       onClick="updateArea1(this.name);">label</a>


 function updateArea1(el){
   alert( el)
 }

1 Comment

thanks @srvikram13 - Can you also tell me how I would complete the function to update the db? Basically the part that does this (UPDATE row WHERE id = 1 WITH name)
1

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.

1 Comment

Can you explain If the function is global or in window scope?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.