Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Javascript with php and parameter questions

Status
Not open for further replies.

dmkFoto

Programmer
Jul 9, 2007
20
US
Can you write php code inside a javascript function?
How do you pass parameter into a javascript function like the one below?
I want to change the file name "Header.php" dynamically. Can this be done?


<SCRIPT language="JavaScript1.2">
function alert()
{
testwindow= window.open ("Header.php", "mywindow",
"location=1,status=1,scrollbars=1,width=400,height=300");
testwindow.moveTo(200,400);
}
</SCRIPT>
Thanks.

Alex
 
Yes it csn be done, but be aware that all the PHP will be run before the JS is run:

Code:
<SCRIPT language="JavaScript1.2">
   function alert() {
      testwindow= window.open ("<? echo $yourPHPvar ?>", "mywindow", "location=1, status=1, scrollbars=1, width=400, height=300");
      testwindow.moveTo(200,400);
   }
</SCRIPT>

If you need more help with outputting PHP to the page, I'd ask in the PHP forum.

Also, I'd refrain from calling your function "alert", as that could interfere with JavaScript's "alert" method.

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Is there any want to change the value of the variable in the following click before calling the javascript function?

<p>
<A href="javascript: alert('Clicked on link!')">Click
here!</A></p>

Thanks.

Alex
 
You'll want to setup your links dynamically. If i understand what you're wanting to do correctly, then is not possible, that is using a PHP variable inside Javascript and changing it later on the page. You have to realize PHP is simply helping the page display, but once PHP has done it's job, and the page has loaded, there's nothing else it can do until the page loads again (or you use some sort of AJAX function). You want to set your link up dynamically like this:

Code:
<p><A href="javascript: alert('Clicked on link!<?php echo "and did this too"; ?>')">Click
here!</A></p>

Using this method, you're able to pass different variables to your Javascript using different links. So to go along with your example:
Code:
<SCRIPT language="JavaScript1.2">
function alert(popupURL) {
testwindow= window.open (popupURL, "mywindow",
    "location=1,status=1,scrollbars=1,width=400,height=300");
testwindow.moveTo(200,400);
}
</SCRIPT>

<p><A href="javascript: alert('<?php echo $URLvariable; ?>')">Click
here!</A></p>

I would also recommend changing the name of your function "alert" since it's already a reserved Javascript function which pops up a message box.

Hope that helps.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top