最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Same code on browserserver - Stack Overflow

programmeradmin6浏览0评论

I'm writing a web app using PHP, and running into several situations where I need similar code on the server and browser. For example:

  • I want to validate user input, and generate the same error messages on both sides.
  • I want to format data using certain rules (e.g. if a given field is less than 1, show it with two decimal places, otherwise none), and have it appear the same regardless of which side renders it.

It seems like this should be a mon problem, as people are moving more logic from the server to the browser. But are there any mon patterns or libraries for dealing with it (particularly for PHP)?

I'm writing a web app using PHP, and running into several situations where I need similar code on the server and browser. For example:

  • I want to validate user input, and generate the same error messages on both sides.
  • I want to format data using certain rules (e.g. if a given field is less than 1, show it with two decimal places, otherwise none), and have it appear the same regardless of which side renders it.

It seems like this should be a mon problem, as people are moving more logic from the server to the browser. But are there any mon patterns or libraries for dealing with it (particularly for PHP)?

Share Improve this question asked Jan 26, 2010 at 20:21 JW.JW. 51.7k39 gold badges122 silver badges151 bronze badges 2
  • 3 Good question, although strictly speaking, moving validation from the server to the browser is a bad idea for security. Better to "pre-validate" in the browser (for immediate user feedback) and re-validate on the server (for security). – Nathan Long Commented Jan 26, 2010 at 20:24
  • Absolutely -- I want it in both places. – JW. Commented Jan 26, 2010 at 20:25
Add a ment  | 

7 Answers 7

Reset to default 3

One solution would be to use server-side JavaScript. You'd be able to share huge chunks of code then.

Interesting question! As far as I know there are 2 options for PHP:

  • Use Ajax to do form validation, so everything happens on the server.
  • There is this effort to bring PHP functions to JS, which should at least ease the porting: http://phpjs/

In Java however, you can run Javascript with Rhino.

The other things I can think of are ugly hacks with Flash or Java applets, or involve a framework like Pyjamas.

You may try to use haxe language. It could be translated in js/php and even actionscript (flash).

A partial solution is AJAX. For example, write your validation code once on the server, and let your forms talk to the server to see if an input is valid and ask for an error message to display if not.

json_encode and json_decode are a great start - the same source of data available in php arrays and json objects as required.

the json_encoded data can be served through <script> tags or ajax as you see fit.

Do fast validation client-side (like pattern-matching, date&number correctness) and security check server-side.
Also you can use AJAX to do server-side validation but this way you can have a responce gap between user input and validation result. So use JavaScript for fast and basic validation and do heavy one with PHP.

You could use http://phpjs/ to run you php code on the client side. This works for most functions in php.

发布评论

评论列表(0)

  1. 暂无评论