Hi,
I am trying to send input Don't, can't or anything else which contains '. But Ajax sends it like: t_name=Don%27t&_=1589636831048 and then I got an error while trying to insert to mysql. I only have problem with '. I tried lots of thing but something is missing. I couldn't figure it out. Could somebody help me?
My Ajax:
var t_name = document.getElementById(mydiv).value;
$.ajax(
{
type: "GET",
url: './api/objects/add.php',
async:false,
cache:false,
contentType: "application/x-www-form-urlencoded;charset=ISO-8859-1",
dataType: 'json',
data: {
t_tsk: id,
t_name: t_name
},
success: function (result) {
},
error: function(xhr, status, error) {
document.getElementById("errorDiv").hidden = false;
if (xhr.responseText) {
} else {
}
}
});
My url when I write don't to input area: /add.php?t_tsk=1&t_name=Don%27t&_=1589636831048
My php code:
<?php
// include database and object files
include_once '../config/database.php';
include_once '../objects/myObject.php';
// get database connection
$database = new Database();
$db = $database->getConnection();
$myObject = new MyObject($db);
$myObject->t_id = isset($_GET['t_tsk']) ? $_GET['t_tsk'] : die();
$myObject->name = isset($_GET['t_name']) ? utf8_decode($_GET['t_name']) : die();
if($myObject->create()){
http_response_code(200);
$myObject_arr=array(
"status" => true,
"message" => "Successfully created!",
"id" => $myObject->id,
"name" => $myObject->name
);
}
else{
http_response_code(401);
$myObject_arr=array(
"status" => false,
"message" => "Error!"
);
}
print_r(json_encode($myObject_arr));
?>
Thank you all.