Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #9475 > unrolled thread

Problem with quotes in javascript output

Started byrichard <noreply@example.com>
First post2013-05-20 12:17 -0400
Last post2013-05-21 13:59 +0200
Articles 5 — 4 participants

Back to article view | Back to comp.lang.php


Contents

  Problem with quotes in javascript output richard <noreply@example.com> - 2013-05-20 12:17 -0400
    Re: Problem with quotes in javascript output Jerry Stuckle <jstucklex@attglobal.net> - 2013-05-20 12:53 -0400
    Re: Problem with quotes in javascript output richard <noreply@example.com> - 2013-05-20 14:12 -0400
    Re: Problem with quotes in javascript output Denis McMahon <denismfmcmahon@gmail.com> - 2013-05-21 10:47 +0000
      Re: Problem with quotes in javascript output Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-05-21 13:59 +0200

#9475 — Problem with quotes in javascript output

Fromrichard <noreply@example.com>
Date2013-05-20 12:17 -0400
SubjectProblem with quotes in javascript output
Message-ID<54lw0fd343qv$.gm5ydjjd60j9$.dlg@40tude.net>
This is not a javascript question ok?
First, the desired look of my endeavor is here.
www.mroldies.net/test1.html

What I am working on is here.
www.mroldies.net/test2.php

The problem mainly deals with required quotes in the javascript output.
Either single or double.

The code below shows you what I have been trying.
The output code shows the text with the variable names, not the desired
output.

Do not run the code locally as the key will prevent it.



<!DOCTYPE html>
<html><head>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
<script src="jwplayer/jwplayer.js"></script>
  <script>jwplayer.key="NIV";</script>
</head>

<body>
<div id='my-video'></div>
<script type='text/javascript'>

jwplayer("my-video").setup({
    playlist: [{
<?php
/* connect to the db */
$con = mysql_connect('localhost','user','pass');


if (!$con){die("can not connect: " . mysql_error());}

mysql_select_db('richbull_top100',$con);

$year=1960;
$number=1;

$result = mysql_query("SELECT atitle,artist,avid FROM A$year WHERE id =
$number");
if (!$result) { echo 'Could not run query: ' . mysql_error();  exit; }
$vid = mysql_fetch_row($result);


echo "{";

echo 'file: "http://www.youtube.com/watch?v=$vid[2]"';
echo ",";
echo 'description: "$year # $number - $vid[1]"';

echo "}";





?>
	],

        "startparam":"start",
        "repeat":"list",
        "width":800,
        // adjust these 2 lines if you'd like to omit/resize playlist, etc:
        listbar:{position:'right',size:320},
        "height":450
	
});
</script>

</body>
</html>

[toc] | [next] | [standalone]


#9476

FromJerry Stuckle <jstucklex@attglobal.net>
Date2013-05-20 12:53 -0400
Message-ID<kndk7h$got$1@dont-email.me>
In reply to#9475
On 5/20/2013 12:17 PM, richard wrote:
> This is not a javascript question ok?
> First, the desired look of my endeavor is here.
> www.mroldies.net/test1.html
>
> What I am working on is here.
> www.mroldies.net/test2.php
>
> The problem mainly deals with required quotes in the javascript output.
> Either single or double.
>
> The code below shows you what I have been trying.
> The output code shows the text with the variable names, not the desired
> output.
>
> Do not run the code locally as the key will prevent it.
>
>
>
> <!DOCTYPE html>
> <html><head>
> <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
> <script src="jwplayer/jwplayer.js"></script>
>    <script>jwplayer.key="NIV";</script>
> </head>
>
> <body>
> <div id='my-video'></div>
> <script type='text/javascript'>
>
> jwplayer("my-video").setup({
>      playlist: [{
> <?php
> /* connect to the db */
> $con = mysql_connect('localhost','user','pass');
>
>
> if (!$con){die("can not connect: " . mysql_error());}
>
> mysql_select_db('richbull_top100',$con);
>
> $year=1960;
> $number=1;
>
> $result = mysql_query("SELECT atitle,artist,avid FROM A$year WHERE id =
> $number");
> if (!$result) { echo 'Could not run query: ' . mysql_error();  exit; }
> $vid = mysql_fetch_row($result);
>
>
> echo "{";
>
> echo 'file: "http://www.youtube.com/watch?v=$vid[2]"';
> echo ",";
> echo 'description: "$year # $number - $vid[1]"';
>
> echo "}";
>
>
>
>
>
> ?>
> 	],
>
>          "startparam":"start",
>          "repeat":"list",
>          "width":800,
>          // adjust these 2 lines if you'd like to omit/resize playlist, etc:
>          listbar:{position:'right',size:320},
>          "height":450
> 	
> });
> </script>
>
> </body>
> </html>
>

There is no variable substitution in single quoted ('...') strings, only 
double quoted ("...") strings.

You are using single quoted strings in your PHP code.

-- 
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

[toc] | [prev] | [next] | [standalone]


#9477

Fromrichard <noreply@example.com>
Date2013-05-20 14:12 -0400
Message-ID<1vxbb5ww5trpq$.1ine07ufzjbay.dlg@40tude.net>
In reply to#9475
fixed!

The problem with the player not showing was an extra {.
so now the script works just fine.

[toc] | [prev] | [next] | [standalone]


#9497

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2013-05-21 10:47 +0000
Message-ID<knfjcj$e55$1@dont-email.me>
In reply to#9475
On Mon, 20 May 2013 12:17:26 -0400, richard wrote:

> This is not a javascript question ok?

Actually it is, because the problem seems to be that you're producing 
invalid javascipt.

> The problem mainly deals with required quotes in the javascript output.
> Either single or double.

I don't think it does, although I suspect they may be an issue.

As far as I can tell, the final argument to jwplayer("my-video").setup() 
looks like this (I've added some spaces for readability):

{ playlist : [ { { file : "http://www.youtube.com/watch?v=$vid[2]", 
description : "$year # $number - $vid[1]" } ], "startparam" : "start", 
"repeat" : "list", "width" : 800, listbar : { position : 'right', size : 
320}, "height" : 450 }

There are several things here that might cause problems:

1) Mismatched brackets. You don't have the same number of all of (, { and 
[ as you do respectively of ), } and ]. This is probably breaking the 
javascript.

2) Some attributes are quoted and others are not. Although javascript is 
fairly tolerant as to which quotes you use in that it generally treats 
single and double quoted strings identically, I suspect that either you 
should be passing either strings for all the attributes, or names for all 
the attributes, but not a mixture of strings and names. For example, 
playlist, file, description, listbar, position and size are names; and 
"startparam", "repeat", "width" and "height" are strings. This may be 
breaking the javascript.

3) Your php variable substitution isn't going to work for two reasons. 
You need something more like:

echo "file: 'http://www.youtube.com/watch?v={$vid[2]}'";
echo ",";
echo "description: '$year # $number - {$vid[1]}'";

or

echo "file: \"http://www.youtube.com/watch?v={$vid[2]}\"";
echo ",";
echo "description: \"$year # $number - {$vid[1]}\"";

This is to address that (a) php variable substitution only works in 
double quoted strings (ie when using "" and not ''), and (b) you should 
wrap constructs of $array[element] with {} when using variable 
substitution. This is probably causing the data passed to the javascript 
to not be what you think it's getting.

-- 
Denis McMahon, denismfmcmahon@gmail.com

[toc] | [prev] | [next] | [standalone]


#9499

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2013-05-21 13:59 +0200
Message-ID<1592783.kto8HJv2Jy@PointedEars.de>
In reply to#9497
Denis McMahon wrote:

> As far as I can tell, the final argument to jwplayer("my-video").setup()
> looks like this (I've added some spaces for readability):
> 
> { playlist : [ { { file : "http://www.youtube.com/watch?v=$vid[2]",
> description : "$year # $number - $vid[1]" } ], "startparam" : "start",
> "repeat" : "list", "width" : 800, listbar : { position : 'right', size :
> 320}, "height" : 450 }
> 
> There are several things here that might cause problems:
> 
> 1) Mismatched brackets. You don't have the same number of all of (, { and
> [ as you do respectively of ), } and ]. This is probably breaking the
> javascript.

ACK.  “{ { … } }” is not a valid Object initializer; the property name (like 
“{foo: { … } }”) is missing.

> 2) Some attributes

