Page 1 of 1

Passing variables to Javascript

Posted: Wed Nov 13, 2002 7:18 am
by jonat8
Sorry to be a pain (again!) but I've read the sticky on passing variables to JavaScript but still can't get it to work.

The code I'm using is as follows. The variable I want to pass to JavaScript is pulled from an external file using "include 'xxx'"

Code: Select all

<?php
include "config.inc.php";


echo("<b>Search Results: Click on the file's title to start the player.</b><br><br>");

$dbconn = mysql_connect($sServer, "$sUser", "$sPassword");
$result = mysql_select_db($sDB, $dbconn);
if ( $result == false )
{
	echo mysql_error();
} else {
	$sql = "SELECT * from dalmedia WHERE ".$D1." = '".$searchcriteria."'";

	$result = mysql_query( $sql );
	if ( $result != false )
	{
		while ( $data = mysql_fetch_assoc( $result ) )
		{
			echo 'Track Details: '.
			$dataї'artist'].' (<a href="javascript:playMedia('.
			$dataї'filename'].')"><b>'.
			$dataї'trackname'].'</a></b>)';
		}
	} else {
		echo ("Welcome, please enter your search criteria below.");
	}
}

echo '<script language="JavaScript">
<!--
	function playMedia(filename) {
		WindowsMediaPlayer.FileName = $sPath + filename
	}
//-->
</script>';
?>
When I open the page, search for something then click to start the file playing, JavaScript tells me that sPath is undefined. I've tried a couple of variations on that, as shown in the example including using:
WindowsMediaPlayer.FileName = .$sPath. + filename
but that doesn't work either, when I load the page it tells me I have a syntax error.

Sorry for being a pain :oops: but does anyone have any idea why this couldn't be working. Just a point, the WindowsMediaPlayer control that the script is referencing to is outside the ?> of PHP.

jonat8

Posted: Wed Nov 13, 2002 7:27 am
by twigletmac
PHP does not parse strings placed in single quotes, that means that in this part of your script:

Code: Select all

echo '<script language="JavaScript"> 
<!-- 
   function playMedia(filename) { 
      WindowsMediaPlayer.FileName = $sPath + filename 
   } 
//--> 
</script>';
PHP ignores everything between the quotes so instead of $sPath being replaced with its value you just get $sPath echoed literally. You can quickly change this by changing the code to:

Code: Select all

echo '<script language="JavaScript"> 
<!-- 
   function playMedia(filename) { 
      WindowsMediaPlayer.FileName = '.$sPath.' + filename 
   } 
//--> 
</script>';
Mac

Posted: Wed Nov 13, 2002 7:43 am
by jonat8
Hi, thanks for replying.

That didn't work. The page showed up as loading without errors but once I searched for something and it returned a result, Internet Explorer gave me error: Expected ';'. Line 7, Char 37.

The page doesn't by default return any results the first time you open it, you have to search using a form on the page which submits back to itself, then that returns results. The error only occurs when the form returns a result.

Time for a cut and paste job? This is the entire code of the document, it's a bit of a long read... sorry, because of all the WMP variables having to be set.

If I put a -> ; at the end of

Code: Select all

WindowsMediaPlayer.FileName = '.$sPath.' + filename
this line, then it still gives me the same error.

Any ideas greatly appreciated!
jonat8

Code: Select all

<? include "header.inc.php" ?>

<?php

include "config.inc.php";


echo("<b>Search Results: Click on the file's title to start the player.</b><br><br>");

$dbconn = mysql_connect($sServer, "$sUser", "$sPassword");
$result = mysql_select_db($sDB, $dbconn);
if ( $result == false )
&#123;
	echo mysql_error();
&#125; else &#123;
	$sql = "SELECT * from dalmedia WHERE ".$D1." = '".$searchcriteria."'";

	$result = mysql_query( $sql );
	if ( $result != false )
	&#123;
		while ( $data = mysql_fetch_assoc( $result ) )
		&#123;
			echo 'Track Details: '.
			$data&#1111;'artist'].' (<a href="javascript:playMedia('.
			$data&#1111;'filename'].')"><b>'.
			$data&#1111;'trackname'].'</a></b>)';
		&#125;
	&#125; else &#123;
		echo ("Welcome, please enter your search criteria below.");
	&#125;
