Is there any way to open a new window or new tab using PHP without using JavaScript.
Nope, a window can only be opening by adding target="_blank" attribute (invalid in Strict (X)HTML, but valid in HTML5) or using JavaSript's window.open(url '_blank').
PHP runs server side - therefore it can generate the HTML or JavaScript, but it can't directly interact with the client.
Short answer: No.
PHP is a server side language (at least in the context of web development). It has absolutely no control over the client side, i.e. the browser.
No. PHP is a server-side language, meaning that it is completely done with its work before the browser has even started rendering the page. You need to use Javascript.
No, there is not.
No, PHP is server-side scripting
PHP is a server-side language, it's what produces all the code you see on a page when you choose View Source. PHP cannot affect the client on its own, it needs to do it through a language such as JavaScript.
PHP is server-side, as everyone states, however you can add a target="_blank" attribute to your form tag. This doesn't perform any work server side, but does let you submit the form to a new window to be processed on the server.
A neat trick, but 1) deprecated in HTML Strict and 2) rarely useful.
This answer is dedicated to the How to call a JavaScript function from PHP? thread; you can execute this block of code:
<?php
echo "<script> window.open(\"about:blank\"); </script>";
?>
Hopefully this helps!
Related
is it possible to run some javascript expression? for example echo eval("Math.sqrt('25')");
In normal situations :
PHP runs on the server
and, then, Javascript is run on the client, in the browser.
So, no, it's not quite possible to have PHP execute some Javascript code on the server.
But there is at least on PHP extension that embed (or wrap arround) a Javascript engine, and, as a consequence, allows one to execute Javascript on the server, from PHP.
The extension I'm thinking about is the spidermonkey one : installing and enabling it on your server will allow you to execute Javascript code, on the server, from PHP.
Of course, like any other PHP extension, you'll need to be admin of your server, in order to install it -- and this one is never installed by default, as it answers a very specific need.
About this extension, I have never seen it used in real situations, and there are not many people who tried it... here are two articles you might want to read :
Using JavaScript in PHP with PECL and SpiderMonkey
and SpiderMonkey : Exécuter du Javascript côté serveur, depuis PHP (this one is in french, and on my own blog)
Try this
echo "<script language='javascript'> Math.sqrt('25') </script>"
There is also the J4P5
I don't know if it's still maintained but you can always fork it, it's released under the GPL license.
put your php into a hidden div and than call it with javascript
html / php part
<div id="mybox" style="visibility:hidden;"> echo sqrt(25); </div>
javascript part
var myfield = document.getElementById("mybox");
myfield.visibility = 'visible';
now, you can do anything with myfield... like this
alert(myfield);
Since PHP is a server-side scripting language that runs on the server and Javascript is a client-side scripting language that runs in a browser you would have to have the PHP generate Javascript code (the same way it generates HTML) that gets executed after the page is loaded.
echo sqrt(25);
See:
http://php.net/manual/en/function.sqrt.php
Run JavaScript code from PHP
php v8js: https://github.com/phpv8/v8js
$v8 = new V8Js;
$v8->executeString("Math.sqrt('25')"); // 5
https://github.com/chenos/execjs
use Chenos\ExecJs\Context;
$cxt = new Context;
$cxt->eval("Math.sqrt('25')"); // 5
if you have node installed on your server then you can exec this command with php
node a.js
and then use the console.log as output.
Let say I have this JS code:
function plus2(){
print (2+2);
};
So I want to post this code into textarea#input at http://dean.edwards.name/packer/ and then get the result back from textarea#output.
Can use PHP Curl, Shell Curl or JQuery to do the job?
P.S.: By the way, there is bug in PHP Packer port and that's whay I am not using it.
PHP implementation of JSMin works well for me. Also, if you have server-side JS interpreter you can use UglifyJS, it's fast and provides a good compression.
Not really, no. It would need to be passed to a JavaScript engine at some point, so PHP/cURL is not going to be enough.
I recommend using an alternative server-side implementation of packer, or perhaps even YUI Compressor.
It's pretty simple. i'm calling an PHP script from my flash using navigateToURL. It runs the script and opens a new window in my browser
It's possible to just call the php script from AS3, without open a new window ?
Thanks.
Are you looking to download information from the PHP script in to your app?
If so, you might be looking for URLLoader.
http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/net/URLLoader.html
You can specify the target like so:
navigateToURL(myRequest, '_self');
Hope that helps!
flash.net.sendToUrl() may be of use.
http://www.adobe.com/livedocs/flash/9.0/ActionScriptLangRefV3/flash/net/package.html
I am trying to control stuff with PHP from keyboard input. The way I am currently detecting keystrokes is with:
function read() {
$fp1=fopen("/dev/stdin", "r");
$input=fgets($fp1, 255);
fclose($fp1);
return $input;
}
print("What is your first name? ");
$first_name = read();
The problem is that it is not reading the keystrokes 'live'. I don't know if this is possible using this method, and I would imagine that this isn't the most effective way to do it either. My question is 1) if this is a good way to do it, then how can I get it to work so that as you type on the page, it will capture the keystrokes, and 2) if this is a bad way of doing it, how can I implement it better (maybe using ajax or something)?
edit: I am using PHP as a webpage, not command line.
I'm assuming that you're using PHP as a web-scripting language (not from the command line)...
From what I've seen, you'll want to use Javascript on the client side to read key inputs. Once the server delivers the page to the client, there's no PHP interaction. So using AJAX to read client key inputs and make calls back to the server is the way to go.
There's some more info on Javascript and detecting key presses here and some info on how to use AJAX here.
A neat option for jQuery is to use something like delayedObserver
If you are writing a CLI application (as opposed to a web application), you can use ncurses' getch() function to get a single key stroke. Good luck!
If you're not writing a CLI application, I would suggest following Andrew's answer.
Try readline:
http://us3.php.net/manual/en/function.readline.php
I need to fire a php class from a javascript function.
code:
<input type="button" name="Submit" value="Submit" class="opinionbox"
onclick="verifyControl('<?=$control_no?>')"/>
function verifyControl(rNo) {
Cont_no=document.getElementById("ContNo").value;
if(rNo==Cont_no) {
frames['frame1'].print();
showPage('payment');
}
else if(rNo!=Cont_no) {
alert("invalid control no");
}
}
i need to run the code
$data = $obj_com -> getSelectedData('tbl',
'control_no', $contno);
$control_no = $contno;
$obj_com -> recordPay('tbl',$contno);
inside the verifyControl() how can I do this?
You cannot "call" a PHP class from Javascript because Javascript is run on the client side (ie, the browser) while PHP is run on the server. What you can do, however, is call a PHP script asynchronously, get its output, and do fun stuff with javascript. This is known as AJAX. If you're going to go down this road, you are highly advised to use a library like jQuery and learn from there. Here are a few questions to get you started (check out the answers):
How to dynamically call a php function in javascript
Javascript and PHP functions
To call PHP code from Javascript, given that PHP is executing on the server and Javascript is executing on the client, you will need to set up some sort of interface at the server that can be accessed remotely.
You may also want to be aware of the security implications of doing so. In particular, if you want to ensure that only your users will be calling your server in this way - that is, if a malicious user calling this code could do damage, you will need some sort of authentication.
You will also need to decide on a protocol for communicating between the client and server.
Protocols such as SOAP and XML-RPC define everything you need to remotely call procedures on the server. Or you can roll your own, just by calling a certain URL and receiving a certain result, in a certain format (JSON can help) from the server.
you can use Brent Ashley jsrsClient.js or $.ajax of jQuery Javascript lib.