4

Below is my Javascript code where k and m are Javascript variables.

function javascriptfunction() {
  document.forms[formname].action="gotopage.php?parameter1="+k+"&parameter2="+m;
  document.forms[formname].submit();
}

The above code executes correctly when my HTML form has a POST method. Below is my HTML page:

<form name="formname" action=# method=POST>
  <input type=text name="data1" value="one">
  <input type=text name="data1" value="two">
  <input type=button name="button1" value="send" onclick="javascritfunction();">
</form>

But when I give a GET method in my HTML form, then the HTML form data is submitted i.e

gotopage.php?data1=one&data2=two is submitting not Javascript action value i.e
gotopage.php?parameter1="+k+"&parameter2="+m

So how to submit the form with Javascript parameter when the method is GET in the HTML form?

2 Answers 2

8

Submitting a GET form will replace the query string in the action with the form data.

Put the data in hidden inputs instead.

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

2 Comments

how can i hidden input in html form
createElement / appendChild
0

In a GET request the form data has nowhere to 'go' other than in the query parameter string. So as Quentin points out, the query string gets automatically swapped out with the form field key value pairs. So you must use hidden fields to include extra data.

In a POST request you've also got the query parameter string available, if you want to use it, but the form data gets inserted into the request payload (which a GET request does not have), so does not interfere with the query string. That's why you can customise the action url with whatever query string you like.

1 Comment

thanks Quentin and thanks Davnicwil for giving solution of my question

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.