I am absolutely new to jQuery and ajax. Currently I am trying to create a table on my local sql server from a javascript file from which I am posting the statement to .php file to execute the statement.
.js file:
function executeStatement(sqlStatement){
$.ajax({
type: "post",
data: sqlStatement,
cache: false,
url: "api.php",
dataType: "text",
error: function(xhr, status, error) {
var err = eval("(" + xhr.responseText + ")");
alert(err.Message);
},
success: function ()
{
alert ("Success!!");
}
});
}
.php file:
require_once('PhpConsole.php');
PhpConsole::start();
debug('HERE!!!');
$sqlStatement = $_POST['sqlStatement'];
$host = "*****";
$user = "*****";
$pass = "*****";
$databaseName = "db_user_data";
// Create connection
$con = mysqli_connect($host, $user, $pass, $databaseName);
// Check connection
if (mysqli_connect_errno($con)){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
else{
$con->query($sqlStatement);
header('Location: success.php');
}
I use PHP Console to debug .php files but in this case even the first log 'HERE!!!' is not printed to the console so I am wondering whether it even reaches this .php file. Anyway the event success within executeStatement method is reached and 'Success' printed even though there are no changes in the database.By the way the .php file is also executed on the local server. Does someone has any ideas where the problem can be??
Thanks in advance
jsonformat.{username: 'hello', password: 'world'}, set thedataTypeoption in theajaxfunction asjson, then inPHP, decode thejsonand do your PHP query in the PHP file$_POST