-1

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.

5
  • json_encode favors a two-dimensional array which holds the key and value of a JSON list/dictionary. Commented Dec 30, 2013 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 Commented Dec 30, 2013 at 23:38
  • @user3122479 make sure to mark the "correct" answer (by clicking the checkmark) Commented Dec 30, 2013 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. Commented Dec 30, 2013 at 23:43
  • 1
    @Allendar, json_encode does not "prefer" any such thing: us1.php.net/json_encode Commented Dec 30, 2013 at 23:45

6 Answers 6

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 ?>;
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for clearing up some misconceptions (mine included :P) OP, if you are still hanging around, this has my vote for accepted answer.
Yes, this is correct and thouroughly explains why. Upvote it, accept it, and etc.
Ah thanks for the great explaination, exactly what I needed. This sums it all up pretty much. THANKS!
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';

...

1 Comment

Damn, I forgot one ; and it messed up my whole code, thanks tho !
1

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

8 Comments

rather inside the <?php echo $strlen; ?>, js dont require semicolon (altho I do prefer it personally).
@OptimusCrime If it solves the problem, it most certainly is an answer.
@Jite, JS very much requires a semicolon.
@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.
This is the solution, the ; after the var length = <?php echo $strlen ?>, so yes, for sure it is the answer!
|
1
                var length = <?php echo $strlen ?>

Maybe change it to

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

1 Comment

Yes that was the problem ^^
0

Put it in quotes. Worked for me.

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

1 Comment

This will just convert the value to a string.
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; ?>;

6 Comments

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.
Well, is the length of $_POST['message'] 0?
@JAAulde Thank you for the info, I will remove that part of my answer, aperantly the json_encode part was not the issue. :)
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?
@Jordan from the docs: "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."
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.