I have other functions just like this that work, however when I click, the Ajax gets posted but my page would originally refresh, until I added event.preventDefault. I have been stumped for hours and I cant see the problem..
Here is my AJAX:
$(function(){
$(document).on('click','.addnewproject',function(){
event.preventDefault();
var curaid= $(this).attr('id');
var $ele = $(this);
$.ajax({
type:'POST',
url:'components/sql/insertproject.php',
data:$("#addnewprojecttable").serialize(),
success:
function(data){
$(".image-list" + curaid).append(
'<li id="projectfolder' + curaid + '">\n\
<img width="35px" onclick="openproject('+ curaid +')" style="cursor: pointer;" src="img/folder.png" /> \n\
<p>Project ' + curaid + '</p> \n\
');
}
});
});
});
My HTML:
<form id="addnewprojecttable" method="post" >
<table>
<tr>
<td>
<input style="display:none;" name="username" id="username" value="<?php echo "" . $_SESSION['username']; ?>" type="text"/>
<input style="float:left; width:100%; height:20px; margin-bottom: 20px;" name="projectname" id="projectname" type="text" placeholder="projectname"/>
<input style="float:left; width:100%; height:20px; margin-bottom: 20px;" name="contactname" id="contactname" type="text" placeholder="contactname"/>
<input style="float:left; width:100%; height:20px; margin-bottom: 20px;" name="contactemail" id="contactemail" type="text" placeholder="contactemail"/>
<input style="float:left; width:100%; height:20px; margin-bottom: 20px;" name="contactphone" id="contactphone" type="text" placeholder="contactphone"/>
<input style="float:left; width:100%; height:20px;" name="description" id="description" type="text" placeholder="description"/>
<input style="float:left; width:100%; height:20px;" name="notes" id="notes" type="text" placeholder="notes"/>
<button class="addnewproject" id="<?php echo $row["accid"]; ?>" >Add</button>
<button onclick="closenewprojectwindow()">Cancel</button>
</td>
</tr>
</table>
</form>
My SQL:
<?php
include 'config.php';
$sqlupdateincome = "INSERT INTO projects (username, projectname)
VALUES ('". $_POST['username'] ."', 'Ochrom Test Project')";
if ($conn->query($sqlupdateincome) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
Here is my database just in case it might be that. click for image
I do appreciate all help and responses, thank you!