PDA

View Full Version : help with javascript to php please



dorello
04-27-2010, 08:31 PM
Hi everybody,

can you please help me with this javascript? Id like to make a php script out of it.

<script type="text/javascript">
window.onload=function() {
if (document.getElementById) {
document.getElementById('form1').onsubmit=function() {
return getURL(document.getElementById('url').value);
}
}
}
function getURL(val){
if ((val=='')||(val=='http://www.')) { alert('Please enter a valid URL including http://www.'); }
else {
val = val;
var win=open(val);
}
return false;
}
</script>

If somebody could help me out, Ill say loud THANK YOU VERY MUCH !

bluewalrus
04-27-2010, 09:05 PM
This should be in the PHP forum but anyway what is the objective of this? Have an address entered in a form and load it if it is valid, validation being it contains http://www.

dorello
04-27-2010, 11:34 PM
The purpose is to load a form from html. Anyway, it works as a js but I wanted to load it as a php file.

bluewalrus
04-28-2010, 12:22 AM
Something like


<?php
if (isset($_POST['domain'])) {
$domain = $_POST['domain'];
$correct_domain = "http://www.";
$chars_allowed = strlen($correct_domain);
if (substr($correct_domain, 0, $chars_allowed)) {
//Correct Address Entered
//You can do this a few ways
//Method 1, 2, and 3 are in the head. Method 4 needs to be before any html
?>
<script type="text/javascript">
// Method 1: window.open('<?php echo $domain;?>')
//or
//Method 2: window.parent.location="<?php echo $domain;?>";
</script>
<!-- Method 3: <meta http-equiv="refresh" content="0;url=<?php echo $domain;?>" /> -->
<?php
//Method 4: header("Location: $domain");
} else {
//Error
?>
<span style="color:#ff0000;">Please Enter a Valid Domain starting with <?php echo $correct_domain; ?>.</span>
<?php
}
}
?>
<form action="<?php $_SERVER['PHP_SELF']?>" method="post">
<input name="domain" type="text" />
<input type="submit" />
</form>

dorello
04-28-2010, 12:34 PM
I could keep the following code and put it into html:


<script type="text/javascript">
window.onload=function() {
if (document.getElementById) {
document.getElementById('form1').onsubmit=function() {
return getURL(document.getElementById('url').value);
}
}
}
function getURL(val){
if ((val=='')||(val=='http://www.')) { alert('Please enter a valid URL including http://www.'); }
else {
val = val;
var win=open(val);
}
return false;
}
</script>

Id need however to append another .js file to it (like splashpage.js)

I tried:

function getURL(val){
if ((val=='')||(val=='http://www.')) { alert('Please enter a valid URL including http://www.'); }
else {
val = val;
var win=open(val+splashpage.js);
}
return false;
}

...with no success. Do you know howto attach that other splash.js file to it, so it would open ONLY AFTER a valid URL has been submited?

bluewalrus
04-28-2010, 02:46 PM
Do you want javascript or PHP? I don't do JS well, the solution I gave you was the php solution. If something is wrong please be specific about what is wrong in the php whether it be an error message or not functioning correctly.

dorello
04-28-2010, 05:01 PM
Hi,
so, I have the form in a html file (and also the .js script above, in the same html).

