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 7987629
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T12:12:12+00:00 2026-06-04T12:12:12+00:00

I have some Condition at PHP and if it’s true i need php variable

  • 0

I have some Condition at PHP and if it’s true i need php variable to get javascript function:
Something like that:

<?php
if(x == true){
$x = <script type="text/javascript">xfunction();</script>;
}
?>

I don’t really know how to get them to combination.

  • 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-06-04T12:12:13+00:00Added an answer on June 4, 2026 at 12:12 pm

    To invoke a function onload when rendering your document.. you can just echo out a self executing function.

    <?php 
     // your other code
     if( $x ) { // no need to check if true... this will fail if falsey 
       echo "<script>";
       echo "(function() { xfunction(); }());"; // I will execute when the parser hits me
       echo "</script>";
     }
     // or (after reading your comments above) 
     if( $x ) { 
       $x  = "";
       $x .= "<script>";
       $x .= "(function() { xfunction(); }());"; // I will execute when the parser hits me
       $x .= "</script>";
     }
     // somewhere else in the document.... 
     echo $x;
    

    or if you are trying to pass values from javascript to php you will need to make use of HTTP Request variables (POST, GET) or XHR…

    Example of the XHR:

    var request = new XMLHttpRequest();  
    request.open('POST', 'http://www.somepage.com/foo.php', true);  
    request.send('data=somedata');  
    
    if (request.status === "200") {  
      console.log(request.responseText);  
    }  
    

    and on the php end of like access it with $_POST['data']

    More reading can be done about PHP Request variables here

    More information regarding XHR can be read here

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

Sidebar

Related Questions

I have a script that I want to exit early under some condition: if
I need to remove some Javascript code based on a server side condition (PHP).
I have a drop down list box like following. Under some condition, in the
I need to query the MYSQL with some condition, and get five random different
Let's say I have some code like this: <html> <head><title>Title</title></head> <body> <?php if (!$someCondition){
I have some (php) code that performs critical updates on an InnoDB table. I'm
I have a condition in php based on some session variables. If condition passes,
I have some HTML that I need to sometimes wrap in an anchor tag
I have two files : index.php condition.php condition.php hold some switch statements. I wrote
I have some weather xml that I am trying to parse via php loop.

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.