0

I want to do operation to a database that is running on xampp in my index.html I have the following code

<script src="oPBD.php" async defer></script>
       <h1>This is a title</h1>
       <form action="opBD.php">
           <input type="submit" value="Do stuff" name="Button">
       </form>

And this works fine, howevewer I would like to put all the code in opBD.php in a function called function1 for say something and run in html when needed. But How I do that. I would like to use something like:

<script src="oPBD.php" async defer></script>
       <h1>This is a title</h1>
       <form action="opBD.funtion1().php">
           <input type="submit" value="Do stuff" name="Button">
       </form>

I don't find how to do this. Could you guys help me?

  • If the extension of the file is `.html` then it will not be passed to the PHP interpreter and therefore the PHP wont run – RiggsFolly Jun 20 '19 at 15:25
  • 4
    That is not possible. The action must be a *URL*. URLs don't have any notion of functions, which is a language-specific implementation detail. – deceze Jun 20 '19 at 15:28
  • But it works in the first case – Hector Gurle Jun 20 '19 at 15:29
  • 1
    Right, but in the second you attempt to call a function. – Jay Blanchard Jun 20 '19 at 15:29
  • Form `action` triggers an HTTP request to the URL → web server maps URL to file on disk → PHP is engaged with that file as argument → **PHP file** runs. There's no API or anything to pass a specific *function* through that chain. You can't even invoke PHP on the command line and tell it to run a specific function within a file. Always the entire file will run, period. – deceze Jun 20 '19 at 15:32
  • You could use a GET/POST Parameter and depending on that in the PHP file process the data in a different function. Putting the functionname into the filename will not work, because now the webserver will not know which file he shall open – nbar Jun 20 '19 at 15:32
  • https://stackoverflow.com/questions/12328354/calling-a-particular-php-function-on-form-submit/12328409 – Harshana Jun 20 '19 at 15:33

1 Answers1

-1

Can you just create a new file opBD.functionName.php and then use that in your form action? Otherwise your only real option is to use an MVC (Model View Controller) framework like Laravel or similar which would let you create real URLs for this kind of thing (eg. /user/update or something).

Matt Andrews
  • 2,868
  • 3
  • 31
  • 53
  • 1
    using a separate file in OPs case is not the worst idea. At least he would be able to use that file directly in the action parameter of his form-element. It's not the most elegant solution. But a beginner friendly one. – nbar Jun 20 '19 at 15:34