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.
Related
I've been doing a lot of ajax calls and using the returned data to build html with javascript. However, I've noticed some people are returning the constructed html in the ajax calls since they're doing it all in php.
What is the preferred method? I have a bunch of stuff already using javascript, so I guess I would prefer not changing everything to use just php. But, I'm assuming php would be more "secure."?
The following is what I've been doing:
$main_frag = $("<div class='order-container'/>");
$contact_frag = $("<div class='group'><div class='line-data'>Name: "+data.name+"</div><div class='line-data'>Email: "+data.email+"</div><div class='line-data'>Phone: "+data.phone+"</div></div>");
$address_frag = $("<div class='group'><div class='line-data'>Address 1: "+data.address_one+"</div><div class='line-data'>Address 2: "+address2+"</div><div class='line-data'>City: "+data.city+"</div><div class='line-data'>Province: "+data.province+"</div><div class='line-data'>Postal Code: "+data.postal+"</div></div>");
etc..
I just want to hear the opinions of the community.
Look at some Javascript template solutions, such as Underscore.js (http://underscorejs.org/). Which is just an example, there's many. This should give you more separation and flexibility than just JQuery.
Also for more complex Javascript logic, you should consider to start using some Javascript MVC framework, such as Backbone.js (http://backbonejs.org/). Again there's many.
For browser / server communication, you can use JSON.
I don't think that retrieving them in parts or as a whole structure will have difference. both of them can be changed either via MITB ( Man in The Browser ) or even simple using developr tools that let you modify JS and HTML in the Browser..
IMO mustache is a good choice: http://coenraets.org/blog/2011/12/tutorial-html-templates-with-mustache-js/
with mustache you can use templates within your javascript to generate the markup
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.
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!
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.