I'm calling a function on a page on a test php website to display a html form to record data to be submitted to my SQL database via ajax, but for some reason no data is being inserted. I have all the required bootstrap, js, ajax links in an included header.php and I have other forms submitting to tables in the same database on the test site which are working with no issues so the database connection seems to be fine, which is why I'm struggling so much to work out why the data isn't being inserted.
This is a localhost test server on MAMP using MySQL 5, PHP 5.
My file with the code to display a html entry form when called is in functions.php:
<?php
session_start();
$link = mysqli_connect("localhost", "user", "pass", "tables");
if (mysqli_connect_errno()) {
print_r(mysqli_connect_error());
exit();
}
// other code ...
function displayTextInput() {
global $link;
echo '
<div class="form">
<div class="form-group">
<textarea class="form-control" id="textDesc" placeholder="Description"></textarea>
<textarea class="form-control" id="textType" placeholder="Type"></textarea>
</div>
<button id="postTextButton" class="btn btn-primary">Post Text</button>
</div>';
}
?>
My ajax call is in a script inside a footer.php:
<script>
$("#postTextButton").click(function() {
$.ajax({
type: "POST",
url: "actions.php?action=postText",
data: {textDesc: $("#textDesc").val(), textType: $("#textType").val()},
success: function(result) {
if (result == "1") {
alert("ajax successful");
} else if (result != "") {
alert("ajax unsuccessful");
}
}
})
})
</script>
And finally my SQL is within a actions.php:
if ($_GET['action'] == 'postText') {
if (!$_POST['TextDesc']) {
echo "Your text description is empty!";
} else {
$textDesc = mysqli_real_escape_string($link, $_POST['textDesc']);
$textType = mysqli_real_escape_string($link, $_POST['textType']);
$userIDForTable = mysqli_real_escape_string($link, $_SESSION['id']);
mysqli_query($link, "INSERT INTO text (`description`, `type`, `userid`) VALUES ('$textDesc', '$textType', '$userIDForTable')");
echo "1";
}
}
I can echo out statements within the actions.php else { } section so the code is definitely connected to there but I really just can't seem to work out why. I've even linked the above code to a different php table within the same database and had it working so I just can't get my head around why this isn't working. There are no errors in the console I can see either. Any help would be greatly appreciated.
Thanks