Form with two submits - a newbie scope question
if (isset ($_POST['somethingA']))
{
//code for doing something A
}
elseif (isset ($_POST['somethingB']))
{
//code for doing something B
}
I will need to access some data from somethingA code, into somethingB code.
开发者_运维问答How can I do that in a proper way?
Should I declare a variable outside the conditionals, work inside the conditionals, and later (bottom) I use that?
Should I work with them inside the conditionals, and, somehow, pull them out after the conditional lines?
Thanks in advance, MEM
you may do something like you said:
$innervar = null;
if (isset ($_POST['somethingA']))
{
$innervar = new A();
//code for doing something A
}
elseif (isset ($_POST['somethingB']))
{
$innervar = new B();
//code for doing something B
}
$innervar->CommonMethod();
If you are accesing $_POST directly consider using some sort of framework like symfony, or Zend, unless you are learning or working in a very simple(house) project.
Best of luck David
You should just declare the variable outside. So you can use and access the resources in both
According to your clarification the comments, you want to share data between two successive executions of the PHP script. That's not a question of where to put your variables in the ocde. You want the code to run once, compute some value, and then have this value available when the code is run again.
There are two ways of doing this:
- Put the value into a hidden input field on the webpage
- Put the value into the user session (which means it's stored "somewhere" by the PHP runtime and made available for all subsequent requests with the same session ID)
精彩评论