Jordy
Jordy

Reputation: 1049

Passing PHP code to Javascript won't work

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.

Upvotes: 0

Views: 192

Answers (6)

Zach Lysobey
Zach Lysobey

Reputation: 15724

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 ?>;

Upvotes: 6

Jite
Jite

Reputation: 5847

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; ?>;

Upvotes: 0

BillyJMcDonald
BillyJMcDonald

Reputation: 71

Put it in quotes. Worked for me.

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

Upvotes: 0

Nitzan Raber
Nitzan Raber

Reputation: 36

                var length = <?php echo $strlen ?>

Maybe change it to

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

Upvotes: 1

MillaresRoo
MillaresRoo

Reputation: 3848

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

Upvotes: 1

OptimusCrime
OptimusCrime

Reputation: 14863

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';

...

Upvotes: 1

Related Questions