&#125;

echo '<script language="JavaScript">
<!--
	function playMedia(filename) &#123;
		WindowsMediaPlayer.FileName = '.$sPath.' + filename
	&#125;
//-->
</script>';

?>

<center>
<img src="http://www.lovelarisa.org/exphorsa.gif" width=300 height=10>
</center>



<br><br>
<object classid="clsid:22D6F312-B0F6-11D0-94AB-0080C74C7E95" width="287" height="228" id="WindowsMediaPlayer" border="1">
  <param name="AudioStream" value="-1">
  <param name="AutoSize" value="0">
  <param name="AutoStart" value="-1">
  <param name="AnimationAtStart" value="-1">
  <param name="AllowScan" value="-1">
  <param name="AllowChangeDisplaySize" value="-1">
  <param name="AutoRewind" value="0">
  <param name="Balance" value="0">
  <param name="BufferingTime" value="15">
  <param name="ClickToPlay" value="-1">
  <param name="CursorType" value="0">
  <param name="CurrentPosition" value="-1">
  <param name="CurrentMarker" value="0">
  <param name="DisplayBackColor" value="0">
  <param name="DisplayForeColor" value="16777215">
  <param name="DisplayMode" value="0">
  <param name="DisplaySize" value="4">
  <param name="Enabled" value="-1">
  <param name="EnableContextMenu" value="0">
  <param name="EnablePositionControls" value="-1">
  <param name="EnableFullScreenControls" value="0">
  <param name="EnableTracker" value="-1">
  <param name="InvokeURLs" value="-1">
  <param name="Language" value="-1">
  <param name="Mute" value="0">
  <param name="PlayCount" value="1">
  <param name="PreviewMode" value="0">
  <param name="Rate" value="1">
  <param name="SelectionStart" value="-1">
  <param name="SelectionEnd" value="-1">
  <param name="SendOpenStateChangeEvents" value="-1">
  <param name="SendWarningEvents" value="-1">
  <param name="SendErrorEvents" value="-1">
  <param name="SendKeyboardEvents" value="0">
  <param name="SendMouseClickEvents" value="0">
  <param name="SendMouseMoveEvents" value="0">
  <param name="SendPlayStateChangeEvents" value="-1">
  <param name="ShowCaptioning" value="0">
  <param name="ShowControls" value="-1">
  <param name="ShowAudioControls" value="-1">
  <param name="ShowDisplay" value="0">
  <param name="ShowGotoBar" value="0">
  <param name="ShowPositionControls" value="-1">
  <param name="ShowStatusBar" value="-1">
  <param name="ShowTracker" value="-1">
  <param name="TransparentAtStart" value="0">
  <param name="VideoBorderWidth" value="0">
  <param name="VideoBorderColor" value="0">
  <param name="VideoBorder3D" value="0">
  <param name="Volume" value="-410">
  <param name="WindowlessVideo" value="0">
</object><br>
<b><u>Search for media below. Or, <a href="showall.php">click here to show all available media</a></u></b><br>
<form action="test.php" method="post">
<select name="D1" size="1">
<option selected value="trackname">trackname</option>
<option value="artist">artist</option>
<option value="albumname">albumname</option>
</select>
 = 
<input type="text" name="searchcriteria">
<input type="submit">
</form>

<br><br>

<? include "footer.inc.php" ?>
</center>
?>

trry this

Posted: Wed Nov 13, 2002 1:30 pm
by phpScott
First of all I think javaScript is a real pain but I am forced to use way too often for my taste.
That rant aside when I have to echo values into my js code is usally try to leave as much of the js code in the html page as possible and then to do something like the following.

Code: Select all

<head>
<script language="JavaScript">
<!--
   function playMedia(filename) &#123;
      WindowsMediaPlayer.FileName = <?php echo $sPath?> + filename;
   &#125;
//-->
</script>
</head>
I prefer this method because if I am having trouble with my js I can quickly substitute in a value where the php should be.

phpScott