_property names_

> are quoted and others are not. Although javascript is

… non-existent, see <http://PointedEars.de/es-matrix>, but ECMAScript 
implementations are (per Specification) …

> fairly tolerant as to which quotes you use in that it generally treats
> single and double quoted strings identically, I suspect that either you
> should be passing either strings for all the attributes, or names for all
> the attributes, but not a mixture of strings and names. For example,
> playlist, file, description, listbar, position and size are names; and
> "startparam", "repeat", "width" and "height" are strings. This may be
> breaking the javascript.

It does not; quotes have always been optional for property names that are 
not reserved words and not numeric, and are completely optional since 
ECMAScript Edition 5 (2009).  However, if you are not sure whether a 
property name could be a reserved word or numeric, you should quote it for 
backwards compatibility.

> 3) Your php variable substitution isn't going to work for two reasons.
> You need something more like:
> 
> echo "file: 'http://www.youtube.com/watch?v={$vid[2]}'";
> echo ",";
> echo "description: '$year # $number - {$vid[1]}'";
> 
> or
> 
> echo "file: \"http://www.youtube.com/watch?v={$vid[2]}\"";
> echo ",";
> echo "description: \"$year # $number - {$vid[1]}\"";

It would be best if they skipped the consecutive “echo” altogether (almost 
always a bad idea to write such code; a mark of beginners) and let the 
preprocessor only work on what it has to.  That is, (in PHP 5.4)

<?php … ?>
  file: "http://www.youtube.com/watch?v=<?= addslashes($vid[2]) ?>",
  description: "<?= addslashes("$year # $number - {$vid[1]}") ?>"
<?php … ?>

In fact, it is even easier to json_encode() a PHP array as long as the leaf 
values can be expressed as ECMAScript primitive (JSON-compatible) values:

<?php … ?>
  foo(<?= json_encode(array(
    'file':        "http://www.youtube.com/watch?v={$vid[2]}",
    'description': "$year # $number - {$vid[1]}"
  )) ?>);
<?php … ?>

(The leading and trailing PHP sections are optional.)


PointedEars
-- 
    realism:    HTML 4.01 Strict
    evangelism: XHTML 1.0 Strict
    madness:    XHTML 1.1 as application/xhtml+xml
                                                    -- Bjoern Hoehrmann

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.php


csiph-web