0

Is there a way to add data to a form before submitting it?

or add this form to a formdata and submit the formdata (without .$post, .$ajax or XMLHttpRequest). the formdata can be submitted as a normal form.

Martin
  • 37
  • 1
  • 6
  • Are you saying you can do any kind of external requests using JavaScript or just not AJAX? – Cory Fail Feb 17 '21 at 06:48
  • It is because of the format, we will want to send additional information generated by javascript to the form but in a normal way, not asynchronous, in order that the server can validate the data of the form and display it. – Martin Feb 17 '21 at 15:24

3 Answers3

2

You could do form submit event

function test(){
 document.querySelector('#new_insert').value="hi"
 //do stuff here

 return true
}
<form action="action.php" method="get" onsubmit="return test()">
 <input type="text" name="one"/>
 <input id="new_insert" name="new" type="text">
 <input type="submit">
</form>
prasanth
  • 22,145
  • 4
  • 29
  • 53
1

you can add any hidden input you want:

<input type="text" name="my_hidden_input" value="my_value" id="hidden_input" hidden/>

and change the value whenever you want (on form submit or after page gets load)

$('#hidden_input').val("my_second_value")
msaba
  • 76
  • 4
1
<form onsubmit="return addfield();" id="myform">
  <input type="text" name="txtname" value="Your name">
  <input type="submit" name="btnsubmit" value="Submit">
</form>

<script>

  function addfield(){
    var oldhtml=document.getElementById("myform").html();
    var newhtml='<input type="text" name="txtcity" value="Your city">';
    document.getElementById("myform").html(oldhtml+newhtml);
  }

</script>
RobC
  • 22,977
  • 20
  • 73
  • 80