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(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入