2

HTML code-

<form> .... <a class="art-button" href="edit.php" >Edit</a> <a class="art-button" href="#" onclick="show_confirm()">Delete</a> </form> 

JavaScript-

<script type="text/javascript"> function show_confirm() { var r=confirm("Do you want to delete?"); if (r==true) { // call "delete.php?id=value" } else { //go back to same page } } 

how can I call delete.php?id=value from javascript`

3
  • 3
    why is there form tag with anhor tags only? Commented Aug 20, 2010 at 7:43
  • Updated my answer in reply to your comment Commented Aug 20, 2010 at 7:49
  • Not separating content and logic is so 1990's (you shouldn't use JavaScript as HTML attributes). Commented Aug 20, 2010 at 8:50

7 Answers 7

2

Not sure what you are trying to do but the code should be like:

<a class="art-button" href="delete.php?id=value" onclick="return confirm('Are you sure you want to delete this?')">Delete</a> 

no need to submit the form.

Sign up to request clarification or add additional context in comments.

Comments

1

Use .submit() as HS suggests.

Alternatively, if you want to make this work even when the user has JavaScript turned off (that's not necessarily the case - your decision) make the two links submit buttons:

<button type="submit" value="Delete"> 

and add on_confirm() to the form's submit event:

<form onsubmit="return on_confirm()"> 

and make the function return false or true depending on the outcome of the confirm():

 var r=confirm("Do you want to delete?"); return r; 

2 Comments

but when user clicks on edit then?? because both form actions are different can I set the form action in javascript also?
@amanda "Edit" is not a form action, is it? It's just a link, isn't it?
0

In your javascript:

(document.forms['formname'] || document.formname).submit(); 

1 Comment

but when user clicks on edit then?? because both form actions are different can I set the form action in javascript also?
0

Give the and id="formId" (<form id="formId">)

document.getElementById("formId").submit(); 

For this to work the form needs the url it should submit to.

<form id="myform" method="POST" action="delete.php?id=value"> 

Edit

If you just want a confirm button i'd use pekkas answer (+1)

Edit in reply to question

You could to:

form = document.getElementById("formId") form.action = "edit.php?id=value"; form.submit(); 

if you want it to submit to different pages.

1 Comment

but when user clicks on edit then?? because both form actions are different can I set the form action in javascript also?
0
if (r==true) { $.ajax({ type: "POST", url: "delete.php", cache : false, data: $("#id_of_form").serialize(), // here will be the values from form success: function(msg){ } }); 

and in delete.php you will receive values in $_POST array

Comments

0

With jQuery:

<form id="randomForm"> .... <a class="art-button" href="edit.php" >Edit</a> <a class="art-button" id="deleteLink" href="#" onclick="show_confirm()">Delete</a> </form> <script type="text/javascript"> $("#deleteLink").click(function(){ $("randomForm").submit() }); </script> 

Please note the extra ID tags I have added

Comments

0

window.location.href = "delete.php?id=" + value;

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.