3

I am trying to do a form submit from javascript. The form gets submitted but the parameters are not getting passed. Below is my code.

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Tic Tac Toe</title>
<script type="text/javascript">
function selection(selected){
    theform = window.document.tictactoe;
    theform.action="GameAction?selection="+selected.id;
    theform.submit();
}
</script>
</head>
<body>
<form name="tictactoe" id="tictactoe" method="GET" action="GameAction">
<table border="1">
    <tr><td><img id="0,0" src="images/white.JPG" onclick="selection(this);"></img></td><td><img id="0,1" src="images/white.JPG" onclick="selection(this);"></img></td><td><img id="0,2" src="images/white.JPG" onclick="selection(this);"></img></td></tr>
    <tr><td><img id="1,0" src="images/white.JPG" onclick="selection(this);"></img></td><td><img id="1,1" src="images/white.JPG" onclick="selection(this);"></img></td><td><img id="1,2" src="images/white.JPG" onclick="selection(this);"></img></td></tr>
    <tr><td><img id="2,0" src="images/white.JPG" onclick="selection(this);"></img></td><td><img id="2,1" src="images/white.JPG" onclick="selection(this);"></img></td><td><img id="2,2" src="images/white.JPG" onclick="selection(this);"></img></td></tr>
</table>
</form>
</body>
</html>

It would be great if someone could help in resolving this issue.

1 Answer 1

3

When the form gets submited through 'get', the browser takes the values of the form, makes a query string out of it and then sends the window to that location. If there was already a query string there, it doesn't just append the new values.. it wipes them out. If you change the method to a 'post', you'll notice that it does in fact add that data you wanted to have added to the URL. Since I'm not sure what exactly your end result really is, I at least hope this information helps you out. It's just based off experience, so others might have more technical insights.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Stephen for comming forward to help. As mention by I changed the method from get to POST. Now the form is not getting submitted at all.
Well, is it that the form isn't being submitted, or you aren't getting the data? You're adding the data to the query string, so the server is going to have to look for it from the GET (i.e. $_GET['selection']) and it won't have any posted data due to no data actually being posted. You might want to take a couple steps back and possibly re-think how your application works and explain it a little more. Otherwise I'm just taking shots in the dark :)
Thanks a lot Stephen. What you is said is right after changing the method to POST worked. In the servlet I was checking for parameter in the doGet instead of doPost()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.