2

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>';
3
  • 1
    Why not just use header("Location: $url")? Commented Mar 31, 2018 at 12:18
  • header is not working it is not redirecting so i am using location.href 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>'; Commented Mar 31, 2018 at 12:20

4 Answers 4

2

try to set quotation marks

echo '<script>location.href = "reportConsumption.php?creategenReport='.$genid.'&sDate='.$startdate.'&eDate='.$enddate.'"</script>';
Sign up to request clarification or add additional context in comments.

Comments

1

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.

6 Comments

lets suppose i have this kind of situation then? echo '<script>window.open("reportConsumption.php?creategenReport='.$genid.'&sDate='.$startdate.'&eDate='.$enddate.'",'_blank');</script>';
Then you should probably double quote this '_blank' like "_blank", basically this is the opposite issue where $enddate.'",'_blank specifically .'",' closes the block. PS you can do inline code format with the backtic ` its aka ~ tilde
man it really worked but the problem is that it is showing popup blocked can i resolve it via code
I would post it as a separate question. Comments is not really the place to handle it, just saying.
i am not been able to post question before 60 mins
|
1

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>';

Comments

0

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>';

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.