Viewed   100 times

I am using PHP conditions and want to know if I can run a JavaScript function without some one have to click on it using JavaScript:

if($value == 1)     
{  
    JavaScript to run the function  
}

How would I do that?

 Answers

1

First of all keep in mind that your PHP code is evaluated on the server, while JavaScript runs in the browser on the client-side. These evaluations happen in different places, at different times. Therefore you cannot call a JavaScript function from PHP.

However with PHP you can render HTML and JavaScript code such that it is only rendered when your PHP condition is true. Maybe you may want to try something like this:

if($value == 1) {
   echo "<script>";
   echo "alert('This is an alert from JavaScript!');";
   echo "</script>";
} 
Thursday, December 15, 2022
 
1

Ok. I think can see the clear picture now.

No. it is not possible. You can not can not call client-side javascript method from server-side PHP script.

In web application there are to sides:

  • client (in your case its web browser, that works with html/css and javascript + data received from the server)
  • server side that works with whatever (in your case its PHP) and generates the html/css, javascript and data.

Now, your index.php on the server side is processed by the web server and produces index.html that is being sent by the server to the client. Client opens this file and loads/executes all the scripts,styles that are defined in it. And it creates document structure. All of it is now existing on the client side.

However, server side does not have a direct link to your client context. So the are two ways to run this method as a result of the server processing.

1) to send a ajax request to the server, and, based on the result, execute the method.

2) If you don't want to deal with ajax cal logic, you may do a script insertion approach. How it works, is that when the button is pressed you do something like this:

var scriptRef = document.createElement('script');
scriptRef.setAttribute('src','url_to_script.php');
document.head.appendChild(scriptRef);

What it will do, it will dynamically append a script tag to your document and load script content as a result of your php script output. There you can decide to launch the method or do something else.

However, script insertion is a bit of a headache and should not be used on a long-running pages (in my opinion), as it actually adds a new script every time you press a button. And its your responsibility to keep track of it and remove it.

Thursday, October 27, 2022
4

Well technically you could do what you want.

  • Find a command line JS interpreter, this might be a good starting point
  • Make a JS script that accepts input and produces output
  • Start doing whatever you are doing in php
  • Throw a $output = system('./jsInterpreter -param value', $retval); or similar
  • Do something with data

EDIT: Rhino seems to be a perfect match for your needs:

Predefined Properties

Scripts executing in the shell have access to some additional properties of the top-level object. arguments

The arguments object is an array containing the strings of all the arguments given at the command line when the shell was invoked.

Tuesday, August 9, 2022
 
2

If you don't want to use javascript, you can handle it via php. Take a look at this lib: http://code.google.com/p/php-mobile-detect/. And then you could do something like:

<?php
include 'Mobile_Detect.php';
$detect = new Mobile_Detect();

if ($detect->isMobile()) {
    header('Location: yourpage.php');
    exit(0);
}
Friday, October 21, 2022
1

Cookies are not the way to transfer variables between client and server. you should append key/variables pairs to your request URL using either a get (querystring) or post method.

jQuery ajax example;

$.get('http://www.myphpserver.com/script.php?row_id=' + NewCookieValue);
Monday, October 31, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :