A common problem is that for validation you need to run the same code on the server and on the client. On a JavaScript heavy page there are many other function you like to share between both sides. Since you can't run PHP on the client I wounder if it's possible to run a Javascript function from PHP. I am thinking about server-side JS like Rhino but I have no idea how to include it to PHP.
-
1You can store the validation options in JSON. I mean, you should have a minimal codebase in PHP and JS that can run validation based on these options, and the options can be shared. – kapa Jun 20 '12 at 13:35
-
1if you need run same code on the server and on the client, use AJAX – AlexeyKa Jun 20 '12 at 13:40
-
`A common problem is that for validation you need to run the same code on the server and on the client` Not exactly the same and your PHP should be more secure than your JS – vol7ron Jun 20 '12 at 13:56
-
@vol7ron If you don't have the same validation the form might tell you it's valid but it fails on the server. So it's better to have them equal. – PiTheNumber Jun 20 '12 at 14:11
-
1@PiTheNumber, you want them close, but not necessarily the same. For instance, you might have a looser security model on the front end, where if a user can get to the page, they can submit the form. Whereas, you may need to re-validate the user submitting the form on the backend. -- I agree, many times it seems double the work, but the good news is that js translates into perl/php relatively easily (asside from object syntax). – vol7ron Jun 20 '12 at 14:15
-
@vol7ron sure you might add some extra checks in PHP but I think it is bad to translate the validation from Javascript to PHP even if you could just copy it. Because that creates duplicated code and that is bad style. – PiTheNumber Jun 20 '12 at 14:20
-
@PiTheNumber: `Because that creates duplicated code and that is bad style.` Everything has a time and a place. If your code is really that independent, then you might consider compiling it, perhaps using C – vol7ron Jun 20 '12 at 16:21
4 Answers
PHP has a V8 extension: http://php.net/manual/en/class.v8js.php never tried it that much, but might be useful
EDIT: here's the pecl package link: http://pecl.php.net/package/v8js

- 7,062
- 1
- 33
- 46
You could use a server side javascript service i.e. node.js and call out to it using PHP, but it would not be a straight forward or trivial solution to the problem. It would probably only be worth it if it was a large application there was an awful lot of crossover in the validation logic.

- 5,685
- 32
- 33
Try this in php:
echo "<script type='text/javascript'>
myfunction();
</script>";

- 76
- 8
-
That will execute on the client side. I don't want to run validation only one the client side that would be kind of bad. – PiTheNumber Jun 20 '12 at 14:16
I feel the same as AlexeyKa.
Rather than running JavaScript on both client and server, I think you need to go the opposite route. Create your validation code on the server-side and use Ajax to call the validation before the form submits. That way you are using the same code to validate (no duplications) and you don't have to worry about running a JavaScript engine on your server.
Also, if you give the client the validation code that you will then run on the server, you're just asking for someone to find a hole in your validation code. Keeping validation on the server can keep your validation code away from users.
Update:
I didn't realize your question included more than validation. This isn't a PHP solution, but Google's Closure Tools is a technology that allows your client to be the same as your server. You write UI templates and define how they should work (layout, validation, persistence) and the framework creates the HTML, JavaScript and server code. This is all to ensure that you write code once and any code that is similar between client and server is handled by the framework.

- 1
- 1

- 5,891
- 4
- 36
- 55
-
Why would it be bad to run JavaScript on the server? AJAX would be a elegant solution for a few lines of validation, indeed. But I am not only thinking about validation. On a JavaScript heavy page there are many function you like to share. – PiTheNumber Jun 21 '12 at 07:53
-
@PiTheNumber - It's not 'bad', but I was really only thinking of validation. Without examples, I don't really see what other things you would want to share between the client and server. In my experience, JavaScript is used to manipulate the view and shuttle data back and forth from the server. I don't tend to have functionality in JavaScript that I would like to see on my server. – RustyTheBoyRobot Jun 21 '12 at 13:34