answersLogoWhite

0

You can't actually do that. There is no direct way to make JavaScript code talk to PHP code, as the two languages are interpreted in different locations. The PHP is interpreted by the server, and the JavaScript is interpreted by the client. This means it's easy enough to transfer data from PHP to JavaScript (by generating the JavaScript with the PHP), but not the other way around.

If you're simply looking for a way to see if a JavaScript variable is set (from within the JavaScript itself), that can be done with a line like this one:

if(myVariable !== undefined){ /* do stuff */}

If you actually want to handle it on the PHP side, one way to do so would be to use additional PHP code when that happens. For example:

<?php

$jsVars = array();

?>

<script type="text/javascript">

var foo = 'bar'; <?php $jsVars['foo'] = 'bar';?>

var yub = 'nub'; <?php $jsVars['yub'] = 'nub';?>

</script>

... You can then check to see whether a certain variable has been set by seeing if it's in that array:

<?php

function jsIsset($varname){

global $jsVars;

return array_key_exists($varname, $jsVars);

}

?>

This however, only works when the JavaScript is generated, not when it's interpreted by the client system. For example, imagine you have a variable that is defined by a JavaScript function that is called from an onclick event. By the time that event happens, the page has already been served and the PHP is done executing.

If you want the JavaScript to tell the PHP that a variable is defined, you would need to do it through an AJAX request, which I believe is beyond the scope of this question.

User Avatar

Wiki User

14y ago

What else can I help you with?

Related Questions

What is isset in php?

isset( $var ) is a PHP function which returns TRUE or FALSE depending on whether a specified $var has been assigned to any value (or, initialized).


How can you declare the variable in PHP?

Variables in PHP do not need to be declared like some languages (e.g. JavaScript). This is all that needs to be done to assign a variable: $variable = "Value";


How do you use the JavaScript confirm function in PHP?

It has to be done with Javascript. Though you could do something like this:


How do you equate a php variable to a javascript variable?

Ideal thing would be to retrieve the value from PHP using AJAX and then assigning it to a java script variable. Thereafter compare it to the java script variable that is already present.


How do you use a function with a button in PHP?

A simple function call &lt;html&gt; &lt;body&gt; &lt;?php if(isset($_POST['button'])) { setValue(); // Function is called } function setValue() { echo "&lt;br&gt;The button property to call PHP function works"; // Your code here } ?&gt; &lt;input type="submit" name="button" onclick=&lt;?php $_SERVER['PHP_SELF']; ?&gt; /&gt; &lt;/body&gt; &lt;/head&gt;


How do you create static variables in PHP?

Use the "static" keyword to declare a static variable within a function like shown below. &lt;?php function fun() { static $variable; static $another_variable = ''; } ?&gt;


How do you get a cookie on a certain page?

Its an easy PHP code


How do you clear a variable in PHP?

You can unset a variable in PHP by setting it to NULL. You can set a variable to NULL by declaring it normally, or using the function "unset()". Both methods are acceptable, and accomplish the same thing. Examples of both: ---- $variable = NULL; ---- unset($variable); ----


How do you pass a variable by value in php?

In the declaration of the receiving function, you add an ampersand. &lt;?php function myWayCoolFunction( &amp;$params) {.....} $x = array('1','2','3'); myWayCoolFunction($x) ?&gt;


How do you create a function in HTML?

You don't. You can only create it in Scripting languages like Javascript or Server Side languages like PHP.


What are the similarities between PHP and JavaScript?

there are not many similarities, except the if and else statements, loops, etc... but PHP talks to the server, JavaScript can't. That's why both languages have totally different functions. If you only use basic JavaScript, PHP doesn't differ too much.


How can you pass a JavaScript value into a PHP file?

JavaScript lives in the browser, PHP lives on the server.To pass a value from JavaScript to a PHP page, you can eitherdo an HTML form submituse AJAXIn both cases, the value is sent to the server and you write your PHP to handle it and send a response.The important thing is that the JavaScript value won't be available in PHP when you first generate the page, creating the page and handling the value submitted from the browser are two distinct steps.