Philiphorst
Philiphorst

Reputation: 45

php function call direct as html form action

I have found that following HTML form code

<form action="<?php foo(bar)?>" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="submit">
</form>

works fine when foo(bar) is a function available in the current scope.

I am very new to php and html and couldn't find any documentation of this functionality online. Are there any downsides of using <?php foo(bar)?> instead of calling a script file foo.php?

Upvotes: 0

Views: 4728

Answers (2)

Daniel Gasser
Daniel Gasser

Reputation: 5133

This wouldn't work. The server is executing PHP before sending HTML to the browser.

But here's an idea:

<?php

function foo($bar)
{
    echo $bar;
}
if (isset($_POST['func'])){
    foo($_POST['file']);
}
?>
<form action="<?php $_SERVER['PHP_SELF'] ?>" method="post" enctype="multipart/form-data">
   <label for="file">Filename:</label>
    <input type="file" name="file" id="file"><br>
    <input type="hidden" name="func" id="file" value="foo">
   <input type="submit" name="submit" value="submit">
</form>

But IMO a better way would be to use CURL or other url-catching-and-pointing-to-controller systems, like e.g. with a Framework

Upvotes: 0

Fractaliste
Fractaliste

Reputation: 5957

The PHP function will not be called as a HTML form action.

Actually the PHP function will be executed before the server send the form to your browser, so the form's action will be the value printed by the foo function if it did.

Just open the web page's sources received from your server, you shouldn't see anymore PHP code.

Upvotes: 1

Related Questions