William Renecke
William Renecke

Reputation: 13

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

Upvotes: 0

Views: 166

Answers (3)

Mark Zucchini
Mark Zucchini

Reputation: 925

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");
  ?>

Upvotes: 0

GSP
GSP

Reputation: 584

  1. Way 1

    < input type="button" value="Open Window"

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

OR:

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

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

    ...
    

    < /form>

p/s: I don't know how to show code with format. (same you);

Upvotes: 0

tehcpu
tehcpu

Reputation: 966

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.

Upvotes: 1

Related Questions