Я пытался преобразовать форму ajax вставки, удаления, редактирования в свою собственную. Он отлично работает в localhost. Но всякий раз, когда я пытаюсь запустить этот код на сервере моего веб-сайта, вставка не работает (работает удаление и редактирование). Мои коды:
<div class="container box">
<h1 align="center">PHP PDO Ajax CRUD </h1>
<br />
<div class="table-responsive">
<br />
<div align="right">
<button type="button" id="add_button" data-toggle="modal" data-target="#userModal"
class="btn btn-info btn-lg">Add</button>
</div>
<br /><br />
<table id="user_data" class="table table-bordered table-striped">
<thead>
<tr>
<th width="10%">Image</th>
<th width="30%"> Name</th>
<th width="30%">Education</th>
<th width="10%">age</th>
<th width="10%">Edit</th>
<th width="10%">Delete</th>
</tr>
</thead>
</table>
</div>
</div>
</body>
</html>
<div id="userModal" class="modal fade">
<div class="modal-dialog">
<form method="post" id="user_form" enctype="multipart/form-data">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Add User</h4>
</div>
<div class="modal-body">
<label>Enter First Name</label>
<input type="text" name="name" id="name" class="form-control" />
<br />
<label>Enter Last Name</label>
<input type="text" name="education" id="education" class="form-control" />
<br />
<label>Enter Last Name</label>
<input type="text" name="age" id="age" class="form-control" />
<br />
<label>Select User Image</label>
<input type="file" name="user_image" id="user_image" />
<span id="user_uploaded_image"></span>
</div>
<div class="modal-footer">
<input type="hidden" name="user_id" id="user_id" />
<input type="hidden" name="operation" id="operation" />
<input type="submit" name="action" id="action" class="btn btn-success" value="Add" />
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</form>
</div>
</div>
<script type="text/javascript" language="javascript" >
$(document).ready(function(){
$('#add_button').click(function(){
$('#user_form')[0].reset();
$('.modal-title').text("Add User");
$('#action').val("Add");
$('#operation').val("Add");
$('#user_uploaded_image').html('');
});
var dataTable = $('#user_data').DataTable({
"processing":true,
"serverSide":true,
"order":[],
"ajax":{
url:"fetch.php",
type:"POST"
},
"columnDefs":[
{
"targets":[0, 3, 4],
"orderable":false,
},
],
});
$(document).on('submit', '#user_form', function(event){
event.preventDefault();
var name= $('#name').val();
var education= $('#education').val();
var age= $('#age').val();
var extension = $('#user_image').val().split('.').pop().toLowerCase();
if(extension != '')
{
if(jQuery.inArray(extension, ['gif','png','jpg','jpeg']) == -1)
{
alert("Invalid Image File");
$('#user_image').val('');
return false;
}
}
if(name!= '' && education!= '' && age!= '')
{
$.ajax({
url:"insert.php",
method:'POST',
data:new FormData(this),
contentType:false,
processData:false,
success:function(data)
{
alert(data);
$('#user_form')[0].reset();
$('#userModal').modal('hide');
dataTable.ajax.reload();
}
});
}
else
{
alert("Both Fields are Required");
}
});
код во вставке. php: (Я просто показываю код, вместо того, чтобы правильно использовать скобки, чтобы мы могли быть прямо в точке)
<?php
include('db.php');
include('function.php');
if(isset($_POST["operation"]))
{
if($_POST["operation"] == "Add")
{
$image = '';
if($_FILES["user_image"]["name"] != '')
{
$image = upload_image();
}
$statement = $connection->prepare("
INSERT INTO new_table (name, education, age, image)
VALUES (:name, :education, :age, :image)
");
$result = $statement->execute(
array(
':name' => $_POST["name"],
':education' => $_POST["education"],
':age' => $_POST["age"],
':image' => $image
)
);
if(!empty($result))
{
echo 'Data Inserted';
}
}
?>
Результат после отправки выглядит следующим образом:
введите описание изображения здесь