This code executes post.php
:
function SubmitForm() {
var input = $("#input").val();
var user = "anon";
$.post("post.php", {input: input, user: user}, function(data) {});
alert("hello");
}
But if I remove a line, this doesn't:
function SubmitForm() {
var input = $("#input").val();
var user = "anon";
$.post("post.php", {input: input, user: user}, function(data) {});
}
Seems irrational, but why is this happening?
The PHP which the JS should call inserts some values into the database:
<?php
$dbhost = "xxx";
$dbname = "xxx";
$dbuser = "xxx";
$dbpass = "xxx";
// Create connection
$con = mysqli_connect($dbhost, $dbuser, $dbpass);
// Check connection
if (!$con) {
die("Database connection failed: " . mysqli_error());
}
// Select database
$db_select = mysqli_select_db($con, $dbname);
// Check selection
if (!$db_select) {
die("Database selection failed: " . mysqli_error());
}
$message = $_POST["input"];
$user = $_POST["user"];
echo $message;
echo $user;
$query = "INSERT INTO posts (user, message) VALUES ('$user', '$message')";
mysqli_query($con, $query);
?>