当前位置: 首页 > 知识库问答 >
问题:

使用超文本标记语言中的PHP重定向到随机站点[复制]

黎玺
2023-03-14

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

我试着把PHP放在JavaScript里面,放在超文本标记语言里面,像这样:

<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>

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

共有3个答案

宋晋
2023-03-14

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>

超文本标记语言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>
彭英逸
2023-03-14

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>
百里京
2023-03-14

试试这个,

<?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>
 类似资料:
  • 我确信这对我来说是简单而愚蠢的,但是我的php代码一直显示在我的html页面中。 这只是一个简单的测试: 这就是它的样子: 我到底做错了什么?上面我混合了回声和打印来表明它似乎没有什么不同。

  • 我想知道是否有一种方法可以使用我的超文本标记语言/PHP网站与我的不和谐机器人进行交流。所以我想做的是有一些代码或使用另一个解决方案来发送由我的不和谐机器人发送的消息。例如:假设我写了一行,每次有人提交表单时,我都想从我的网站上发送,然后我想让不和谐机器人在特定渠道发送一条类似于“XXXX刚刚提交了表单”的信息。 我曾尝试搜索youtube、stack overflow和其他网站,但没有找到方法。

  • 我想在单击输入字段时触发一个处理程序,在取消选择输入字段时触发另一个处理程序(即,如果有人在字段外单击)。有没有办法做到这一点? 单击处理程序非常简单: 是否可以创建“取消单击”处理程序?

  • 我到处找,到处找。我已经看到了我将要发布的PHP代码中的许多问题,但没有看到任何与我所看到的特定问题相关的问题。 这是我的问题-两行($values/$required),其中我有“name”,“email”,“subject”,“message” 我想在上面加上“电话”。(即“姓名”、“电子邮件”、“电话”、“主题”、“信息”) 如果我把它添加到两行中的一行(不管是哪一行),我仍然可以提交表格,

  • 我是新来的。我想解析html,但问题是我们必须在中指定的URL,我将在运行时从其他页面响应此URL。有没有办法将收到的网址传递到中?我读过这样的东西: 但是我不知道如何使用它。我很想知道是否有其他方法比jsoup更好。

  • 对于上面的html内容,我如何使用Jsoup解析并获取文本 当我使用 我得到了这样的东西