I have a simple HTML form:
var submitForm=function() {
var i={};
i[0]=$('#sname').val();
var json=JSON.stringify(i);
$.ajax({
url: './form.php',
type: 'POST',
data: {data: json},
success: function() {
alert('Section Added.');
}
});
}
<input type="text" id="sname">
<button onClick="submitForm()">submit</button>
In PHP, I take this value and insert it into a database, such as:
$data=json_decode($_POST['data'], true);
$section=$data[0];
include './sql/write-section.php';
$sql="CREATE TABLE ".$section." (revdate varchar(250), title varchar(250), descrip varchar(255), permissions varchar(99999), file varchar(255))";
$result = $conn->query($sql);
$conn->close();
The issue is, that the table isn't being created. Initially, I thought it was an issue in my code, however if I were to replace line 2 in the PHP with just
, it works fine.$section="test";
Additionally, if I add in some lines between the POST and SQL query, it sometimes works.
It is a very odd behavior and I am thinking there is some sort of async or lag issue between JS/PHP/SQL. Any ideas?
Thanks!
</div>