У меня есть форма, загруженная в БД с помощью AJAX.Страница php, где у меня есть код для загрузки формы, возвращает «Success» в случае успешной загрузки, «Failed» в другом случае.
На моей главной странице (где запускается скрипт) у меня есть функция, которая отображает сообщение (успешная / неудачная загрузка) на основе эха со страницы php.
Основная проблема заключается в том, что эхо-дисплей отображает дополнительный пробел перед «успехом», поэтому в сценарии условие if (data == 'Success') никогда не выполняется.
<!-- SCRIPT -->
<script>
// Document get ready --> DefaultOpen Page + Variable from form
$(document).ready(function(){
$('#home').css('display', 'block');
$("#submitNewStrategy").click(function(e){
e.preventDefault();
$.post(
'./php/newStrategy.php',
{ name: $("#newStrategyName").val(),
owner: $("#newStrategyOwnerEmail").val(),
description: $("textarea#newStrategyDescription").val(),
region: $("#newStrategyRegionId").val(),
country: $("#newStrategyCountryId").val(),
market: $("#newStrategyMarketId").val(),
strategy: $("#newStrategyStrategyId").val(),
status: $("#newStrategyStatusId").val(),
nextStep: $("#newStrategyNextStep").val(),
highLevelInvestment: $("#newStrategyInvestment").val()
},
function(data){
console.log(data);
if(data == 'Success'){
$("#dbCommentNewStrategy").css('display', 'block');
$("#dbCommentNewStrategy").html("<p>cela fonctionne ! </p>");
}
else{
$("#dbCommentNewStrategy").css('display', 'block');
$('#dbCommentNewStrategy').html('Error! Unable to add project ' + $("#newStrategyName").val()+" into the database.");
}
},
'text'
);
});
});
</script>
и мой php:
<?php include 'functions.php';
$bdd = new PDO('mysql:host=localhost;dbname=workplan;charset=utf8', 'root','');
$name=htmlspecialchars($_POST['name']);
$owner=htmlspecialchars($_POST['owner']);
$description=htmlspecialchars($_POST['description']);
$region=htmlspecialchars($_POST['region']);
$country=htmlspecialchars($_POST['country']);
$market=htmlspecialchars($_POST['market']);
$strategy=htmlspecialchars($_POST['strategy']);
$status=htmlspecialchars($_POST['status']);
$nextStep=htmlspecialchars($_POST['nextStep']);
$highLevelInvestment=htmlspecialchars($_POST['highLevelInvestment']);
$country=is_empty($country);
$market=is_empty($market);
$add = $bdd -> prepare("INSERT INTO market_strategy(name, owner, description, region_id, country_id, market_id, strategy_id, status_id, next_step, high_level_investment)
VALUES (:name, :owner, :description, :region_id, :country_id, :market_id, :strategy_id, :status_id, :next_step, :high_level_investment)");
$add->bindParam(':name', $name);
$add->bindParam(':owner', $owner);
$add->bindParam(':description', $description);
$add->bindParam(':region_id', $region);
$add->bindParam(':country_id', $country);
$add->bindParam(':market_id', $market);
$add->bindParam(':strategy_id', $strategy);
$add->bindParam(':status_id', $status);
$add->bindParam(':next_step', $nextStep);
$add->bindParam(':high_level_investment', $highLevelInvestment);
if($add->execute()) {
echo "Success";
}
else {
echo "Failed";
}?>
Мой console.log с моей главной страницы возвращает мне: «[пробел] Успех» вместо «Успех».Я дважды проверяю, но я не вижу неправильного написания.
Я видел этот вопрос (та же проблема: php, создающий дополнительное пространство на html-странице ), но я не нашел ответа, как его решить.
Спасибо.