1

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]));
?>
6
  • 5
    Hello, 1990 called and wants their uppercase HTML back... And no, you cannot skip PHP. HTML is purely a client-side thing, and cannot talk to a database in any way/shape/form. That's why there's PHP. Commented Apr 8, 2012 at 18:08
  • 1
    Why don't you make the HTML a PHP file, and call the insert method if the POST variables are set. Commented Apr 8, 2012 at 18:10
  • @MarcB I don't mean I want to get rid of the PHP entirely, but the small 3-line PHP file...It seems like I could probably combine it with the existing HTML file (that is actually a .php, though I primarily use HTML). And sorry, I'm a beginner with HTML, and this way the HTML tags stick out to me...Doesn't make a difference though, really. Not like clients are going to see it. Commented Apr 8, 2012 at 18:38
  • Joking aside, I agree with @MarcB - it's useful to standardise on a number of approaches, especially if other people will work with your code (now or in the future). Lower-case has been the norm for a good ten years I should think. Ditto using quotes for attributes rather than apostrophes (although I believe apostrophes still constitute valid (X)HTML). Commented Apr 8, 2012 at 18:59
  • Good point. I will take this into account. As for the apostrophes, I try to only use them when there is PHP code, as PHP gets really fussy about apostrophes and quotation marks (you can't do """", but '""' or "''"). Commented Apr 8, 2012 at 19:11

4 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

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

1 Comment

And then try to make call in the main php file itself. If you are somehow unable to do this, then I think you can not skip this middle file
0

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.

1 Comment

Please demonstrate your answers.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.