0

I'm attempting to put a php variable inside a javascript function and having no luck, here's what I got

<a class="code" href="javascript:void(0);"
 onclick="javascript:if(window.dd && dd.elements) 
 d.elements.name1.moveTo(<? echo "$xpos"; ?>, <? echo "$ypos"; ?>);
 return false;">
name1.moveTo(name1.x-20, name1.y+7);</a>

`

the moveTo() function works perfectly when I send it a javascript variable or simple numbers.

the reason its in a php variable at all is because I need the xpos to be inside a session variable to be accessed in other places. Afterwards I assign it as follows

$_SESSION['productcheck']['x'] = $xpos;

I'm kinda new to this, if you haven't already noticed, Thank you ahead of time :)

jrharshath
26.7k34 gold badges99 silver badges128 bronze badges
asked Jan 4, 2010 at 2:28
8
  • tried view html source? You might want to see what $xpos and $ypos returns. Commented Jan 4, 2010 at 2:30
  • thank u for such a quick response i have tried echoing the variable and it does return the proper number, i should have also mentioned that originally the variable is assigned from a $_GET which is sent in the prev page Commented Jan 4, 2010 at 2:35
  • something wrong with the quotation maybe? Commented Jan 4, 2010 at 2:38
  • Can you post the HTML source? Ie, go to the page, then copy+paste the <a class="code" href="javascript:void(0);" onclick="javascript:if(window.dd &amp;&amp; dd.elements) d.elements.name1.moveTo(<? echo "$xpos"; ?>, <? echo "$ypos"; ?>); return false;"> Commented Jan 4, 2010 at 2:40
  • yup, need to see the generated source. Commented Jan 4, 2010 at 2:42

3 Answers 3

2

try not putting double quotes.

echo $xpos;
answered Jan 4, 2010 at 2:48
Sign up to request clarification or add additional context in comments.

Comments

2

This is just to clarify, but you seem to have a typo (d should be dd). Corrected:

<a class="code" href="javascript:void(0);"
 onclick="return (function () {
 if(window.dd && dd.elements) 
 dd.elements.name1.moveTo(<? echo $xpos; ?>, <? echo $ypos; ?>);
 return false; 
 })()"
>
 name1.moveTo(name1.x-20, name1.y+7);
</a>

Some issues:

  • You don't need PHP variable interrpolation, $xpos by itself is fine
  • onclick should have only one expression that returns false, so you'd ideally wrap it in a function elsewhere. Here I used an anonymous one

Also, onclick need not start with 'javascript:, since it already is implicitly so.

answered Jan 4, 2010 at 3:03

1 Comment

Assuming dd is a form name you also should not use window.dd or dd-as-variable name. This will only work (sometimes) on IE. Use document.forms.dd.elements. Finally, for sanity's sake consider moving the multi-line handler function out into a <script> block or external script, assigning with link.onclick= function() { ... return false; }. href="#" is also less ugly than the javascript: pseudo-URL. (Always avoid javascript:.)
0

My guess would that xpos and ypos are not in scope at the time that part of the page is processed.

Scope refers to the enclosing braces. for example the following will not work

$xpos = 100;
function printx(){
 echo $xpos; // enclosing quotes not required
}
// call the function
printx();

Nothing will be printed

to fix it use the following

$xpos = 100;
function printx(){
 global $xpos;
 echo $xpos; // enclosing quotes not required
}
// call the function
printx();

this would then print 100

answered Jan 4, 2010 at 2:44

1 Comment

Just to enhance the above answer you can also use the syntax <?=$xpos ?> to print out the xpos but scope still matters and it won't work in some installations (due to a php.ini setting) So I personally don't recommend this method

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.