Jump to content

Problem with JS form submition when using image buttons


javaholic5

Recommended Posts

Hi,I was trying have multiple thankyou pages upon submittion of an html form. And these pages will be different depending on the country chosen in the <select> <option></option></select> tagsI can get this to work using the javascript bellow. However, It will only work using the default javascript buttons. <input type="submit" name="Submit" value="Submit" />If I wanted to have images as buttons then it would not work, as the <input type="image" src="#" /> Creates an image control that, when clicked, causes the form to be immediately submitted and ommit my javascript all together.I tried to find a work-around by using a function, but It fails.I also understand that when using SWITCH inside a function, the return statement can be used instead of the break statement as both serve to terminate the SWITCH statement and prevent execution from falling through to the next case.However, when I use return; It seems to take me out of the function all together.I managed to get the script to work by not using a function at all and just doing it the following way, but 'curiosity killed the cat' (I want to know how to do it in a function / using an image button as well) :redirect=document.getElementById('redirect'); switch ( country.options[country.selectedIndex].value ){ case 'UK': redirect.value = 'http://blahBlahBlah.co.uk/newsletters/E0/files/Confirmation_UK.html'; break; case 'IE': case 'BE': case 'AU': case 'SG': case 'HK': case 'ZA': case 'NL': case 'DK': case 'NO': case 'NZ': case 'CA': case 'US': redirect.value = 'http://blahBlahBlah.co.uk/newsletters/E0/files/Confirmation_GSA.html'; break; default: redirect.value = 'http://blahBlahBlah.co.uk/newsletters/E0/files/Confirmation_Others.html'; } return true;}P.S. In the case labels i'm puting in the value for each country. Some assistance would be greatly appreciated.Regards,Javaholic5 :)

Link to comment
Share on other sites

Hi,I was trying have multiple thankyou pages upon submittion of an html form. And these pages will be different depending on the country chosen in the <select> <option></option></select> tagsI can get this to work using the javascript bellow. However, It will only work using the default javascript buttons. <input type="submit" name="Submit" value="Submit" />If I wanted to have images as buttons then it would not work, as the <input type="image" src="#" /> Creates an image control that, when clicked, causes the form to be immediately submitted and ommit my javascript all together....
How about:
<img src="myimage.jpg" onclick="myfunction()" />

The function can include submitting the form:

document.getElementById('myform').submit();

[...] I also understand that when using SWITCH inside a function, the return statement can be used instead of the break statement as both serve to terminate the SWITCH statement and prevent execution from falling through to the next case.However, when I use return; It seems to take me out of the function all together. [...]
Return always returns from a function. You would only use it to terminate a switch statement if you also wanted the function to return at that point. Otherwise stick to break.
Link to comment
Share on other sites

How about:
<img src="myimage.jpg" onclick="myfunction()" />

Yes, I tried the preceeding before but it still didn't work. I even did the following as a workaround:
<a href="java script:document.form.submit()" onClick="return myredirect()"><img src="http://blahblahblah.co.uk/E0/files/MA2568_go.gif" border=0 alt="Submit"></a>

Using image an image link didn't work either. I know there has to be a way of doing it with an image button. :)

The function can include submitting the form:
document.getElementById('myform').submit();

Return always returns from a function. You would only use it to terminate a switch statement if you also wanted the function to return at that point. Otherwise stick to break.

Ok, thanks, I will stick with
break;
Link to comment
Share on other sites

Yes, I tried the preceeding before but it still didn't work....
It should work. Maybe you left off the "()"? That stops it working. Try this and you'll see it working:
<html><head><script type="text/javascript">function myfunction(){  alert('myfunction called!');}</script><body><img src="my.jpg" onclick="myfunction()"></body></html>

If you still have problems please post your code, because it must be something else that's the problem.

Link to comment
Share on other sites

It should work. Maybe you left off the "()"? That stops it working. Try this and you'll see it working:
<html><head><script type="text/javascript">function myfunction(){  alert('myfunction called!');}</script><body><img src="my.jpg" onclick="myfunction()"></body></html>

If you still have problems please post your code, because it must be something else that's the problem.

I tried that, and it worked. But when I substitute the function code between {}, it doesn't do my redirect.I will try the snippet you just sent me again but without the onclick, using anchors instead and using the image as a regular image:
<a><img src="my.jpg"></a>

then i'll make sure I include:

<form name='a'>

and at the end i will do something like:

document.form['a'].submit();

if that doesn't work, in the morning I will post my code as suggested.pls bear with me. I'm a beginner. :)

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...