Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New Coder
    Join Date
    Apr 2012
    Posts
    31
    Thanks
    10
    Thanked 2 Times in 2 Posts

    Combining Javascript and PHP?

    I have a button which when clicked, it redirects you to a random website in a list I specified in the code. Now, this list might get big (hundreds of thousands of URLs) so the user might take a while for everything to load, since all that Javascript code would have to be shipped across the web.

    Here is the code:
    Code:
    <html>
    <head>
    <script type="text/javascript">
    
    function randomsite()
    {
    var urls = new Array();
    urls[0] = "http://www.google.com";
    urls[1] = "http://www.youtube.com";
    urls[2] = "http://www.yahoo.com";
    var random = Math.floor(Math.random()*urls.length);
    window.location = urls[random];
    }
    </script>
    
    <form name="form1" id="form1">
        <input type=button value='Display' class="form1" onClick="randomsite()"
    
    
    
    
      </form>
    However, I would like to make this whole process server-sided, so the URL is chosen in the server directly and transferred back to the user.

    How would I go about doing this?

    Thank you!
    Last edited by Flames; 05-27-2012 at 07:59 PM.

  • #2
    Regular Coder dan-dan's Avatar
    Join Date
    Aug 2009
    Location
    England
    Posts
    483
    Thanks
    22
    Thanked 79 Times in 78 Posts
    Best best approach would be to store the URL's in a database and use MySQL to randomly select a row.
    You could use this:

    PHP Code:
    <?php

    $db_hostname 
    'localhost'
    $db_database 'sites'
    $db_username 'danny'
    $db_password 'letmein'

    $db_server mysql_connect($db_hostname$db_username$db_password);

    if (!
    $db_server)
        die(
    "Unable to connect to mysql: " mysql_error());

    mysql_select_db($db_database$db_server)
        or die(
    "Unable to select database: " mysql_error());

    if (isset(
    $_POST['getSite'])) {
        
    $result mysql_query("SELECT * FROM `sites` WHERE id >= (SELECT FLOOR( MAX(id) * RAND()) FROM `sites` ) ORDER BY id LIMIT 1");
        if (!
    $result
            echo 
    'Sorry, you encounted a problem: ' mysql_error();
        else {
            
    $row mysql_fetch_assoc($result);
            echo 
    'You are being redirected to ' $row['url']. '...';
            echo 
    '<meta http-equiv="refresh" content="3; URL='.$row['url'].'">';
        }
    }    

    ?>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
    <head>
        <title>TEST</title>
    </head>

    <body>
        <form method="post" action="" name="form1" id="form1">
            <input type="submit" value='Display' class="form1" name="getSite" />
        </form>
    </body>

    </html>
    Here the SQL dump including some test sites:
    Code:
    -- phpMyAdmin SQL Dump
    -- version 3.4.5
    -- http://www.phpmyadmin.net
    --
    -- Host: 127.0.0.1
    -- Generation Time: May 27, 2012 at 09:00 PM
    -- Server version: 5.5.16
    -- PHP Version: 5.3.8
    
    SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
    SET time_zone = "+00:00";
    
    
    /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
    /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
    /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
    /*!40101 SET NAMES utf8 */;
    
    --
    -- Database: `sites`
    --
    
    -- --------------------------------------------------------
    
    --
    -- Table structure for table `sites`
    --
    
    CREATE TABLE IF NOT EXISTS `sites` (
      `id` int(5) NOT NULL AUTO_INCREMENT,
      `url` varchar(300) NOT NULL,
      PRIMARY KEY (`id`),
      UNIQUE KEY `url` (`url`)
    ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;
    
    --
    -- Dumping data for table `sites`
    --
    
    INSERT INTO `sites` (`id`, `url`) VALUES
    (2, 'http://www.codingforums.com'),
    (1, 'http://www.facebook.com'),
    (4, 'http://www.google.com'),
    (3, 'http://www.hotmail.com'),
    (5, 'http://www.youtube.com');
    
    /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
    /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
    /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
    Last edited by dan-dan; 05-27-2012 at 09:08 PM.

  • Users who have thanked dan-dan for this post:

    Flames (06-06-2012)

  • #3
    New Coder
    Join Date
    Apr 2012
    Posts
    31
    Thanks
    10
    Thanked 2 Times in 2 Posts
    Thank you, however, when you click the button, it remains there and proceeds to inform me "You are being redirected to..".

    The thing is that I don't want the button there when it is telling me I am being redirected, how would I get rid of it?

    Thanks again.

  • #4
    Regular Coder d'Anconia's Avatar
    Join Date
    Jan 2010
    Location
    Tempe, AZ
    Posts
    149
    Thanks
    16
    Thanked 6 Times in 6 Posts
    What exactly do you want it to show while you are being directed (ie while the server and your computer process the info)?
    Datagonia Web (My Portfolio)

    Powerful ideas for all lovers of personal and political freedom:
    Freedomain Radio
    Free Talk Live

  • #5
    New Coder
    Join Date
    Apr 2012
    Posts
    31
    Thanks
    10
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by d'Anconia View Post
    What exactly do you want it to show while you are being directed (ie while the server and your computer process the info)?
    Simply "You are being redirected to...", alone, without the button.

    Before clicking "Display":


    After clicking "Display":


    When "Display" is clicked, I don't want the button or anything else from the page.

  • #6
    New Coder
    Join Date
    Apr 2012
    Posts
    31
    Thanks
    10
    Thanked 2 Times in 2 Posts
    P.S. I have tried changing the form so it opens in a new tab (which is what I want to do), but it remains showing me the message WITH the original page content (i.e. the button).

  • #7
    Regular Coder dan-dan's Avatar
    Join Date
    Aug 2009
    Location
    England
    Posts
    483
    Thanks
    22
    Thanked 79 Times in 78 Posts
    OK, I added the form itself into the statement. You can't open a page in new tab with PHP, it's sevver side scripting.

    PHP Code:
    <?php

    $db_hostname 
    'localhost'
    $db_database 'sites'
    $db_username 'danny'
    $db_password 'letmein'

    $db_server mysql_connect($db_hostname$db_username$db_password);

    if (!
    $db_server)
        die(
    "Unable to connect to mysql: " mysql_error());

    mysql_select_db($db_database$db_server)
        or die(
    "Unable to select database: " mysql_error());

    if (isset(
    $_POST['getSite'])) {
        
    $result mysql_query("SELECT * FROM `sites` WHERE id >= (SELECT FLOOR( MAX(id) * RAND()) FROM `sites` ) ORDER BY id LIMIT 1");
        if (!
    $result
            echo 
    'Sorry, you encounted a problem: ' mysql_error();
        else {
            
    $row mysql_fetch_assoc($result);
            echo 
    'You are being redirected to ' $row['url']. '...';
            echo 
    '<meta http-equiv="refresh" content="3; URL='.$row['url'].'">';
        }
    }    
    else
    echo
    <<<_HTML

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
    <head>
        <title>TEST</title>
    </head>

    <body>
        <form method="post" action="" name="form1" id="form1">
            <input type="submit" value='Display' class="form1" name="getSite" />
        </form>
    </body>

    </html>
    _HTML;
    ?>

  • Users who have thanked dan-dan for this post:

    Flames (06-07-2012)

  • #8
    New Coder
    Join Date
    Apr 2012
    Posts
    31
    Thanks
    10
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by dan-dan View Post
    OK, I added the form itself into the statement. You can't open a page in new tab with PHP, it's sevver side scripting.

    PHP Code:
    <?php

    $db_hostname 
    'localhost'
    $db_database 'sites'
    $db_username 'danny'
    $db_password 'letmein'

    $db_server mysql_connect($db_hostname$db_username$db_password);

    if (!
    $db_server)
        die(
    "Unable to connect to mysql: " mysql_error());

    mysql_select_db($db_database$db_server)
        or die(
    "Unable to select database: " mysql_error());

    if (isset(
    $_POST['getSite'])) {
        
    $result mysql_query("SELECT * FROM `sites` WHERE id >= (SELECT FLOOR( MAX(id) * RAND()) FROM `sites` ) ORDER BY id LIMIT 1");
        if (!
    $result
            echo 
    'Sorry, you encounted a problem: ' mysql_error();
        else {
            
    $row mysql_fetch_assoc($result);
            echo 
    'You are being redirected to ' $row['url']. '...';
            echo 
    '<meta http-equiv="refresh" content="3; URL='.$row['url'].'">';
        }
    }    
    else
    echo
    <<<_HTML

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
    <head>
        <title>TEST</title>
    </head>

    <body>
        <form method="post" action="" name="form1" id="form1">
            <input type="submit" value='Display' class="form1" name="getSite" />
        </form>
    </body>

    </html>
    _HTML;
    ?>
    Once again here to save the day, thank you very much.

    By the way, to make it open in a new tab I just modified the form and added:
    Code:
    onclick="this.form.target='_blank'"

  • #9
    New Coder
    Join Date
    Apr 2012
    Posts
    31
    Thanks
    10
    Thanked 2 Times in 2 Posts
    One last thing, is there no possible way of opening in a new tab without all the content from the original page? And also, since the form code has been converted into php I cannot view or modify the button in design view, is there anyway to change this? Because i really want to add CSS to it but it looks like I can't because everything is in PHP tags.

    Thanks.
    Last edited by Flames; 06-07-2012 at 02:56 PM.

  • #10
    Regular Coder dan-dan's Avatar
    Join Date
    Aug 2009
    Location
    England
    Posts
    483
    Thanks
    22
    Thanked 79 Times in 78 Posts
    You can style it like any other HTML page. Just add the CSS like below.

    As for the tab thing, like I said it can't be done in PHP alone. What you have above is javascript. I really don't know anything about javascript to further help you in that aspect, sorry.

    PHP Code:
    <?php

    $db_hostname 
    'localhost'
    $db_database 'sites'
    $db_username 'danny'
    $db_password 'letmein'

    $db_server mysql_connect($db_hostname$db_username$db_password);

    if (!
    $db_server)
        die(
    "Unable to connect to mysql: " mysql_error());

    mysql_select_db($db_database$db_server)
        or die(
    "Unable to select database: " mysql_error());

    if (isset(
    $_POST['getSite'])) {
        
    $result mysql_query("SELECT * FROM `sites` WHERE id >= (SELECT FLOOR( MAX(id) * RAND()) FROM `sites` ) ORDER BY id LIMIT 1");
        if (!
    $result
            echo 
    'Sorry, you encounted a problem: ' mysql_error();
        else {
            
    $row mysql_fetch_assoc($result);
            echo 
    'You are being redirected to ' $row['url']. '...';
            echo 
    '<meta http-equiv="refresh" content="3; URL='.$row['url'].'">';
        }
    }    
    else
    echo
    <<<_HTML

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
    <head>
        <title>TEST</title>
        <style type="text/css">
        .form1 {
            background: pink;
            border: 3px solid purple;
            padding: 15px;
            -webkit-border-radius: 16px;
            -moz-border-radius: 16px;
            border-radius: 16px;
            cursor: pointer;
        }
        </style>
    </head>

    <body>
        <form method="post" action="" onclick="this.form.target=_blank" name="form1" id="form1">
            <input type="submit" value='Display' class="form1" name="getSite" />
        </form>
    </body>

    </html>
    _HTML;
    ?>

  • Users who have thanked dan-dan for this post:

    Flames (06-08-2012)


  •  

    Posting Permissions

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