Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6096565
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:56:33+00:00 2026-05-23T12:56:33+00:00

I have a PHP variable thePHPvar in a file DoStuff.php in my website. thePHPvar

  • 0

I have a PHP variable thePHPvar in a file DoStuff.php in my website.
thePHPvar gets set to a string.

Then after that the code in DoStuff.php does

header("Location: http://localhost/theWebite/index.php");

I need 2 things in my onLoad handler inside the body tag of index.php:

<body onload="doLoad()">

My doLoad() function must be able to do 2 things:

1) get access to ‘thePHPvar’ variable, which again is a string (a path name) that was set in PHP code in DoStuff.php just prior to the header() redirect.

2) also, doLoad() must be able to detect when ‘thePHPvar’ is empty so it can avoid trying to use
‘thePHPvar’ and skip the logic that uses that string.

I’m new to php and javascript and have impressed myself with getting working what I have now,
but I’ve spent 1/2 day on this, read lots of similar issues on SO.

I have to be able to redirect back to index.php, grab this string variable and if it’s been set (not null), execute some javascript code.

Right now this is all my onLoad() does, and hey — it works. But not the right way, yet.

<head>
<script type="text/javascript">

function doLoad()
{
   alert("Page is loaded");
}
</script>
</head>

Without introducing a bunch of new stuff I’d have to learn like Ajax etc., how can I get this done in javascript, html, php and what I have here?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T12:56:34+00:00Added an answer on May 23, 2026 at 12:56 pm

    You can use a session variable as Jorge said, if this myPHPVar is specific to a user. Otherwise, pass the variable in your header redirect:

    header("Location: http://localhost/theWebite/index.php?myVar=" . $myPHPVar);
    

    As you’re a beginner, you probably won’t want to use JSON to get the variable from PHP to Javascript, but do look it up if you ever have to pass a lot of data to Javascript code.

    <script>
        var myPHPVar="<?php echo $_GET['myVar']; ?>";
    </script>
    

    If the user has any influence over that PHP variable, be sure to sanitise the variable against XSS attacks.

    To check if myPHPVar is empty in your doLoad method, just use if (!myPHPVar) { .... }

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have PHP variable that contains string like: http://domain.com/uploads/image1.jpg|||http://domain.com/uploads/image2.jpg|||http://domain.com/uploads/image3.jpg|||... I need to get first
I have a PHP function that takes a variable number of arguments (using func_num_args()
I have a multiple language website, and I use a php get variable to
I have the following string in the smarty (php templating system) variable $test: <img
In my PHP code I have a date in my variable $postedDate. Now I
I want to have a string variable for a PHP class, which would be
I have a php file that I am trying to send the output in
I have a php variable ( $list ) that is a list of values
I have a php variable that comes from a form that needs tidying up.
I have a PHP file that contains a button. When the button is clicked

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.