0

So I have this code:

else {
    $strlen = json_encode(strlen($_POST['message']));
    ?>
    <script language="javascript" type="text/javascript">
        var length = <?php echo $strlen ?>
        alert("Your message was too long (max 255 chars), yours was " + length);
    </script>
    <?php 
    echo "Your message was too long (max 255 chars)";
    echo "Yours was " . $strlen . " charachters long";
}

... and It will just not display the alert box with the value of the actual strlen of the string.

The "Yours was $strlen charchters long" works fine.

I have also tried removing json_encode(), that did not either.

Zach Lysobey
  • 14,959
  • 20
  • 95
  • 149
Jordy
  • 948
  • 2
  • 9
  • 28
  • `json_encode` favors a two-dimensional array which holds the `key` and `value` of a JSON list/dictionary. –  Dec 30 '13 at 23:26
  • I think it would be better if you paste the resulting JavaScript code so we can see if it's a PHP or JavaScript problem – Alex Barroso Dec 30 '13 at 23:38
  • @user3122479 make sure to mark the "correct" answer (by clicking the checkmark) – Zach Lysobey Dec 30 '13 at 23:41
  • It's very surprising to me however that the problem was a missing semi-colon at the end of a `var` declaration. JavaScript has automatic-semi-colon-insertion, and should not blow up if you forget one. As suggested above, you really should paste in the JS output. – Zach Lysobey Dec 30 '13 at 23:43
  • 1
    @Allendar, `json_encode` does not "prefer" any such thing: http://us1.php.net/json_encode – JAAulde Dec 30 '13 at 23:45

6 Answers6

6

OK, I figured out what's going on here. Lots of mis-information in the comments here.

First off lets clear up the misconceptions here:

OP's code is broken because JavaScript is NOT adding a semi-colon on the following line:

var length = <?php echo $strlen ?>

Why not?

One newline character (or sequence) is dropped out by the parser [immediately] after "?>"

This is somewhat odd behaviour for php (in my opinion), but I've verified that it does in fact behave this way.

So, in conclusion, you DO need a semicolon on that and similar lines, because, when parsed it would look like: var length = 3 alert("Your message... otherwise.

If OP had included the generated source (JS) in his question, this would've been pretty immediately evident.

An alternative solution would be to add some more whitespace in there, so that JS ASI would still work - but as others have mentioned, its generally accepted "good practice" to add semi-colons in JS, even when they are optional.

Found my answers in the PHP documentation here: http://php.net/manual/en/language.basic-syntax.instruction-separation.php and its comments.

TL;DR: add a semicolon at the end of this line

var length = <?php echo $strlen ?>;
Community
  • 1
  • 1
Zach Lysobey
  • 14,959
  • 20
  • 95
  • 149
  • Thank you for clearing up some misconceptions (mine included :P) OP, if you are still hanging around, this has my vote for accepted answer. – Jordan Dec 31 '13 at 00:03
  • Yes, this is correct and thouroughly explains why. Upvote it, accept it, and etc. – JAAulde Dec 31 '13 at 00:05
  • Ah thanks for the great explaination, exactly what I needed. This sums it all up pretty much. THANKS! – Jordy Dec 31 '13 at 07:58
1

This should work:

...

$strlen = strlen($_POST['message']);
?>
<script language="javascript" type="text/javascript">
var length = <?php echo $strlen; ?>;
alert("Your message was too long (max 255 chars), yours was " + length);
</script>
<?php 
echo 'Your message was too long (max 255 chars)';
echo 'Yours was ' . $strlen . ' charachters long';

...
OptimusCrime
  • 14,662
  • 13
  • 58
  • 96
1

You are missing a semicolon here: var length = <?php echo $strlen ?>;

MillaresRoo
  • 3,808
  • 1
  • 31
  • 37
  • 1
    rather inside the ``, js dont require semicolon (altho I do prefer it personally). – Jite Dec 30 '13 at 23:30
  • 1
    @OptimusCrime If it solves the problem, it most certainly is an answer. – Jordan Dec 30 '13 at 23:32
  • @Jite, JS very much requires a semicolon. – halfdan Dec 30 '13 at 23:34
  • 1
    @Jordan - It is not the reason this is not working. Javascript does not require semicolon. It uses semicolon to separate lines, but since there is a newline before the alert, it won't matter. This is not the source of the problem OP is facing, hence not an answer for this question. – OptimusCrime Dec 30 '13 at 23:35
  • This is the solution, the ; after the var length = , so yes, for sure it is the answer! – Jordy Dec 30 '13 at 23:35
  • @halfdan There are many cases in JS where you do not need a semicolon, much more so than other languages. There is a fair amount of debate on whether or not you should use them. – Jordan Dec 30 '13 at 23:36
  • @OptimusCrime I am well aware. However, this answer and the first comment _do_ answer the question, and the OP _was_ missing a critical semicolon on this line. Therefore, in my mind, this answer solves the problem, and educates people who come to this problem later as to the difference placing a semi-colon before the `?>` vs after can make to a program. – Jordan Dec 30 '13 at 23:38
  • from [the docs](http://php.net/manual/en/language.basic-syntax.instruction-separation.php): "The closing tag of a block of PHP code automatically implies a semicolon; you do not need to have a semicolon terminating the last line of a PHP block." – Zach Lysobey Dec 30 '13 at 23:50
1
                var length = <?php echo $strlen ?>

Maybe change it to

var length = <?php echo $strlen ?> ;
0

json_encode expects an array as input parameter, you supply it with a int value (length of a string).
You don't really need to convert the int value to a json object, $strlen = strlen($_POST['message']); will probably be enough.

You are also missing a semicolon in this like:

var length = <?php echo $strlen ?>
//Should be:
var length = <?php echo $strlen; ?>
//Or even:
var length = <?php echo $strlen; ?>;
Jite
  • 5,761
  • 2
  • 23
  • 37
  • Tried that man, isn't the solution, it still displays 0 as value, and not the actual value it has to show. It does work on the php itself, but in the javascript it just doesnt. – Jordy Dec 30 '13 at 23:28
  • Well, is the length of `$_POST['message']` 0? – Jite Dec 30 '13 at 23:29
  • 1
    @JAAulde Thank you for the info, I will remove that part of my answer, aperantly the json_encode part was not the issue. :) – Jite Dec 30 '13 at 23:37
  • No the length is not 0, I checked that offcourse ;), but I got it sorted out now, the semicolumn fixed it, kinda weird that that messed up the code tho, as semicolumns are not a must do right in javascript? – Jordy Dec 30 '13 at 23:37
  • @user3122479 You should always terminate your statements properly with a semi-colon, you'll find that statement literally in every javascript book in the world. And if your problem was fixed, be so kind to accept and/or upvote this answer. – nietonfir Dec 30 '13 at 23:44
  • 1
    @Jordan from [the docs](http://php.net/manual/en/language.basic-syntax.instruction-separation.php): "The closing tag of a block of PHP code automatically implies a semicolon; you do not need to have a semicolon terminating the last line of a PHP block." – Zach Lysobey Dec 30 '13 at 23:50
0

Put it in quotes. Worked for me.

var length ="<?php echo $strlen ?>";