I'm doing an AJAX request and somehow a trailing newline is added somewhere.
My PHP script echoes (or is supposed to echo) 'SUCCESS'
if the request succeeded, 'ERROR'
otherwise.
But currently it returns: 'SUCCESS'
(i.e. 'SUCCESS\n'
).
I saw that by adding an alert("!" + msg + "!")
that showed the line break.
My AJAX call:
function addMedia() {
var addMediaName = $("#addMediaName").val();
var notif;
if(addMediaName != ""){
$.ajax({
url : '../../controler/add/addMedia.php',
type : 'POST',
data : "mediaName="+ addMediaName,
dataType : 'text',
success: function(msg,data, settings){
if(msg == 'SUCCESS'){
$.toaster({ priority : 'success', title : 'Success', message : 'Mode created' });
} else {
$.toaster({ priority : 'warning', title : 'Failed', message : 'Mode already exists' });
}
},
});
}
}
My PHP controller:
<?php
include ('../../model/request/add.php');
if((include_once '../../model/request/add.php')===FALSE) exit('erreur include');
$mediaName = $_POST['mediaName'];
$mediaName = ucfirst(strtolower($mediaName));
$media = addMedia($mediaName);
?>
And the addMedia
function:
function addMedia($mediaName)
{
global $conn;
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
try {
$sql = 'INSERT INTO media (mediaName) VALUES
("'.$mediaName.'")';
$conn->exec($sql);
echo 'SUCCESS';
} catch(PDOException $e) {
echo 'ERREUR';
}
}
Any idea where that newline is coming from and how I can fix it?