0

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.

  • You probably have a space before the ` – MonkeyZeus Sep 28 '18 at 14:35
  • You should add a screenshot of what you are seeing in the console log. – MonkeyZeus Sep 28 '18 at 14:36
  • 2
    And something can happen even in the imported 'functions.php' file – Lorenzo S Sep 28 '18 at 14:37
  • try create a new php file, copy paste the code on it and save. if the problem persist, use sublime text and from file use save with encoding UTF-8. Probably is a problem caused by BOM encoding and this happens when copy paste from other files. – Sigma Sep 28 '18 at 15:26
  • @LorenzoS in case the file 'functions.php' is the source of the problem, is the problem (the extra space) coming only from the function called (in this case 'is_empty') or it can come from any others function from this file? – Corentin Canet Sep 30 '18 at 08:20
  • @CorentinCanet not in the other functions, but in the imported file itself – Lorenzo S Sep 30 '18 at 13:15
  • 1
    @LorenzoS so I check the file and I found out I had some space after the '?>'. I didn't know that would make any issue. After removing the space, it works properly. – Corentin Canet Oct 01 '18 at 06:04

0 Answers0