PDA

View Full Version : Submitting Parameters to a JavaScript Function



WebmasterGeeks
09-27-2006, 05:39 AM
I have a form on my site which when submitted, sends the result to a javascript function, which then properly handles the data. This allows the form to be submitted through javascript without the user leaving the page. My problem is that whether I use the "get" or "post" method, I can't figure out how to submit the data properly.

Here's an example of how this should work:

If the user selects "Selection 2" from the dropdown and clicks submit, then the action should be "javascript:submittopage('submitform.php?Option=2', '1');""

What I can't figure out is how to append the "?Option=2" to the end of the submitform.php URL. I've been trying to figure this out for quite some time, but I'm stumped. Is there anyone here who's more proficient in javascript and who knows how to make this work?

Thanks!

Here's my form code:


<form id="form1" name="form1" method="get" action="javascript:submittopage('submitform.php', '1');">
<label>
<select name="Option">
<option value="" selected="selected">Select An Option</option>
<option value="1">Selection 1</option>
<option value="2">Selection 2</option>
<option value="3">Selection 3</option>
<option value="4">Selection 4</option>
<option value="5">Selection 5</option>
</select>
</label>
<label>
<input type="submit" name="Submit" value="Submit">
</label>
</form>

mwinter
09-27-2006, 12:33 PM
I have a form on my site which when submitted, sends the result to a javascript function, which then properly handles the data.

What does "properly handle" mean?



This allows the form to be submitted through javascript without the user leaving the page.

And makes the form useless without scripting support.

There is little point in trying to fight how the Web works, particularly when it means reinventing the wheel square. Perhaps if you explain the real issue you're trying to solve, you can get a better suggestion.

Mike

WebmasterGeeks
09-27-2006, 01:32 PM
Thanks Mike,

By "Properly handles" I mean that it processes the data. If you look at the example code I provided, you'll see that the script pulls submitform.php, which would clearly indicate scripting support. But that really makes no difference in terms of the question that I'm asking.


What I'm doing is simple...

1) The user selects an item from the dropdown menu, and clicks submit.
2) The selected item is sent to a script.
3) The script tells the visitor "thanks" and uploads the data to the server.

Steps #1 and #3 are finished - they work great. The only thing I'm stuck on is how to let the script know which item has been selected.

So... the form currently looks like this:

<form id="form1" name="form1" method="get" action="javascript:submittopage('submitform.php', '1');">


and I essentially need it to look like this:


<form id="form1" name="form1" method="get" action="javascript:submittopage('javascript:submittopage('submitform.php?option=(which ever form item was selected)', '1'); ">

It would be easy for me to assign the value for "option" if I were using php, but this needs to be client side, and automatically update the form when either the dropdown item is selected or the form is submitted.

Thanks for all your help.

Twey
09-27-2006, 05:31 PM
If you look at the example code I provided, you'll see that the script pulls submitform.php, which would clearly indicate scripting support.But it's still useless if the client doesn't have scripting support.
and I essentially need it to look like this:No, you don't. You need a fallback. Probably something like this:
<form id="form1" name="form1" method="get" action="submitform.php" onsubmit="submittopage('submitform.php', '1'); return false;">submittopage(), of course, will process the form into some form of AJAX data that will include the contents of all the form elements.