0

I have an input field which is being sent to the server, checked, then returned. Strangely though, by the time it has returned, it has what looks like a tab break at the end.

jQuery:

var name = $("#name").val();
$.ajax({
    url: "JSON/checkName.php",
    data: "name=" + name,
    method: "POST"
})
.done(function(msg) {
    alert("msg: " + msg + ". Name: " + name + ".");
    }
})

checkName.php:

<?php
require_once "../dbPDO.php";

function clean($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}

$name = clean($_POST["name"]);

$sql = "SELECT * FROM `names` WHERE `name` = '$name' AND `valid` = TRUE";
$stmt = $DBcon->prepare($sql);
$stmt->execute();
$count = $stmt->rowCount();
if($count == 1) {
    echo $name;
} else {
    echo "Error";
}
?>

Upon running this, the alert dialog displays:

msg: James  . Name: James.

...when it should display:

msg: James. Name: James.

Any ideas?

Brad
  • 1,019
  • 1
  • 9
  • 22

1 Answers1

2

If you have any whitespace after the closing ?> tag it will end up in your output. You don't need the end tag anyway (see Why would one omit the close tag?).

Community
  • 1
  • 1
Adam Coster
  • 1,162
  • 1
  • 9
  • 16