简体   繁体   中英

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 ?

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.

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 eg with a Framework

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM