Calling a php function from an html form action

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
glennnz
Forum Newbie
Posts: 9
Joined: Thu Aug 14, 2008 7:16 am

Calling a php function from an html form action

Post by glennnz »

I am trying to get this to work:

<?php
function afunction()
{
some php code that generates an unlock code;
}

echo "<form name='input' action='afunction()' method='post'>";
{
echo "Enter serial number:";
echo "<input type='text' name='serial'>";
echo "<input type='submit' value='Generate Unlock Code'>";
echo "<br/>";
}
echo "</form>";
?>

but I get a 404 error "Can't find page afunction()"

How do I execute afunction() from the form action??

G
User avatar
idevlin
Forum Commoner
Posts: 78
Joined: Tue Jun 26, 2007 1:10 pm
Location: Cambridge, UK

Re: Calling a php function from an html form action

Post by idevlin »

You can't execute the PHP function directly like that, a form action field requires a page to redirect to.

Easiest way would be to create a PHP page that contains the afunction() (e.g. afunction.php) and pass the variables to it. Then call afunction() within the new PHP page.

Alternatively, create afunction() in Javascript rather than PHP and run it when the submit button is clicked. This may not be the best option though, as you probably want to hide the code that generate the unlock code.
Post Reply