2

I trying to display the content of a MySQL TEXT field inside a <pre></pre> html markup.
To accomplish that I use an ajax call

$.ajax({
    type : 'POST',
    url : webserviceFile,
    data : {
        action : "confFile",
        id : id
    },
    success : function(data) {
        $("#configFile").html(data);
    },
    error : function(e, f, g) {
        return ("An error occure: " + e + "\n" + f + "\n" + g);
    }
});

On server Side I have that piece of php :

switch($action) {   
    case 'confFile' :
            $value = getConfigurationFile($_POST['id']);
            echo "##$value##";
    break;
}

Finally my orignal html file is :

<body>
    <pre id="configFile" ondblclick='selectText( "configFile" )'></pre>
</body>

So the script does work, now my problem is that on Firefox IE8 and Chromium I have 3 spaces in front of my ajax result. For example, one of the file contains :
no config file + the two dashes I add to be sure that it does not come from my php script, in my <pre> mark I will end up with :

<pre id="configFile" ondblclick="selectText( &quot;configFile&quot; )" class="ui-dialog-content ui-widget-content" style="width: auto; min-height: 67px; max-height: 667px; height: auto;">
   ##no config##</pre>

And as you can see there's 3 spaces in front of my ##no config## string.

I'm using the last stable jQuery and watching what the POST return with Firebug I do see those spaces, I remove every echo or print in my php code except the one returning the ##no config## but still those spaces appear.

Do you have any idea on how and why those fµµµµ appears, and do you know a work around for that ?

Thanks

Edit 1: Using trim is one solution but the content of the MySQL field is not modifiable and I can't remove white space at the end or at the beginning of that content for displaying.

Complete answer:
Remove every spaces before and after the php markup <?php and ?> in all you php files to avoid that behaviour that should do the trick

1
  • use trim() to remove extra whitespaces Dec 18, 2013 at 8:55

2 Answers 2

7

make sure that your server side php file does not have any spaces before the php open tags and if possible do not use '?>'-tags to prevent trailing spaces aswell

4
  • what do you mean by do not use ?> should I remove that from my php script file ?
    – Kiwy
    Dec 18, 2013 at 9:00
  • it seems that is does indeed come from my php script because if I remove every inch of code and just put echo "any value" it is send correctly to the ajax call
    – Kiwy
    Dec 18, 2013 at 9:06
  • It was indeed a space before a <?php markup in one of the included files
    – Kiwy
    Dec 18, 2013 at 9:10
  • 1
    if a file is pure PHP code then you can leave out the ?> at the end of the file. (this is advised, see also php.net/manual/en/language.basic-syntax.phptags.php
    – diesonne
    Dec 18, 2013 at 11:06
1

try jQuery.trim() or replace()

var newAnswer = data.replace(" ", "");
$("#configFile").html(newAnswer );
1
  • not a solution for me, see the Edit and the comment on previous answer
    – Kiwy
    Dec 18, 2013 at 9:01

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.