简体   繁体   中英

how to create a dynamic redirect link with js and php?

PHP has no relation and behavior with browsers and in some times it makes some mistake. For example we can't set for header function to redirect to a page in a blank page. Because it has no relation with the browsers and for doing this, we should use JS .Now I'm mixing these two languages to reaching the goal.

 <?php
     $word = $_POST["tagTxt"];
     $tag = "https://www.example.com/" . $word;

     echo '<script>window.open("Location: <?php $search;?>");</script>';
?>

The goal is this code but it doesn't work. I want get a string from a first page then post it with post method and then past it to a static address for redirecting with blank target. Thanks.

不确定我是否完全理解你的问题,但你有没有尝试过这样的事情:

<?php header("Location: $search");

I think you are trying to open a new url when the page is loaded.

So if you trying to do this check the code below. This way you are mixing the parameters received from client, processing them and them sending back to the client.

Then you use the javascript with the window.onload function to execute the redirect page when the page is loaded with the received parameter.

<?php
$word = "1";
$tag = "www.yahoo.com/" . $word;

echo '<script text="text/javascript">window.onload =     function(){window.open("'.$tag.'");} </script>';

?>

If you are "changing" the web page. Do not use window.open

Use window.location = 'http://www.google.com/';

in your example do this

 <?php
   $word = $_POST["tagTxt"];
   $tag = "https://www.example.com/" . $word;

   echo "<script>window.location = '$tag';</script>";
?>

(copy the code above, and it should work perfectly for what you're asking for)

I fount the way. thanks all.

<?php
   $word = $_POST["tagTxt"];
   $tag = "https://www.example.com/" . $word;
   echo "<script>window.open( '$tag' );</script>";
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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