Ruben Martinez Jr.
Ruben Martinez Jr.

Reputation: 3110

Set Call to PHP Function, with POST Variables as Parameters, as HTML Form Action

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]));
?>

Upvotes: 1

Views: 5878

Answers (4)

james murphy
james murphy

Reputation: 1785

I think you could use a hidden field on the form, and populate it with the name of the function you want to run on "destination.php". Then a switch statement on "destination.php" could pull the name of the function from POST variable.

Upvotes: 0

Marc B
Marc B

Reputation: 360732

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.

Upvotes: 1

Amrendra Oraon
Amrendra Oraon

Reputation: 1

One option is to put method="_the_main_php_file_containing_function_to_be_called_"
I hope it works fine.

Upvotes: 0

Quentin
Quentin

Reputation: 943650

What I am wondering is whether I can somehow skip the middleman PHP file, and simply call the function from my HTML file.

No. The client only knows about URIs.

A URI can map to a PHP program. Multiple URIs can map to the same PHP program. You can use logic to determine what functions to run for a given URI. You can't avoid having that logic in your program.

Upvotes: 1

Related Questions