I am trying to run a function from a PHP script in the form action.
I'm not sure I understand what you are trying to achieve as we don't have what username()
is supposed to return but you might want to try something like that. I would also recommend you don't echo whole page and rather use something like that, it's much easier to read and maintain:
This will post the results in the same page. So first time you display the page, only the empty form is shown, if you press on submit, the textfield field will be in the $textfield
variable and you can display it again as you want.
I don't know if the username()
function was supposed to return you the URL of where you should send the results but that's what you'd want in the action
attribute of your form. I've put the result down in a sample paragraph so you see how you can display the result. See the "Your username is..." part.
// Edit:
If you want to send the value without leaving the page, you want to use AJAX. Do a search on jQuery on StackOverflow or on Google.
You would probably want to have your function return the username instead of echo it though. But if you absolutely want to echo it from the function, just call it like that in your HTML form.
I think you will need to understand the flow of the client-server process of your pages before going further. Let's say that the sample code above is called form.php.