Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Submit form with parameter through Javascript

Tags:

javascript

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?

like image 897
hari vallabh shukla Avatar asked Mar 23 '23 02:03

hari vallabh shukla


1 Answers

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

Put the data in hidden inputs instead.

like image 162
Quentin Avatar answered Mar 28 '23 15:03

Quentin