最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to pass php variable in window.location javascript - Stack Overflow

programmeradmin1浏览0评论

I want to pass the php variable inside the window.location javascript this is my php code and i am unable to do that.

echo '<script>location.href = "reportConsumption.php?creategenReport="'.$genid.'"&sDate="'.$startdate.'"&eDate="'.$enddate;</script>';

I want to pass the php variable inside the window.location javascript this is my php code and i am unable to do that.

echo '<script>location.href = "reportConsumption.php?creategenReport="'.$genid.'"&sDate="'.$startdate.'"&eDate="'.$enddate;</script>';
Share Improve this question asked Mar 31, 2018 at 12:17 hotshot codehotshot code 1731 silver badge11 bronze badges 3
  • 1 Why not just use header("Location: $url")? – Aniket Sahrawat Commented Mar 31, 2018 at 12:18
  • header is not working it is not redirecting so i am using location.href – hotshot code Commented Mar 31, 2018 at 12:18
  • If header("Location: $url") is not working then i am sure location.href will not work also. BTW try once:- echo '<script>location.href ="reportConsumption.php?creategenReport='.$genid.'&sDate='.$startdate.'&eDate='.$enddate.'"</script>'; – Death-is-the-real-truth Commented Mar 31, 2018 at 12:20
Add a ment  | 

4 Answers 4

Reset to default 2

try to set quotation marks

echo '<script>location.href = "reportConsumption.php?creategenReport='.$genid.'&sDate='.$startdate.'&eDate='.$enddate.'"</script>';

You are closing your quote in JS

echo '<script>location.href = "reportConsumption.php?creategenReport="'.$genid.'"&sDate="'.$startdate.'"&eDate="'.$enddate;</script>';

Should be

 echo '<script>location.href = "reportConsumption.php?creategenReport='.$genid.'&sDate='.$startdate.'&eDate='.$enddate.'</script>';

This will cause an error in JS on the client side, you could see this by pressing f12 and looking in the console log in the browser debugger. Your source code will look like this

 <script>location.href = "reportConsumption.php?creategenReport="35"&sDate="...

 //where this is a quoted block
 "reportConsumption.php?creategenReport="
 //and this is just chilling in space
 35
 //and then a new quoted block, etc.
 "&sDate="

And you had this other (php syntax error) issue I took the liberty of fixing.

 .$enddate;</script>';

Just in PHP you can redirect with

  header("Location: $url");

But you have to be sure of 2 things:

  1. You do not output "Anything" not even a line return before calling header
  2. You call exit(); immediately after it. If you don't PHP will continue to execute the current script after it executes the redirect. Which is probably not desirable.

You are closing the double quotes too early. It should be close at the end of the URL. So you have a syntax error in your JavaScript:

echo '<script>location.href = "reportConsumption.php?creategenReport='.$genid.'&sDate='.$startdate.'&eDate='.$enddate.'";</script>';

Or separate using a variable to be more clear:

$url = 'reportConsumption.php?creategenReport='.$genid.'&sDate='.$startdate.'&eDate='.$enddate;
echo '<script>location.href = "'.$url.'";</script>';

You should not use double quote around the values for GET param

  echo '<script>location.href = "reportConsumption.php?creategenReport='.$genid. 
          '&sDate='.$startdate.'&eDate='. $enddate .'"';</script>';
发布评论

评论列表(0)

  1. 暂无评论