Jump to content

Submitting A Form Using Js Onclick


Grabeorama

Recommended Posts

I would like to submit a form when the user clicks a link.So like:

<a href="<?php echo $_SERVER['php_self'] ?>" onclick="submit_form()" >Vote</a>

I'm guessing I would need to use a function ("Submit_form()"). The input fields I have are hidden, as the value I want to send is fixed.Any help is appreciated.Thanks :)

Link to comment
Share on other sites

Thanks that works perfectly.How would I pass a value through onclick as well?e.g.If "Yes" is clicked put "1" into the input field.If "No" is clicked put "2" into the input field.Yes and no are two different strings displayed on the page.What I'm trying to do is depending on which is clicked, a value would be put into the input field and the form would be sent.

Link to comment
Share on other sites

As an option, use no form at all. Your link could simply have a query string (which can be generated in PHP the same way you're probably generating the values of your hidden inputs.) Something like this:<a href="<?php echo $_SERVER['php_self'] . '?value=' . $my_val ?>" >Vote</a>And then your script would expect the data in $_GET['value'] .EDIT. I posted before I saw you had reposted, but the same technique can be applied.

Link to comment
Share on other sites

So go back to your submit_form() function. A simplified version might look like this:

function submit_form(val) {	var f = document.getElementById("the_form");	f.my_field.value = val;	f.submit();}

And your html would look something like this (I'm stripping out the PHP for clarity) -- you can still use onclick if you need to:

<a href="java script:submit_form('1');" >Vote for 1</a><a href="java script:submit_form('2');" >Vote for 2</a>

Remember to delete the space in java script. :)

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...