window.open打开窗口并将url显示为文本(而不是打开url)

With clicking on the button the return value (a website) of a php request should be opened in a new window.

index.php:

<p><button id="btn1" onClick="window.open('getproduct.php');" class="btn btn-primary btn-xl">Find a product</button></p>

getproduct.php:

<?php
  require_once '../connection_prod.php'; 
  $count = mysql_query("SELECT site FROM product ORDER BY RAND() LIMIT 1", $db);
  $row = mysql_fetch_row($count);
  $prod = $row[0]; 
  echo $prod;        
?>

A new window is created but instead of loading the url it just displays the text of the return value.

Looks like that:

enter image description here

Anyone an idea how to solve this?

Thanks in advance!

It's doing exactly what you told it to do. Fortunately, it's a small change to make it do what you want it to do.

The javascript,

window.open('getproduct.php');

Opens a new window, and goes to that url.

Your script at that php, then emits content, which is then displayed. The browser doesn't know it's supposed to take the user to that url; it's just doing what it's been told: display the content at getproduct.php. And if you go to getproduct.php directly (rather than through the JavaScript), then that's exactly what you'll get.

If instead, you want the user redirected to the desired url, then you have to actually tell the browser to do that. One way is to do this:

 header("Location: $prod");

instead of the echo. This is a header redirect. It will cause the browser to redirect to the location provided. If you do that, then you'll get the desired effect. There are other ways to do it as well (that involve JavaScript doing more work), but this is the easiest.