Connecting Tech Pros Worldwide Forums | Help | Site Map

Use php to "echo" javascript on a web page

eholz1
Guest
 
Posts: n/a
#1: Feb 26 '07
Hello Group,

does this make sense?? I have some java code that responds to
keypress events (the arrow keys)

<script language="javascript">
<!--

function keypressed(e)
{
if (e.keyCode == 37) { document.location.href = 'images/?p=100'; }
else if (e.keyCode == 39) { document.location.href = 'viewpix.php?
p=1009&s=1'; }
}

The trick is the document.location.href can (and should) change as i
navigate thru image choices.
it is possible to use 37 to go back to an image document.location.href
= 'viewpix.php?p=100&s=1';
and 39 to go to next image: document.location.href = 'viewpix.php?
p=100&s=3';

this is a case where the image displayed is: document.location.href =
'viewpix.php?p=100&s=2';

I was thinking about making a php function that uses variables for the
href code , and then
echoing a string like this: (with appropriate dink marks, etc)
define these variables:
$back = 'images/?p="$p"';
$i = some number form $_REQUEST['id'];
$next = 'viewpix.php?p=$p&s="$i"';


echo '<script language="javascript">
<!--

function keypressed(e)
{
if (e.keyCode == 37) { document.location.href =
'$back'; }


}';
I would set the variable values, and then write the code in the page.

Any tips will help. the href stuff would change as the image changes.

Thanks,

eholz1


Colin McKinnon
Guest
 
Posts: n/a
#2: Feb 26 '07

re: Use php to "echo" javascript on a web page


eholz1 wrote:
Quote:
I was thinking about making a php function that uses variables for the
href code , and then
echoing a string like this: (with appropriate dink marks, etc)
define these variables:
$back = 'images/?p="$p"';
You won't interpolate $p if its inside single quotes - assuming $p is a php
variable which holds some value:

$back="images/?=\"$p\"";

but really those quotes should be urlencoded (although since you'll be
reading the value back into PHP, I can't imagine why you need/want the
quotes at all).
Quote:
$next = 'viewpix.php?p=$p&s="$i"';
>
Lose the "" around $i, use double quotes around the whole string.

You seem to have a basic grasp of programming - take the time to read
sections I, III & IV of the manual properly.

C.
eholz1
Guest
 
Posts: n/a
#3: Feb 27 '07

re: Use php to "echo" javascript on a web page


On Feb 26, 2:27 pm, Colin McKinnon
<colin.thisisnotmysurn...@ntlworld.deletemeunlessU RaBot.comwrote:
Quote:
eholz1 wrote:
Quote:
I was thinking about making a php function that uses variables for the
href code , and then
echoing a string like this: (with appropriate dink marks, etc)
define these variables:
$back = 'images/?p="$p"';
>
You won't interpolate $p if its inside single quotes - assuming $p is a php
variable which holds some value:
>
$back="images/?=\"$p\"";
>
but really those quotes should be urlencoded (although since you'll be
reading the value back into PHP, I can't imagine why you need/want the
quotes at all).
>
Quote:
$next = 'viewpix.php?p=$p&s="$i"';
>
Lose the "" around $i, use double quotes around the whole string.
>
You seem to have a basic grasp of programming - take the time to read
sections I, III & IV of the manual properly.
>
C.
C et All,

Thanks for reminding me about the double quotes, cleaner.
I will check the manual (I assume it is the on-line PHP manual)

Thanks,

eholz1

Closed Thread