answersLogoWhite

0


Best Answer

You cannot do that, since PHP is serverside and javascript is clientside. The best way is usually to have an ajax layer (a php file) to handle requests from the main php file. For example: You would have 3 files index.php, ajax.php, and functions.js The index.php is what people would see, and would include the functions.js javascript file. The functions.js file would then send requests to ajax.php. These requests could be a search query, a login request, etc... or any other object for that matter. Accessing javascript objects on demand is not possible though.

User Avatar

Wiki User

14y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you call JavaScript object in PHP?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

How can you use a JavaScript variable in the PHP isset function?

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.


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 insert PHP code into JavaScript?

PHP is server-side code which means that the code is executed on the web server. The pages are dynamically created and sent to the user's browser. JavaScript is a client-side code which means that it runs on the user's computer after the page has been sent from the server. It is possible to combine PHP commands within JavaScript commands. You would mainly use this in setting up starting variables for your JavaScript. Example: ; …etc --> Another option is that you may consider using Ajax which allows you to dynamically call server-side code from JavaScript.


How do you shortcut systemoutprintln in javascript?

There is no systemout object in JavaScript. Do you mean Java?


Is PHP a general purpose programming language?

I defiantly wouldn't call it General Purpose but if you are fluent in PHP then there is pretty much nothing you can't do in PHP (provided you understand HTML, CSS and JavaScript). I've used PHP for items which I could have made via programs such as Access and Visual Studios but PHP is more first hand and easier for me. One good point to programs in PHP is because they are Server-side it should work for all Users.

Related questions

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.


Does JavaScript interact with PHP?

Yes, JavaScript and PHP can interact with each other in web development. JavaScript is a client-side scripting language that runs in a user's browser, while PHP is a server-side language that runs on the server. The two languages can communicate with each other through the use of AJAX (Asynchronous JavaScript and XML) requests. JavaScript can make an AJAX request to a PHP script on the server, and the PHP script can process the request and return data back to the JavaScript code. This allows for dynamic, interactive web pages that can update their content without requiring a full page refresh. Overall, JavaScript and PHP are complementary technologies that can work together to create powerful web applications


How do you create a text editor in PHP?

Well it got nothing to do with PHP, you going to need JavaScript to do that :)


How can you use a JavaScript variable in the PHP isset function?

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.


How do you add code for printing document in php code?

You cannot call the print function from within PHP code. This is mainly because the PHP code is run server-side, and the printer is firmly part of the client's individual setup. That said, you can use JavaScript to politely ask the browser to print the document. The call will cause the browser to bring up its default print dialog. The specific JavaScript method you're after is window.print();


How do you use the JavaScript confirm function in PHP?

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


What programming language is used for webOS applications?

HTML5, JavaScript, CSS


Why should you consider placing javascript in an external file?

If you are using PHP for creating a website, it will turn into a mess if you don't use separate files for CSS & Javascript. It is not that much of a problem as external javascript functions are not that difficult to call from html or php files. The code looks neat and we can differentiate the problem areas more accurately. The problem obviously resides when you use ASP.NET & call external javascript files. ASP.NET assigns different client side and server side ids for controls. It maybe a good idea there to discard the external coding of javascript if javascript functions are not called


How do you connect javascript using php?

Any communication between Javascript code and PHP code needs to be instigated by the Javascript. This is because Javascript is a client side script and PHP is server side. The server can't force data to the browser; it can only reply to requests. The technique of using Javascript on the client side to talk to server side scripts is referred to as AJAX (Asynchronous Javascript And XML). There are several libraries available for simplifying it's usage, and many online tutorials.


How can you close the iframe in calling page in php and javascript?

Using JavaScript, you can accomplish this by deleting the iframe node from the DOM.


How does form validation differ in JavaScript and PHP?

In JavaScript the validation is done client side, which means it can be easily bypassed by turning JavaScript off in your browser. But with PHP it is server side, which means you can't switch it off, so you can't bypass the validation.