1

It's a simple embed and I don't understand why it's getting this error.

In the action class...

$TESTME = "what";

In the view....

<script type="text/javascript">
    $(document).ready(function () {
        var someVale = "<?php echo $TESTME; ?>";
        alert(someVale);
    });
</script>

The error is pointing to right after the var assignment first quote

ie. var someVale = "< br />....

3
  • 1
    if $TESTME has " things will crash and burn :) Commented Jan 24, 2013 at 15:24
  • That looks like a php error message, I'm thinking undefined variable. Commented Jan 24, 2013 at 15:31
  • possible duplicate of javascript Syntax error : Unterminated string literal Commented Mar 31, 2013 at 11:40

2 Answers 2

4

You need to examine the content of $TESTME. It either contains newline characters or double quotes. The error you're seeing usually indicates that the string in question is broken over several lines, or that the number of quotes don't match up.

In your case, it's probably newlines...

var someVale = "< br />
<tag>
<tag>
<tag>";

This obviously won't work, and you need to deal with the string so that you end up with...

var someVale = "< br />\n<tag>\n<tag>\n<tag>";

You can convert your PHP variable with something like...

$TESTME = str_replace(chr(13), "\n", $TESTME);

(Depending on the OS involved, your newlines may also be chr(13) . chr(10).)

Sign up to request clarification or add additional context in comments.

7 Comments

I define $TESTME right there. it only consists of "what". Where could it become corrupted? I also tried making it a number. Same error
Nice catch on the newline. He should also do a nl2br().
It'd probably be a good idea to just use a JSON encoding facility.
nl2br() did not work json_encode did not work str_replace technique did not work. Any other ideas?
You still need to visually inspect the contents of $TESTME. If you post the actual content here, we can provide better assistance. Even so, it'll give you a better idea of what the issue may be. You should also look for errant "s in the string. Try modifying your code to var someVale = '<?php echo $TESTME; ?>';.
|
0

Most of the time, you can get away with echo-ing variables, but sometimes those echoed strings contain line terminators, or quotes (aka string delimiters). You can test, and test again, but you just have to defend yourself against "malicious" and "unpredictable" input. In this very answer I've used both single and double quotes
You can str_replace or urlencode your strings, which would solve your problems, but honestly... what on earth is wrong with json_encode? It's just perfect for Server <-> client data, like you're using:

var someVal = JSON.parse(<?= json_encode(array('data' => $someVar));?>).data;

All chars that need escaping will be escaped... job done, and with a "native" PHP function.

Update:
As the comments below show, this is probably a PHP error, due to a scope issue. Instead of declaring a variable in the class, you should declare a property:

class Foo
{
    public $theProperty = null;
    public function __construct($argument = null)
    {
        $this->theProperty = $argument;//assign a variable, passed to a method to a property
        $someVar = 123;//this variable, along with $argument is GC'ed when this method returns
    }
}
//end of class
$instance = new Foo('Value of property');
echo $instance->theProperty;//echoes "value of property"
$anotherInstance = new Foo();//use default value
if ($anotherInstance->theProperty === null)
{//is true
    echo 'the property is null, default value';
    $anotherInstance->theProperty = 'Change a property';
}

This is, basically how it works. I don't know how you're using your view-script, so the code below might not work in your case (It's what you can do in Zend Framework, in the controller):

public function someAction()
{
    $instance = new Foo('Foobar');
    $this->view->passedInstance = $instance;//pass the instance to the view
}

Then, in your viewscript, you'd do something like this:

var someVal = JSON.parse('<?= json_encode(array('data' => $this->passedInstance->someProperty)); ?>').data;

But in order for my answer to work in your case, I'd have to see how you're rendering the view... are you using a framework? Are you using the classic MVC pattern, or is the view-script just something you include?

6 Comments

@James show the entire script tag(the generated code) where you get the error. It looks to me as you get a php error, which then causes the js error.
@James: ?? What do you mean? You wrote the PHP code yourself, right? Also, I've just read your question again... am I right in saying you're echo-ing a PHP variable in a view-script, that was declared in a class? In that case: You're out of scope/luck... classes are "self-contained", you can only access public properties, not the variables.
Soo. I think you're right and it's a scope problem. How do I get that php variable into the view?!?!
pass the instance of that class to the view, assign the variable as a property, and access it like that... I'll update my answer
problem solved. I am new with symfony and I had to have $this->TESTME. Thanks!
|

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.