简体   繁体   English

使用提交按钮而非锚标记打开新链接

[英]Opening a new link using submit button not anchor tag

I need to be able to when the submit button is clicked a new tab must be open based on the anchor link coming from the database. 我需要能够在单击“提交”按钮时根据来自数据库的锚链接打开一个新选项卡。 I have been struggling for hours. 我已经奋斗了几个小时。

this is my form 这是我的表格

<form action="" method="POST">

<select name="name">

<option value="">Select Names</option>
<option value="1">Names</option>
</select>
<input type ="submit" name="submit" value="find random name">

</form>

Here is the php 这是PHP

    <?php 
    $query="SELECT * FROM names_table ORDER BY RAND() LIMIT 1  ";
    $result=mysqli_query($connection,$query);
    if(!$result){echo "no results";}
    while($selected=mysqli_fetch_assoc($result)){


    $link=$selected['name'];

    echo  $ran_name= "<a href=$link target=\"_blank\">Click here</a>";
  ?>

// this code works but i dont want to be able to use Click here i want to be able to generate the results once the submit button is clicked , the submit button must open the new tab.I think javascript must be used but i dont know how. //此代码有效,但我不希望使用单击此处,我​​希望一旦单击提交按钮后就能够生成结果,提交按钮必须打开新选项卡。我认为必须使用javascript,但我不希望使用知识。

May be, something like this? 可能是这样的吗?

<?php
..
echo  $ran_name= "<script> document.location.href='".$link."';</script>";
..
?>

More about location object http://www.w3schools.com/jsref/obj_location.asp . 有关位置对象http://www.w3schools.com/jsref/obj_location.asp的更多信息。

  1. Way 1 方式1

    < input type="button" value="Open Window" <input type =“ button” value =“打开窗口”

    onclick="window.open('< ?php echo $link;?>')"> onclick =“ window.open('<?php echo $ link;?>')”>

OR: 要么:

onclick="window.location.href='< ?php echo $link;?>','_blank'";
  1. Way 2: 方式2:

    < form action="< ?php echo $link;?>" method="post" target="_blank"> <form action =“ <?php echo $ link;?>” method =“ post” target =“ _ blank”>

     ... 

    < /form> </ form>

p/s: I don't know how to show code with format. p / s:我不知道如何用格式显示代码。 (same you); (跟你一样);

maybe you try this one? 也许你试试这个? that's should do what you want 那应该做你想要的

<?php 
    $query="SELECT * FROM names_table ORDER BY RAND() LIMIT 1  ";
    $result=mysqli_query($connection,$query);
    if(!$result) {
      exit("no results");
    }
    while($selected=mysqli_fetch_assoc($result)){   
         $link=$selected['name'];
    }

    // Redirect to page
    if (defined($link) && !empty($link)) 
       header("Location: /$link");
  ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM