【问题标题】:Using PHP inside of HTML to redirect to a random site [duplicate]在 HTML 中使用 PHP 重定向到随机站点 [重复]
【发布时间】:2016-11-22 09:20:24
【问题描述】:

我需要能够让用户点击一个按钮,并被重定向到一个随机页面。

我尝试将 PHP 放入 JavaScript 中,然后将其放入 HTML 中,如下所示:

<script>
<button onclick="var jsVar = "<?php 
$urls = array("www.site1.com", "www.site2.com", "www.site3.com","www.site4.com"); 
$url = $urls[array_rand($urls)]; 
header("Location: http://$url"); ?>"">Click</button>
</script>

我知道这可能有很多错误,非常感谢您的帮助。谢谢!

【问题讨论】:

  • 用按钮做一个表单,然后当点击按钮时转到随机页面
  • 你可以在Javascript中做到这一点,你根本不需要php

标签: javascript php html


【解决方案1】:

试试这个,

<?php
$urls = array("www.site1.com", "www.site2.com", "www.site3.com","www.site4.com"); 
$url = $urls[array_rand($urls)]; 
?>
<button onclick="myfunction();">Click</button>
<script>
function myfunction(){
    var href = "<?php echo $url?>";
    window.location.href = "http://"+href;
}
</script>

【讨论】:

  • 没用,不过谢谢!
【解决方案2】:

PHP 脚本会生成随机 URL,当你点击按钮时,它会调用 randsite($url) JavaScript 函数,该函数会将你重定向到随机站点。

<?php
    $urls = array("http://www.site1.com", "http://www.site2.com", "http://www.site3.com","http://www.site4.com"); 
    // select random url
    $rand = $urls[mt_rand(0, count($urls) - 1)];
?>

<button onclick="randsite(<?php echo "'".$rand."'"; ?>)">Click</button>

<script type="text/javascript">
function randsite($url){
    window.location = $url;
}
</script>

【讨论】:

    【解决方案3】:

    PHP + HTML + JS:

     <?php $url = "http://....."; ?>
        <button name="redirect"onclick="redirectFunc(<?php echo $url; ?>);">Redirect with button</button>
    
        <script>
        function redirectFunc($url){
            window.location.href = "<?php echo $url?>";
        }
        </script>
    

    重定向 HTML + PHP: http://www.w3schools.com/php/php_forms.asp

    假设您的 php 文件位于以下地址: http://www.yourserver.com/form-action.php 在这种情况下,PHP_SELF 将包含:“/form-action.php”

    <form method="post" action="<?php $_PHP_SELF ?>">
         // type means what should button do submit -> submit your post
         // name how you will recognize which post was sended
         // value value of button which you can get
         <button type="submit" name="redirect" value="redirectValue" id="redirect">Redirect with button post</button>
    </form>
    

    然后你点击按钮处理你的帖子

    <?php
    if(isset($_POST['redirect'])) { 
       // rand your url
       // echo $_POST['redirect']; will output redirectValue
        header('Location: http://....');
    }
    ?>
    

    或使用 ahref: http://www.w3schools.com/html/html_links.asp

    //or you can use ahref e.g
        <?php $url = "http://..."; 
        // code for randoming url
        ?>
    
          <a href="<?php echo $url; ?>">Redirect with a href</a></p>
    

    HTML + JS:

    <button id="buttonID">redirect</button>
    
    <script type="text/javascript">
        // here you can rand your urls and choose one of them to redirect
        document.getElementById("buttonID").onclick = function () {
            location.href = "http://...";
        };
    </script>
    

    【讨论】:

      猜你喜欢
      • 2015-04-19
      • 1970-01-01
      • 2013-10-25
      • 1970-01-01
      • 1970-01-01
      • 2012-06-23
      • 1970-01-01
      • 2014-07-26
      • 1970-01-01
      相关资源
      最近更新 更多