0

Just a quick question.

At the moment I have a login script that re-directs to dashboard.php where the user can click a button to retrieve Google Analytics stats for the pre-selected current month and year.

The url for May 2011 is: dashboard.php?month=May&year=2011&submit=Get+Statistics!

So what I am trying to do is have the stats load with the page instead of having to click the button.

The only way I can think to do this is by changing the following line in my login script:

// redirect to secure page
document.location='dashboard.php';

to something like:

// redirect to secure page
document.location='dashboard.php' + <?php echo ?month=currentMonth&year=currentYear&submit=Get+Statistics! ?>';

with the month and year variables set as follows: $currentMonth = date('F'); $currentYear = date('Y');

But this doesn't work, it just goes to dashboard.php as normal.

Any ideas?

3 Answers 3

2

You're missing the single quote in front of the second string:

document.location='dashboard.php' + '<?php echo "?month=currentMonth&year=currentYear&submit=Get+Statistics!" ?>';

should work in a PHP file.

Sign up to request clarification or add additional context in comments.

8 Comments

ah, thanks, trouble with that is I am literally being redirected to dashboard.php<?php echo "?month=currentMonth&year=currentYear&submit=Get+Statistics!" ?>, will see if I can mess around with it to get it working in the meantime.
Is this JavaScript in a PHP file? If not, the PHP statement won't execute. I.e, the file that it's in must end in .php, and be on a server (locally using XAMPP/MAMP etc or remotely).
I have login.php which includes login.js which in turn has this re-direct code in it.
I think you'll need to put that JS inline within login.php or it won't work.
ok, I'll give that a bash, it's not too bad, was just trying not to clutter up the page with html, php and js! Let you know in two minutes if this works...
|
1

I don't know if this is when you wrote your post, but your echo is missing quotes :

Try this :

document.location='dashboard.php' + <?php echo "?month=currentMonth&year=currentYear&submit=Get+Statistics!"; ?>';

1 Comment

thanks, I tried document.location='dashboard.php' + <?php echo "?month=$currentMonth&year=$currentYear&submit=Get+Statistics!"; ?>; but no joy I'm afraid. It might be an issue but the js is in an external file which is included after the php variables are set so I don't think that should break it.
1

As long as you are in the same scope, you can get the variables in any part of the code:

// redirect to secure page
document.location='dashboard.php' + <?= "?month=$currentMonth&year=$currentYear&submit=Get+Statistics!"; ?>';

3 Comments

Thanks, looks the same as what I tried except for the <?=, is that a typo or just something I've not seen before? I tried it anyway and it kills the page!
It's called short open tag, and it's not a good programming style. php.net/manual/en/ini.core.php#ini.short-open-tag I think I'm too used to codegolfing :P
cool, I knew you could do <? but never seen <?= . I used to use <? but turns out it can cause problems depending on the server as some can't process it so I always use the 'long' version now!

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.