dongshi1914 2014-02-18 10:09
浏览 52

在表单的action属性中内联php脚本

Why does this not work?

<form action="<?php echo 'test'; ?>"><input type="submit"></form>

Why is it that to echo 'test', I would need to place the PHP into a separate file, and point the action attribute to it? If I have multiple forms with short PHP scripts, can I not simply inline them somehow to carry out tasks, instead of making many external files to do so?

My intention is to have a sort of control panel to make different types of changes to a database.

  • 写回答

2条回答 默认 最新

  • dtgr6303 2014-02-18 10:12
    关注

    You cannot place code in the action attribute, that's simply not how HTML works.

    <form action="index.php">
    <input type="hidden" name="do" value="dotest">
    

    You will need a router in your application that dispatches the tasks. In this case the "do" query parameter will dictate what to do. The code can be a big switch statement (not recommended), or you can store the actions in separate files/classes/class methods. You can look up the code directly (file_exists/method_exists/autoloading...) or you can decouple the lookup and do some kind of pre-registration and store the actions in a dictionary(array).

    I would recommend checking some PHP frameworks too get some ideas. There are some very clever routers and controller abstractions out there. It's going to be better than you can come up with, especially if you're unexperienced.

    Note: there's nothing wrong with lots of small files - just try to use a single entry point for your web application.

    评论

报告相同问题?

悬赏问题

  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?