dongyou6909 2012-04-08 18:06
浏览 15
已采纳

too long

I currently have an HTML file, with a form in it, that when submitted POSTs the form & calls a simple short PHP file that calls a function within another PHP file using the POSTed variables as parameters. The files are both below. What I am wondering is whether I can somehow skip the middleman PHP file, and simply call the function from my HTML file. Ideally, this would set the call to the function:

insert_PE(new PE($_POST[Date],$_POST[Participant],$_POST[Time],$_POST[Result],$_POST[Notes]));

as the form action. Does anyone know how/if this can be achieved?

HTML:

<FORM ID="FORM1" METHOD="POST" AUTOCOMPLETE="off" ACTION = "writeToDL.php">
    <INPUT TYPE="hidden" NAME="Date" STYLE="WIDTH:0px; " MAXLENGTH="8" TITLE="Enter Date" Value="<?php $dt = date('Y-m-d'); echo $dt ?>"/>
    <INPUT TYPE="text" NAME="Time" STYLE="WIDTH:70px; " MAXLENGTH="7" ONCHANGE="validateTime();" />
    <SELECT NAME = "Result">
        <OPTION VALUE = OK></OPTION>
        <OPTION VALUE = C>C</OPTION>
    </SELECT>
    <SELECT NAME = "Participant" STYLE = "WIDTH: 187">
        <OPTION SELECTED VALUE = "">Select...</OPTION>
            <?PHP
                $allParticipants = getall_participants();
                foreach($allParticipants as &$value) {
                    $val = $value->get_id();
                    echo "<OPTION VALUE='",$val,"'>";
                    echo $value->get_first_name()," ",$value->get_last_name();
                    echo "</OPTION>";
                }
            ?>
    </SELECT>
    <TEXTAREA NAME='Notes' COLS='28' ROWS='5'></TEXTAREA>
    <INPUT TYPE="image" SRC = "images/submit.png" VALUE="Submit Participant"/>
</FORM>

PHP File:

<?php
    include_once('database/PE.php');
    insert_PE(new PE($_POST[Date],$_POST[Participant],$_POST[Time],$_POST[Result],$_POST[Notes]));
?>
  • 写回答

4条回答 默认 最新

  • doujie4344 2012-04-08 19:06
    关注

    You COULD do something like this:

    <?php
    
    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
        include_once('database/PE.php');
        insert_PE(new PE($_POST['Date'],$_POST['Participant'],$_POST['Time'],$_POST['Result'],$_POST['Notes']));
    } ?>
    <html>
    ... rest of your page here ...
    </html>
    

    That way the PHP code only fires if an POST was actually performed. Some would suggest checking for the presence of a form field, but that's unreliable - you might change the form down the road and forget to update the if(). Checking the request method is guaranteed to be 100% reliable.

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

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?