Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uncaught ReferenceError: testString is not defined

This is really weird..

I need to send a couple of variables through to jquery from PHP.. one is an INT and the other a string.

When $a is an INT it works fine but when i use a string, i get this error.. Uncaught ReferenceError: testString is not defined

Here is my code.

<?php $a = 'testString'; ?>

<script type="text/javascript">
    var a = <?php echo $a; ?>;
    alert(a);
</script>

I assumed that i needed to stick a (int) or (string) before the variable, but i wasn't entirely sure how to and unsuccessful in my googles/attempts.

Any ideas?

like image 876
user2950370 Avatar asked Jun 16 '26 14:06

user2950370


1 Answers

You forgot the quotes to make the value of var a a string:

var a = "<?php echo $a; ?>";

What you're writing into the document is:

var a = testString;

so javascript is looking for a variable called testString. Instead, you want the result to be:

var a = "testString";

so make sure you include the quotes around what php is writing in.

like image 159
m59 Avatar answered Jun 19 '26 03:06

m59



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!