doujingjiao0015 2009-09-07 17:16 采纳率: 0%
浏览 39
已采纳

使用PHP验证表单信息的逻辑

Im trying my first form validation with PHP.

I need some guidance with the logic.

I have purchase.php (which has the form) and review-purchase.php(which sets SESSION variables and displays the user data inputted)

If any of the fields fail validation I don't want the user to get to review-purchase.php

Should I be sending the user to the review-purchase.php script, checking validation there and then redirecting back the purchase.php with an error message?

or

should I be using an if/else statement with $_SERVER['php_self'] etc in the form action="" and keep all the validation on the purchase.php page itself and only letting purchase-review run if everything passes validation?

Sorry for the confusing question but i myself am very confused...

  • 写回答

3条回答 默认 最新

  • douzhi4311 2009-09-07 17:29
    关注

    Well, it seems you have a misconception about where and when PHP code is executed. If you want to validate user input on the server side - with PHP (and you should because any JavaScript validation on the client can be worked around by a nefarious user) - the PHP validation can only occur after the user has posted data. That is no matter to which page the user posts the data - be it the original form or a different page.

    So, in your situation if you want users to go to a page if validation is successful and to a different page is validation fails yo will need to do a redirect anyway.

    In this case you have two paths:

    • user requests Purchase.php and fills out the form
    • user posts data to validation page
    • if data is valid -> display purchase review information
      else -> re-display form page and have user re-enter data

    So if Purchase.php posts to itself, you can validate there and redirect to review.php only if data is valid. Which means that in the successful case you do 2 redirects and in the failed case you do only 1 post.

    On the other hand, if you post directly to review.php and you validate there, you have 1 post in the successful case, and 2 in the failed case.

    The above is true no matter how you spin it - unless you use the same URL for the form and the review, in which case you can put logic in the same place to do the form, validation and purchase review in the successful case.

    I hope this helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题