1

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?

Pierre Belin
  • 49
  • 10

2 Answers2

3

That is most likely caused by the end of the PHP file, where a newline follows the ?>.

Now while it is possible to die/exit at a previous point, I consider it a much cleaner solution to simply remove the ?>.
The closing tag is not required by PHP, and it is usually even considered better practise not to use it, see this SO question.

Community
  • 1
  • 1
Siguza
  • 21,155
  • 6
  • 52
  • 89
2

add die() or exit() after

echo 'SUCCESS'; die;    

or

echo 'ERREUR'; die;

if you can't add die then code execute next line of code or in this case it will call 'view' in MVC.

Nishit Maheta
  • 6,021
  • 3
  • 17
  • 32
  • Or you can do easier by follow @Siguza answer : Wild guess: There's a newline after ?>. If that's the case, just remove the ?> – Pierre Belin May 20 '15 at 10:58
  • 1
    when you use ajax make sure to stop execution by adding die() or exit() of code when you echo or print rather then remove extra space or other code :) – Nishit Maheta May 20 '15 at 11:00
  • "Rather write bad code and prevent its execution than writing good code." - No. Just no. – Siguza May 20 '15 at 11:01
  • 1
    I get your point, however in that case, a new line after `?>` is not bad code, It's just a stupid and hard to find typo. I think the best thing to do is to die after you get the value you want, AND write good code :) – Jeremy Thille May 20 '15 at 11:11
  • @Siguza . when you use MVC . in you not stop code execution using `die()` or 'exit()'. it will call default layout or view file . in this case ?> formatting will not work . – Nishit Maheta May 20 '15 at 11:21