Why is return getting linebreaks added to it?
I'm calling saveVideo.php via ajax and $result always has t开发者_如何学Cwo linebreaks at the top, I don't see where they could be coming from. It should be simply the $id variable in addVideo. I need the id to ajax in the new video once it's uploaded.
Even with trim(), the output from the saveVideo ajax call in Developer tools has 2 linebreaks in front of it.
Here's all the relevant code:
saveVideo.php:
<?php
require_once '../model.php';
$m = new Model;
$video = new Video;
$user = requireLogin();
$video->file = $_POST['url'];
$video->lib = 0; //sets library to be personal.
$video->title = mysql_escape_string($_POST['video-title']);
$video->desc = mysql_escape_string($_POST['video-description']);
$video->userId = $user->id;
$result = $m->addVideo($video);
echo trim($result);
?>
the addVideo function:
function addVideo($video, $lib = 1) {
// Adds video to database and associates it with a user id
$qstring = "mysqlquery";
$result = mysql_query($qstring);
$id = mysql_insert_id();
if (!$result) {
die("Error adding video to database.");
}
return $id;
}
Look for unnecessary blank lines before your opening <?php
or after your closing ?>
tags. If there are blank lines outside of a <?php ?>
block they will be treated as part of the page content and be sent as part of the PHP response.
This applies to ../model.php
as well. Blank lines in there will do the same thing.
Also, when you're debugging something like this you'll save a lot of hair pulling if you use var_dump
rather than echo
.
var_dump($result);
This would produce output like
string(2) "14"
which would tell you that $result
is not the source of the mysterious line breaks.
You can use a regexp or even a str_replace to remove those line breaks.
str_replace(array("\r", "\r\n", "\n"), '', $string);
精彩评论