在提交之前验证HTML表单详细信息 - 使用php

在提交之前验证HTML表单详细信息 - 使用php

问题描述:

I had an upload form on my website, where users entered details about their upload file, selected the file and uploaded to the webserver.

The POST action of the form was to submit the file to a php page called upload_control.php, where the post details were validated, and if correct the file was stored on disk an entry was placed in the database. The file was renamed to ID_name before storing, where the ID was taken from the database, as the largest ID so far (just a counter).

Now things have changed and it makes more sense to upload the file to storage elsewhere. This is done straight by the user, the action of the form points to the other server which stores the file if the form was submitted correctly. [ I have no control over the processing done by the other server, it's a storage solution like amazon s3 ]

The problem is: How do I get the last used ID from my database, so that I rename the file to ID_filename with javascript before uploading? (I can store the filename on a hidden form field and the remote server will understand to rename it when it receives it).

Better yet: Is there a way to validate all the form details - using php. not javascript, before submiting the form to the storage solution?

My thoughts are towards sending the form details to a php script on my server with ajax, upon hitting the submit button but before posting, so that the php script can get the latest id from the database, validate the request, send back the new details or the new id, and then really submit the form. -- But how can this by done?

我的网站上有一个上传表单,用户输入了有关其上传文件的详细信息,选择了文件并上传到 网络服务器。 p>

表单的POST操作是将文件提交到名为upload_control.php的php页面,其中验证了帖子详细信息,如果文件存储在磁盘上则正确 条目放在数据库中。 该文件在存储之前被重命名为ID_name,其中ID是从数据库中获取的,是目前为止最大的ID(只是一个计数器)。 p>

现在情况发生了变化,这更有意义 将文件上传到其他地方的存储。 这是由用户直接完成的,表单的操作指向存储文件的其他服务器(如果表单已正确提交)。 [我无法控制其他服务器完成的处理,它是像amazon s3这样的存储解决方案] p>

问题是:如何从我的数据库中获取最后使用的ID,所以 在上传之前我用javascript将文件重命名为ID_filename? (我可以将文件名存储在隐藏的表单字段中,远程服务器将理解在收到它时重命名它。) p>

更好的是:有没有办法验证所有表单的详细信息 - 使用PHP。 在将表单提交到存储解决方案之前,不是javascript吗? p>

我的想法是使用ajax将表单详细信息发送到我的服务器上的php脚本,在点击提交按钮但发布之前, 这样php脚本就可以从数据库中获取最新的id,验证请求,发回新的详细信息或新的id,然后真正提交表单。 - 但是怎么做呢? p> div>

on button click call php function through ajax, and pass all the data collected from page to service as json, php is very strong at parsing json. and you can return result from that method to indicate whether to data is authenticated or not. A nice tutorial to hook you up is this.

http://www.queness.com/post/328/a-simple-ajax-driven-website-with-jqueryphp