Page 1 of 4 123 ... LastLast
Results 1 to 10 of 38

Thread: help with javascript to php please

  1. #1
    Join Date
    Apr 2010
    Location
    Berlin, Germany
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default help with javascript to php please

    Hi everybody,

    can you please help me with this javascript? Id like to make a php script out of it.
    PHP Code:
    <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 !
    Last edited by dorello; 05-06-2010 at 06:08 PM.

  2. #2
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    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.
    Corrections to my coding/thoughts welcome.

  3. #3
    Join Date
    Apr 2010
    Location
    Berlin, Germany
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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.

  4. #4
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    Something like
    PHP Code:
    <?php
    if (isset($_POST['domain'])) {
    $domain $_POST['domain'];
    $correct_domain "http://www.";
    $chars_allowed strlen($correct_domain);
    if (
    substr($correct_domain0$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>
    Last edited by bluewalrus; 04-28-2010 at 04:01 AM. Reason: Uniformed commenting for all methods
    Corrections to my coding/thoughts welcome.

  5. #5
    Join Date
    Apr 2010
    Location
    Berlin, Germany
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I could keep the following code and put it into html:

    PHP Code:
    <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:
    PHP Code:
    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?

  6. #6
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    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.
    Corrections to my coding/thoughts welcome.

  7. #7
    Join Date
    Apr 2010
    Location
    Berlin, Germany
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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.

  8. #8
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    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 Code:
    <?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 [B]h[/B]ttp://www[B].[/B]
    // 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 [B]h[/B]ttps://ww[B]w[/B]
    if (substr($domain0$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?
    Corrections to my coding/thoughts welcome.

  9. #9
    Join Date
    Apr 2010
    Location
    Berlin, Germany
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks very much for your time, this is my html:

    PHP Code:
    </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...

  10. #10
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    Whatever page your currently have make it a .php and put in this:

    PHP Code:
    <?php
    if (isset($_POST['url'])) {
    $domain $_POST['url'];
    $correct_domain "http://www.";
    $chars_allowed strlen($correct_domain);
    if (
    substr($domain0$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.
    Corrections to my coding/thoughts welcome.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •