I have a form uploaded into the db with AJAX. The page php where I have the code to upload the form return "Success" in case the uplaod was successful, "Failed" in the other case.
In my main page (where the script is running), I have a function which display a message (successful/failed upload) based on the echo from the php page.
The main problem is the echo display an extra space before the "Success" therefore, in the script the condition if(data == 'Success') is never true.
<!-- 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>
and my 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";
}?>
My console.log from my main page return me: " [space] Success" instead of "Success". I double check, but I don't see any wrong spelling.
I saw this question (same problem: php creating extra space in html page) but I didn't see the answer how to solve it.
Thank you.