Is there any way of making a POST request when an svg image is clicked?
My best attempt so far looks like:
<form action="/test-button" method="POST">
<input name="Submit" type="submit" value="Submit" />
<svg >
<rect width="100" height="100" >
</svg>
</form>
Which is a black rectangle with a submit button next to it.
I want to allow people to choose between several images, so this may be an ok solution, but is there any way of making an image which, when clicked, will fire off a POST?
Extra points for not using javascript.
It's quite impossible without JS, but you can use JS to do it. Attached an
onclick()
and just use:document.getElementById('formID').submit();
.HTML:
and JS (goes within your
<head></head>
tags):Warning: This is a little hacky, but as far as I know it's 100% legit, and doesn't need javascript.
Since the
label
element can also be used to control it's associated input, you could try something like this:Then hide the submit buttons with CSS. You can put anything in the label that you want.
When you click on whatever's in the label, it will trigger the submit button inside it and submit the form, with the button's
value
in the POST array.There is also an
<input type="image">
, but that's for an entirely different purpose (tracking coordinates of where it was clicked).This, maybe:
Jsfiddle: http://jsfiddle.net/Xawuv/
This seems like a great use case for the
<button>
element.Clicking the button element performs the exact same job as
input[type="submit"]
, so you can replace the input entirely. If you go this route, you may also consider putting a text label inside the button and/or atitle
inside the svg for accessibility purposes.This is a simple button with a lightning inside the box.
and here is the script for the fucntion when you click
The problem when you create a button and inside it has lines and when you put to the parent element the onclick you don't click all the svg element but you click separately and all children inside. so you need to create a rect inside the svg with the width and the height of the parent element and hide it, and after put there the onclick!