Question:
Is it possible to submit a form to a function within a page? If so, how is it done?
Question:
Is it possible to submit a form to a function within a page? If so, how is it done?
Yes it's possible. If you don't understand how to do functions you might have a hard time though. However, if you do understand functions it really easy.
basically, when u submit the form, just send a hidden variable such as this:
<form method="post" action="<? echo($php_self); ?>">
<input type="hidden" name="mode" value="function">
then, when you submit the form, the page will refresh the variable "mode" will be set.
So, now, you just do an if statement like this:
if ($mode == "function") {
functionname();
}
else {
//run the rest of the page, or the form, or whatever
}
Thanks guys, but what I had in mind was passing the form to another file containing the function to be processed. Does it work the same?
Yes; submit the form to the page as ever, then in that page call the function
<form action="page.php" method="GET">
page.php:-----------------
function process()
{.....something involving $_GET[]
}
Thanks Weedpacket. My concern though is this:
I have several forms to build. What I want to happen is that it submits itself to only one processing page that is divided to different functions.
As such:
form1:---
$var1;
$var2;
form2:---
$var4;
$var5;
processform.php:---
function form1()
{
}
function form2()
{
form 1 submits to processform.php, but only to use function form1();.
form 2 submits to processform.php, but only to use function form2();.
Just have a hidden field in both forms, telling which function it should run:
<input type="hidden" name="formNum" value="one" />
and then in your PHP script:
if (isset($POST['formNum']) && $POST['formNum'] == "one") {
form1();
}
Diego
As an alternative to diego25's suggestion, give each Submit button a different name (one that describes its form's function, perhaps), and then see if $_POST['that name'] isset. No big difference - just avoids having an extra form field, and since the submit button's value (default "submit") is being passed anyway...
Thanks guys. I'll try it out... I'll get back to you if something comes up...