simple js to php conversion

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
alt303
Forum Newbie
Posts: 6
Joined: Sat Aug 23, 2008 6:12 pm

simple js to php conversion

Post by alt303 »

Hello all. I need some help with converting this javascript to php.

Code: Select all

 
var unknownlinks=new Array()
 
unknownlinks[0]="http://google.com"
unknownlinks[1]="http://yahoo.com"
 
function unknownlink(){
parent.main.window.location=unknownlinks[Math.floor(Math.random()*unknownlinks.length)]
}
 
When this button is clicked it shows a random link in the iframe called "main":

Code: Select all

 
<input type="button" name="button" value="button" onclick="unknownlink()">
 
Any simple way to eliminate the javascript and get the exact functionality with php code? Thanks a lot.
User avatar
it2051229
Forum Contributor
Posts: 312
Joined: Tue Dec 25, 2007 8:34 pm

Re: simple js to php conversion

Post by it2051229 »

parent.main.window.location <--- there's no PHP conversion for this. That means that the JS code cannot be fully converted into PHP but some parts only...
alt303
Forum Newbie
Posts: 6
Joined: Sat Aug 23, 2008 6:12 pm

Re: simple js to php conversion

Post by alt303 »

Thanks for the info. Is there no alternative ?
User avatar
it2051229
Forum Contributor
Posts: 312
Joined: Tue Dec 25, 2007 8:34 pm

Re: simple js to php conversion

Post by it2051229 »

for plain full php code.. nope.. but mixture of php and js is possible.. try some experiments and from there you will know how..
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: simple js to php conversion

Post by Eran »

It's quite possible by submitting a form into a php script which sends redirect headers.
User avatar
Ziq
Forum Contributor
Posts: 194
Joined: Mon Aug 25, 2008 12:43 am
Location: Russia, Voronezh

Re: simple js to php conversion

Post by Ziq »

Code: Select all

 
<?php
if (!empty($_GET['go']))
{
    $unknownlinks[0]="http://google.com";
    $unknownlinks[1]="http://yahoo.com";
     
    header('Location: '.$unknownlinks[rand(0, count($unknownlinks) - 1)]);
}
?>
<form action="<?=$_SERVER['SCRIPT_NAME']?>">
<input type="submit" name="go" value="go">
</form>
 
Post Reply