So i am basically new to AJAX and i am having a hard time fixing this issue. First, i have like a table. i have an onclick function that gets the id .
<a href="javascript:void(0);" onClick="showEdit(this.id)" id="<?php echo $catrow['categoryid'];?>"></a>
This is supposed to pick some data from the database using the id, store it in a modal, the modal can then be used to edit these data and then store it back in the database... all without reloading the page.
function showEdit(catid){
$.ajax({
url: "edit-check.php",
type: 'POST',
data: {"catid":catid},
success: function(data){
console.log(data);
var show = $('#showEdit').attr('id');
document.getElementById(show).innerHTML = data;
$('#addContactModal').modal('show');
},
});
}
this is the edit-check.php
<?php
include ('../connect.php');
if(isset($_POST['catid'])){
$id=$_POST['catid'];
$checkquery = "SELECT * FROM category WHERE categoryid='$id';";
$checkcategory=mysqli_query($conn,$checkquery);
if (mysqli_num_rows($checkcategory) == 0){
echo ("//");
}
else{
$crow = mysqli_fetch_assoc($checkcategory);
?>
<div class="modal fade" id="addContactModal" tabindex="-1" role="dialog" aria-labelledby="addContactModalTitle" aria-hidden="true">
<form id="editCategory" autocomplete="off" enctype="multipart/form-data" method="post" name="form">
<input type="text" class="form-control" name="category_id" id="category_id" readonly value="<?php echo $crow['categoryid'];?>">
<input type="text" name='category_name' id="category_name" class="form-control form-control-danger" placeholder="Food" value="<?php echo $crow['categoryname'];?>">
<input name="file" type="file" id='file' />
<textarea class="form-control" name="about_category" id="about_category" rows="6"><?php echo $crow['aboutcategory'];?></textarea>
<div class="modal-footer">
<button type="submit" name="addCategoryBtn" id="add_category" class="btn btn-success"> <i class="fa fa-check"></i> Update Category </button>
<button class="btn btn-dark" data-dismiss="modal"> Discard</button>
</div>
</form>
</div>
<?php }}?>
the code works well until this point...
then I have another function that is supposed to prevent this modal from submitting. (but it is not). the page just reloads
$(function() {
$("form#editCategory").submit(function(z) {
z.preventDefault();
var formData = new FormData(this);
$.ajax({
url: "add-check.php",
type: 'POST',
data: formData,
success: function(data){
alert('worked');
},
cache: false,
contentType: false,
processData: false
});
})
});