Skip to content Skip to sidebar Skip to footer

How To Add Elements To A Form Before Submitting? Without Ajax

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 su

Solution 1:

You could do form submit event

functiontest(){
 document.querySelector('#new_insert').value="hi"//do stuff herereturntrue
}
<formaction="action.php"method="get"onsubmit="return test()"><inputtype="text"name="one"/><inputid="new_insert"name="new"type="text"><inputtype="submit"></form>

Solution 2:

<formonsubmit="return addfield();"id="myform"><inputtype="text"name="txtname"value="Your name"><inputtype="submit"name="btnsubmit"value="Submit"></form><script>functionaddfield(){
    var oldhtml=document.getElementById("myform").html();
    var newhtml='<input type="text" name="txtcity" value="Your city">';
    document.getElementById("myform").html(oldhtml+newhtml);
  }

</script>

Solution 3:

you can add any hidden input you want:

<inputtype="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")

Post a Comment for "How To Add Elements To A Form Before Submitting? Without Ajax"