Wichapong
Wichapong

Reputation: 3

Need help in HTML submit button and javascript(confirm-button) on django:

Scenario: I'm trying to use image button in my HTML form with JavaScript:

<form method="post" name="delete">
<input type="image" name="{{a.id}}"
    src="http://localhost:8000/site_media/web/img/delete_icon.gif"
    onClick="decision(this,'Delete this Activity?','http://localhost:8000/delAct/listAct')"/>
</form>

this is my script:

<SCRIPT LANGUAGE="Javascript">
<!---
function decision(selectobject, message, url){
   if(confirm(message)) location.href = url+"/"+selectobject.name ;
}
// --->
</SCRIPT>

The problem is it's not redirecting to page delAct/listAct like it is supposed to do. So I tried to change it to use type="button" and it's working fine:

<form method="post" name="delete">
<input type="button" name="{{a.id}}"
   onClick="decision(this,'Delete this Activity?','http://localhost:8000/delAct/listAct')"/>
</form>

My question is how to get the first script to work using type="image"? Is there any way to do that?

As an aside: I've try both get and post methods in the form already and it makes no difference. Also, I'm using Python 2.6 and Django in my project.

Upvotes: 0

Views: 811

Answers (1)

CarlosZ
CarlosZ

Reputation: 8669

The problem you are having is that using an input of type image creates a submit button that looks like an image so when it is clicked the form is submitted to the same page you are on because the <form> tag doesn't specify an action attribute. To prevent the submit from trigering you need to return false from the decision event handler which will prevent the event from bubbling up.

Upvotes: 1

Related Questions