Unexpected identifier in json_encode output - javascript

echo '<script type="text/javascript">
$(document).ready(function(){
$("video").easyWebPlayer({ playlist:\''.json_encode($playlist).'\'
});
});
</script>';
I am using the above code in a php file it produces following output.
'playlist:'[{"id":"0","url":"\/26beb276\/26beb276_0.mp4","thumb":"\/26beb276\/26beb276_0.jpg","title":"big buck test"}]'});});
It gives me error of Unexpected identifier .
How can I avoid this error.

Try below code:
<Script>
var playlist = '<?php echo $playlist; ?>';
var jsonplaylist = JSON.parse(playlist);
</Script>
echo '<script type="text/javascript">
$(document).ready(function(){ $("video").easyWebPlayer({ playlist:'+jsonplaylist+'});
</script>';

solved
I used playlist:"'.str_replace("\"","\'",json_encode($playlist)).'" while json_encode()
and use
var jsonobj=JSON.parse(Playlist.replace(/\'/g, "\""));
at the receiver end.

I had the same console error, the error was due to quotes
Before
<script>window.lang_arr="<?php echo json_encode($lang_arr);?></script>
Fix
<script>window.lang_arr=<?php echo json_encode($lang_arr);?></script>
It could help someone today :_)

Related

Echoing a mulitple line message in alert function

I need to echo multiple line error in JS alert and I am using below code
Working code
$str = "This is a error1\\n";
$alert = $str."This is error2";
if(!empty($alert)){
?>
<script type="text/javascript">
alert('<?php echo $alert;?>');
</script>
<?php
}
but I need to work on an old written code so this has addslashes function before echo alert like below:
$str = "This is a error1\\n";
$alert = $str."This is error2";
$alert = addslashes($alert);
if(!empty($alert)){
?>
<script type="text/javascript">
alert('<?php echo $alert;?>');
</script>
<?php
}
Output : This is a error1\nThis is error2
I need this to print in two lines I have also tried with only \n but it is not working then.
I'd make javascript do the hard work.
<script type="text/javascript">
var alertArr = ["<?php echo $string1; ?>", "<?php echo $string2; ?>"];
alert(alertArr.join("\n"));
</script>
https://jsfiddle.net/0uwmmm3n/
(Magnificent random url by the way)
EDIT: less messy version, maybe more legitimate
<?php $alertArr = [$string1, $string2]; ?>
<script type="text/javascript">
var alertArr = <?php echo json_encode($alertArr); ?>;
alert(alertArr.join("\n"));
</script>
RE-EDIT: nah, too much work for a job so simple, two conversions is way too much
$str = "This is a error1\\n";
$alert = $str."This is error2";
$alert = addslashes($alert);
if(!empty($alert)){
?>
<script type="text/javascript">
alert('<?php echo $alert;?>');
</script>
<?php
}
I have used this code and it working fine according to your need
if still issue continues then clear your cache

Show PHP variable in JS alert

I am trying to alert message but it's not working with my string.
echo "<script>alert('$lang['ALERT']');
window.location.href ='index.php';
</script>";
Not working... Help
You're mixing quotes. Keep it simple,
echo "<script>alert('".$lang['ALERT']."');
window.location.href ='index.php';
</script>";
try it.
<script>
var alertMsg = "<?php echo $lang['ALERT'] ?>";
alert(alertMsg);
window.location.href ='index.php';
</script>;
You're not properly quoting your variable
<?php
$lang['ALERT'] = 'mystring';
echo "<script>alert('".$lang['ALERT']."');window.location.href ='index.php';</script>";
?>
Here's the eval
Try this one
<script>
alert(<?=$lang['ALERT']; ?>);
window.location.href ='index.php';
</script>
quotes is the problem
<?php $alert=$lang['ALERT'];?>
<script>
alert('<?php echo $alert?>');
window.location.href ='index.php';
</script>;

How to Pass a PHP array to JavaScript function?

First of all, this question looks a duplicate of Pass a PHP array to a JavaScript function, but I actually used the first solution of Pass a PHP array to a JavaScript function - and it doesnt seem to work:
More specifically, the php echo line in the code below seems to create erroneous js output according to console error message( Uncaught SyntaxError: Unexpected token <); the console shows created html starting with "var s1 = <br />
<font size='1'><table class='xdebug-error xe-notice' dir='ltr' border='1'..."
...while I'd expect a clean var s1 = [1,2,3,4,5,6,7,8,9] - the result I also see when I tested the echo line in ideone.com
Any idea why the echo line is creating this stuff, and how to fix this?
Related joomla php code:
<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
$document = JFactory::getDocument();
//add jqplot libraries
JHtml::_('jquery.framework');
JHTML::script(JUri::root() .'media/system/js/jquery.jqplot.min.js');
//$document->addStyleSheet(JUri::root() .'media/system/js/jquery.jqplot.min.css');
JHtml::stylesheet( JUri::root() . 'media/system/js/jquery.jqplot.min.css' );
JHTML::script(JUri::root() .'media/system/js/jquery.jqplot.min.css');
JHTML::script(JUri::root() .'media/system/js/jqplot.barRenderer.min.js');
JHTML::script(JUri::root() .'media/system/js/jqplot.categoryAxisRenderer.min.js');
JHTML::script(JUri::root() .'media/system/js/jqplot.pointLabels.min.js');
JHTML::script(JUri::root() .'media/system/js/jqplot.enhancedLegendRenderer.js');
JHTML::script(JUri::root() .'media/system/js/weqlib.js');
$chartvals = array(1,2,3,4,5,6,7,8,9);
?>
<head>
<script type="text/javascript">
jQuery(document).ready(function(){
var s1 = <?php echo json_encode(chartvals); ?>; //!the echo seems to create erroneous js output accoding to console(?)
plot1 = jQuery.jqplot ('chart1', [s1]); //copied from example at
}); //$(document).ready
</script>
</head>
You forgot the $ to referrence the chartvals var at the json_encode() function call:
var s1 = <?php echo json_encode(chartvals); ?>;
Should be
var s1 = <?php echo json_encode($chartvals); ?>;
To not trap into this sort of mistakes again you can add error_reporting(E_ALL); to the beginning of your script and set display_errors to on in your PHP config during development.
var s1 = <?php echo json_encode($chartvals); ?>; //!the echo seems to create

PHP How output string to html

After print php string in js code I get error
<script type = "text/javascript">
var editorHtml = "<?php echo $editorHtml; ?>";
</script>
When the string contains " double-quotes, I get Unexpected token ILLEGAL, because the double-quotes close the JS string in the middle.
<script type = "text/javascript">
var editorHtml = '<?php echo $editorHtml; ?>';
</script>
I try this, but i get similar problem, if the string contains ' single-quotes.
I need a unuiversal solution for this, when the string contains ' AND " quotes.
<script type = "text/javascript">
var editorHtml = <?php echo str_replace(array(']]>', '<!'), array(']]\x3E', '\x3C!'), json_encode($editorHtml)); ?>;
</script>
Sanitizes in all cases properly.
Look an example:
<?php
$someVar = 1;
?>
<script type="text/javascript">
var javaScriptVar = "<?php echo $someVar; ?>";
</script>

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?
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.
There are quotes missing in javascript code:
<script type="text/javascript">
var a = '<?php echo $a; ?>';
alert(a);
</script>

Categories