I created the form with JavaScript, but on submit the page reloads and rest of the codes doesn't run. Creating and submitting this form is for passing JavaScript value to PHP.
How can I submit this form without reloading the page?
var form1 = document.createElement("form");
var element1 = document.createElement("input");
var element2 = document.createElement("input");
form1.id="testForm"
form1.method = "POST";
form1.action = "index.php";
element1.value= amount;
element1.name="amount";
form1.appendChild(element1);
element2.value=description;
element2.name="desc";
form1.appendChild(element2);
document.body.appendChild(form1);
I tried something like that, but still no success.
$("#testForm").on("submit", function (e) {
e.preventDefault();
$.ajax({
type:"POST",
url:"index.php",
data:$("#testForm").serialize(),
success: function(response){
console.log(response);
}
});
});