Reputation: 3317
I have 3 buttons (image links, eventually will evolve to javascript or something better though)
Each of these buttons should send a value to a handler script, which does choice 1 if button 1 is pressed, choice 2, so on and so forth.
I'm wondering what the best way to do this is. I DON'T WANT TO USE GET. I'd like to use POST if that's possible but I don't think it is. I thought about cookies too but the problem is even though you can call a JS function to create a cookie via a link, you can't go to the PHP page for processing, within the same click can you?
It would work like the user clicks the button img, then it takes them to the handler script, but the handler redirects them back before they even know they were there.
Again this isn't a form, I need to do this with a hyperlink. I suppose I could just have a different page for each choice, but I don't think that's efficient.
Thanks!
Upvotes: 2
Views: 1706
Reputation: 4868
If you want the variables to be passed using POST, you could create a form on the page, and have your links execute some javascript code at onClick. They'd set the variable to the desired value, then submit the form. The key lines would be something like:
document.getElementById("user_choice").value = 2; // or whatever the value for this link is
document.getElementById("my_form").submit();
Upvotes: 2
Reputation: 11999
In case you want to use JavaScript, have a look at jQuery. Using jQuery's click-method, you can easily handle clicks on elements:
Suppose you have this HTML:
<div id="target">
Click here
</div>
<div id="other">
Trigger the handler
</div>
Using jQuery, you can easily track click on the element labeled target:
$("#target").click(function() {
alert("Handler for .click() called.");
});
In case you don't want to POST or GET clicks directly, I'd propose to register jQuery click-handlers, which set JavaScript variable based on the clicked element.
Note, that jQuery's click-handler can be registered with any element, not only forms. Furthermore note, that e.g. the above click-handler does not POST or GET the page to the server.
Additionally, have a look here, on how to prevent the browser from submitting forms: What's the effect of adding 'return false' to a click event listener?
Even better than JavaScript is CoffeScript, which compile to JavaScript but makes live much easier. Give it a try!
Upvotes: 1
Reputation: 19251
You could turn your image links into:
<form method="post" action="blah">
<input type="hidden" name="function" value="function1" />
<input type="image" scr="whatever" />
</form>
This way they still look like images, but are actually post forms with whatever you want inside of them. That's the easy way anyways. The harder way would be to use AJAX.
Upvotes: 1
Reputation: 1579
what about AJAX? it's the best choice for your problem and the bonus part is you can use post too.
Upvotes: 0