Id like to load another external .js file (after the one from the html is executed.

This seccond .js file...

splashpage.js

...should then load the splashpage, on top of the browser (with transparency).

I am sure your php script is perfect, I just dont understand it, at least not yet, cause Im on the way of learning, but still a beginner:(

Again: after you enter a www.domainname.whatever, that page get browsed and on top of it (Z index) you get the transparent splashpage (which I managed already to make transparent.

bluewalrus
04-28-2010, 05:52 PM
So you're looking for an explanation of the PHP? I forgot a comparison before and called the wrong variable but that has been corrected here as well. It actually sounds like you aren't looking to redirect the user but are trying to get the contents of another page to display on your page if that is what you are trying to do that is done a different way. Saying you have a js, and html file isn't that helpful because php can work with both of those for instance here if you use method 1 html sends the form to php which processes it to javascript which redirects the user.


<?php
//If the input field with the name domain is set then process
//http://php.net/manual/en/function.isset.php
if (isset($_POST['domain'])) {
// set the variable $domain to the $_post value
$domain = $_POST['domain'];
//set the required starting characters you want here in this case http://www.
$correct_domain = "http://www.";
//count the number of characters in the required characters variable
$chars_allowed = strlen($correct_domain);
//if the required number of characters of $domain match $correct_domain process
//in other words $domain = http://www.google.com
// this compares the first character "h" through the last according to the number of characters in $correct_domain which is 11 so that ends at "."
// so this would pull out http://www.
// and then compare it to $correct_domain which is the same so it processes if $domain were https://www. it would not process because the pulled value would be https://www
if (substr($domain, 0, $chars_allowed)== $correct_domain) {
//here you process your redirect because it was correct
//Method 1, 2, and 3 go in the head. Method 4 needs to be before any html
// Method 1 and 2 are javascript, method 3 is... html? method 4 is php.
//the echos are written so the result is sent to the page once the php is processed. you might have to put methods 1 and 2 into the body tag with the onload not that good at JS the third method goes in the head is uses the meta redirect and the 4th method goes before any html tags it tells the browser what to load.
?>
<script type="text/javascript">
// Method 1: window.open('<?php echo $domain;?>')
//or
//Method 2: window.parent.location="<?php echo $domain;?>";
</script>
<!-- Method 3: <meta http-equiv="refresh" content="0;url=<?php echo $domain;?>" /> -->
<?php
//Method 4: header("Location: $domain");
} else {
//Error, you can handle this however you want
?>
<span style="color:#ff0000;">Please Enter a Valid Domain starting with <?php echo $correct_domain; ?>.</span>
<?php
}
}
?>
<form action="<?php $_SERVER['PHP_SELF']?>" method="post">
<input name="domain" type="text" />
<input type="submit" />
</form>

I guess formatted text doesn't work in commented tags? Can a DD admin/moderator confirm that or verify how to have the bolds show on line 14 of this?

dorello
04-28-2010, 06:53 PM
Thanks very much for your time, this is my html:


</head>
<body>
<form id="form1" name="form1" method="post" action="">
<label for="url">URL:</label>
<input input value="http://www." type="text" name="url" id="url" size="40">
<input type="submit" name="SubmitButton" value="Submit">
</form>
</body>
</html>

So, what should I do, just put your code into a say code.php file and write it into the action? I mean:
<form id="form1" name="form1" method="post" action="code.php"> (see above)? Man, Im such a beginner...

bluewalrus
04-28-2010, 07:47 PM
Whatever page your currently have make it a .php and put in this:


<?php
if (isset($_POST['url'])) {
$domain = $_POST['url'];
$correct_domain = "http://www.";
$chars_allowed = strlen($correct_domain);
if (substr($domain, 0, $chars_allowed)== $correct_domain) {
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<meta http-equiv="refresh" content="0;url=<?php echo $domain;?>" />
</head>
<body>
<?php
} else {
//Error, you can handle this however you want
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
</head>
<body>
<span style="color:#ff0000;">Please Enter a Valid Domain starting with <?php echo $correct_domain; ?>.</span>
<?php
}
}
?>
<form id="form1" name="form1" method="post" action="<?php $_SERVER['PHP_SELF']?>">
<label for="url">URL:</label>
<input input value="http://www." type="text" name="url" id="url" size="40">
<input type="submit" name="SubmitButton" value="Submit">
</form>
</body>
</html>

This is the whole page.

dorello
04-28-2010, 08:05 PM
Hi bluewalrus,
your code works perfectly, like my js file (also embeded. What Id like is the php code load externaly, not embeded in the html file. (like action="code.php").
Im sorry for missunderstanding, I live in Europe and english is not my first language;)

bluewalrus
04-28-2010, 08:21 PM
You want the php loaded on a different page or the page that loads to be in a new window?

dorello
04-28-2010, 09:56 PM
In a new window would be ok. Thanks for your time, you do so much good!

bluewalrus
04-28-2010, 11:11 PM
<?php
if (isset($_POST['url'])) {
$domain = $_POST['url'];
$correct_domain = "http://www.";
$chars_allowed = strlen($correct_domain);
if (substr($domain, 0, $chars_allowed)== $correct_domain) {
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
</head>
<body onload="window.open('<?php echo $domain;?>')">
<span>If a new window didn't open please make sure Javascript is enabled.</span>
<?php
} else {
//Error, you can handle this however you want
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
</head>
<body>
<span style="color:#ff0000;">Please Enter a Valid Domain starting with <?php echo $correct_domain; ?>.</span>
<?php
}
}
?>
<form id="form1" name="form1" method="post" action="<?php $_SERVER['PHP_SELF']?>">
<label for="url">URL:</label>
<input input value="http://www." type="text" name="url" id="url" size="40">
<input type="submit" name="SubmitButton" value="Submit">
</form>
</body>
</html>

This uses the javascript onload window.open.

dorello
04-29-2010, 03:12 PM
This is very cool, thanks a lot. Is it possible to put in this code something like:
<script type="text/javascript" src="splashpage.js"></script> in order to open that page on top of the just browsed page?

bluewalrus
04-29-2010, 06:13 PM
What do you mean by on top? Window 1 on top of window 2? No, you can't be sure it will open in a new window or where the position of the new window is (for every user). If you mean window 1 contains the history of window 2 no, but you could have window 2 open in window 1 then the history would still be present. Maybe you mean something else?

bluewalrus
04-29-2010, 10:24 PM
Okay, that is different. That isn't redirecting to the actual page that is redirecting to another page that pulls the content of the third page then overlays it on its own page. I'm busy tonight and tomorrow maybe tomorrow night I can think of a way to do it and code it.

If someone else wants to give it a try as well feel free.

dorello
04-29-2010, 10:51 PM
Ok, cool. I have the page and the flash movie already done. Im also looking for a business partner in the US for this project.

dorello
04-29-2010, 11:03 PM
Hi bluewalrus, what do you do at the moment? working for a company? I sent you a pm, did you get it?

bluewalrus
05-01-2010, 02:28 AM
Yea, I'm working for a company. My server f'ed up my ftp connections so I can't test my idea out. If you got time try fiddling around with get_file_contents

http://php.net/manual/en/function.file-get-contents.php

bluewalrus
05-03-2010, 02:55 AM
Server's still not accessible maybe try this out assuming your JS (splashpage.js) is all set for the overlay.

Whatever you want to call the page where the user enters the address:



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
</head>
<body>
<form id="form1" name="form1" method="post" action="new_page_name.php">
<label for="url">URL:</label>
<input input value="http://www." type="text" name="url" id="url" size="40">
<input type="submit" name="SubmitButton" value="Submit">
</form>
</body>
</html>


new_page_name.php:



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
</head>
<?php
if (isset($_POST['url'])) {
$domain = $_POST['url'];
$correct_domain = "http://www.";
$chars_allowed = strlen($correct_domain);
if (substr($domain, 0, $chars_allowed)== $correct_domain) {
?>
<body>
<?php
$requested_page = file_get_contents($domain);
$requested_page = str_replace("<head>", "<head>\n<script type=\"text/javascript\" src=\"splashpage.js\"></script>\n", $requested_page);
echo $requested_page;
} else {
//Error, you can handle this however you want
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
</head>
<body>
<span style="color:#ff0000;">Please Enter a Valid Domain starting with <?php echo $correct_domain; ?>.</span>
<form id="form1" name="form1" method="post" action="<?php $_SERVER['PHP_SELF']?>">
<label for="url">URL:</label>
<input input value="http://www." type="text" name="url" id="url" size="40">
<input type="submit" name="SubmitButton" value="Submit">
</form>
<?php
}
}
?>
</body>
</html>

dorello
05-05-2010, 08:23 PM
What do you mean by:
} else {
//Error, you can handle this however you want
-------------------------
Im also trying to replace this javascript string:

$requested_page = str_replace("<head>", "<head>\n<script type=\"text/javascript\" src=\"splashpage.js\"></script>\n", $requested_page);

with "player_page.html"

I just dont realise the correct syntax. Please give me a hint.

dorello
05-07-2010, 07:04 PM
Hi bluewalrus,

your code works pretty well, there is just a problem with showing the images from the called sites. So, I mean when you write an address like google.com or whatever, the images wont show.
Otherwise, in terms of combining two sites, works great!
Got an idea how to? If you need a server to make tests, I can give you one.

bluewalrus
05-07-2010, 07:11 PM
Mine should be up it's kinda pathetic that I'm paying 2 hosts and they still cant transfer my domains. Anyway you need to set the base domain to the requested domain I'd think. Then call all your pages absolutely since you have control on that...



$requested_page = str_replace("<head>", "<head>\n<base href=\"$domain\" />\n<script type=\"text/javascript\" src=\"splashpage.js\"></script>\n", $requested_page);


Is it working other than that? I just saw your post before your most recent, if any of that is still active please repost..

dorello
05-07-2010, 08:58 PM
Thanks, Ill try and replay your message. Just sent you a pm.

dorello
05-08-2010, 11:28 AM
Ok, what I got with your pretty code:
When using this:


<?php
if (isset($_POST['url'])) {
$domain = $_POST['url'];
$correct_domain = "http://www.";
$chars_allowed = strlen($correct_domain);
if (substr($domain, 0, $chars_allowed)== $correct_domain) {
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<meta http-equiv="refresh" content="0;url=<?php echo $domain;?>" />
</head>


<body>
<?php
$requested_page = file_get_contents($domain);
$requested_page = str_replace("<head>", "<head>\n<script type=\"text/javascript\" src=\"splashpage.js\"></script>\n", $requested_page);
//$requested_page = str_replace("<head>", "<head>\n<base href=\"$domain\" />\n<script type=\"text/javascript\" src=\"splashpage.js\"></script>\n", $requested_page);
echo $requested_page;
} else {
//Error, you can handle this however you want
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
</head>
<body>
//<span style="color:#ff0000;">Please Enter a Valid Domain starting with <?php echo $correct_domain; ?>.</span>
<?php
}
}
?>

... I get the splashpage.js on top, except the images on the page under, are not shown.

If I take out this part:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<meta http-equiv="refresh" content="0;url=<?php echo $domain;?>" />
</head>

... the page called per html shows correctly, but splashpage doesnt show on top at all.

I am desperated:-)

bluewalrus
05-08-2010, 04:42 PM
Is this live, could I see how it's functioning? I think the problem is dual html tags but want to double check.

dorello
05-08-2010, 09:25 PM
I sent you a pm:-)

bluewalrus
05-08-2010, 09:41 PM
Okay give this one a try...

<?php
if (isset($_POST['url'])) {
$domain = $_POST['url'];
$correct_domain = "http://www.";
$chars_allowed = strlen($correct_domain);
if (substr($domain, 0, $chars_allowed)== $correct_domain) {
?>
<body>
<?php
$requested_page = file_get_contents($domain);
$requested_page = str_replace("<head>", "<head>\n<base href=\"$domain\" />\n<script type=\"text/javascript\" src=\"splashpage.js\"></script>\n", $requested_page);
echo $requested_page;
} else {
//Error, you can handle this however you want
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
</head>
<body>
//<span style="color:#ff0000;">Please Enter a Valid Domain starting with <?php echo $correct_domain; ?>.</span>
<?php
}
}
?>

You will need to have all of the codes on your site called absolute though with this code.

dorello
05-08-2010, 11:37 PM
Hmm, splashpage doesnt show at all:-( although Ive changed html codes to absolute, as you said.
I must say, I had to change allow_url_fopen to ON, in order to get this working (I mean in the previous version):

$requested_page = file_get_contents($domain);

bluewalrus
05-09-2010, 06:31 PM
Your splash page is not absolute, in the php replace change this



$requested_page = str_replace("<head>", "<head>\n<base href=\"$domain\" />\n<script type=\"text/javascript\" src=\"splashpage.js\"></script>\n", $requested_page);


to


$domain_is = "PUT YOUR DOMAIN NAME HERE in format http://www.site.com/";
$requested_page = str_replace("<head>", "<head>\n<base href=\"$domain\" />\n<script type=\"text/javascript\" src=\"$domain_issplashpage.js\"></script>\n", $requested_page);

This needs to be done because the base tag tells the page that all codes are being called from the other domain. http://www.w3schools.com/tags/tag_base.asp

You have 2 additional dead links on your page images/srpr/nav_logo13.png and extern_js/f/CgJkZRICZGUgACswCjhmQB0sKzAOOA0sKzAWOBYsKzAXOAYsKzAYOAUsKzAZOCIsKzAlOMqIASwrMCY4CiwrMCc4BCwrMDw4AiwrMEA4ECwrMEE4BCwrMEU4ASwrME44BSwrMFE4ASyAAhM/pvTgIlosT_A.js

dorello
05-09-2010, 08:56 PM
Here the changed code:


<?php
if (isset($_POST['url'])) {
$domain = $_POST['url'];
$correct_domain = "http://www.";
$chars_allowed = strlen($correct_domain);
if (substr($domain, 0, $chars_allowed)== $correct_domain) {
?>
<body>
<?php
$requested_page = file_get_contents($domain);

//change begin

$domain_is = "PUT YOUR DOMAIN NAME HERE in format http://www.site.com/";
$requested_page = str_replace("<head>", "<head>\n<base href=\"$domain\" />\n<script type=\"text/javascript\" src=\"$domain_issplashpage.js\"></script>\n", $requested_page);
echo $requested_page;
} else {

//change end

//Error, you can handle this however you want
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
</head>
<body>
<span style="color:#ff0000;">Please Enter a Valid Domain starting with <?php echo $correct_domain; ?>.</span>
<?php
}
}
?>

New addition between the //changes tags. Splashpage still doesnt show.
-------------------------
What do you mean with:


You have 2 additional dead links on your page images/srpr/nav_logo13.png and extern_js/f/CgJkZRICZGUgACswCjhmQB0sKzAOOA0sKzAWOBYsKzAXOAYsKzAYOAUsKzAZOCIsKzAlOMqIASwrMCY4CiwrMCc4BCwrMDw4AiwrMEA4ECwrMEE4BCwrMEU4ASwrME44BSwrMFE4ASyAAhM/pvTgIlosT_A.js

I cant find this 2 dead links anywhere.

dorello
05-09-2010, 11:05 PM
Sent you a pm;-)

dorello
05-11-2010, 09:31 PM
Hi bluewalrus
This line in your code doesnt seem to work
src=\"$domain_issplashpage.js\"
;-)

bluewalrus
05-11-2010, 11:59 PM
Try this in newpage.php



$domain_is = "http://www.dorello-productions.com/"; // alter this when changing servers
$requested_page = str_replace("<head>", "<head>\n<base href=\"$domain\" />\n<script type=\"text/javascript\" src=\"${domain_is}splashpage.js\"></script>\n", $requested_page);

dorello
05-12-2010, 10:30 AM
Thank you, it works great in IE, but not in Firefox:confused:
(this damn Firefox, even I like more that the IE) Hmmmm...

bluewalrus
05-12-2010, 12:14 PM
What happens in IE vs. firefox? Since it is a browser issue I assume it is a javascript issue not with the php so this might be something I can't help with... Is it on the same link?

dorello
05-12-2010, 04:28 PM
If you enter google (in FF), it says: about.html not found.
If you enter aaa.de, it shows the same site overlapt.
In both cases, no dancing Kirsten.

You are right, it might be the splashpage.js screwed up:-((