I want to call forms and upload image with some texts. But i dont want submitting refresh the page. How can i do that? Thank you very much...
When i click to Bring Form button a form comes and when i clicked to submit button page refreshing and form disapears. Like this...
Form disapears. I dont want this. How to do that? Here codes...
index.html
<!DOCTYPE html>
<html>
<head>
<title>TEXT & FILE UPS</title>
</head>
<body>
<button onclick="bring_form();" >Bring Form</button>
<span id="form_field"></span>
<script src="assets/js/jquery-3.2.1.min.js"></script>
<script src="assets/js/custom.js"></script>
</body>
</html>
custom.js
$(document).ready(function () {
$("#form_1_0").on('submit',(function(e){
e.preventDefault();
var formData = new FormData(this);
var action = "test";
formData.append('action', action);
$.ajax({
url: "actions.php",
type: 'POST',
data: formData,
async: false,
success: function (data) {
console.log(data);
alert(data);
},
cache: false,
contentType: false,
processData: false
});
return false;
}));
});
function bring_form(){
var action = "bring_form";
$.ajax({
type: "POST",
url: "actions.php",
beforeSend: function () {
},
data: {action: action},
success: function(msg)
{
var msg = msg.trim();
$("#form_field").html(msg);
}
});
}
actions.php
<?php
if(!empty($_POST)){
$action = $_POST["action"];
if($action == "bring_form"){
echo '<form id="form_1_0" method="post" enctype="multipart/form-data">
<input type="text" name="txt_1" value="Bob" />
<input type="text" name="txt_2" value="James" />
<input type="text" name="txt_3" value="Smith" />
<input name="image" type="file" />
<button >Submit</button>
</form>';
}elseif($action == "test"){
print_r($_POST);
print_r($_FILES);
}
}
?>
You need to delegate the submit handler to an element that actually exists at the time you call it since the form doesn't exist
Try changing:
$("#form_1_0").on('submit',(function(e){
To
$(document).on('submit',"#form_1_0",function(e){
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments