Displaying a form from a nested function.

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
slipstream180
Forum Newbie
Posts: 2
Joined: Thu Jul 17, 2008 2:25 pm

Displaying a form from a nested function.

Post by slipstream180 »

I’m having trouble displaying a form from within a nested function – maybe somebody’s faced this before.

Here’s the setup:

I have an initial starting page – let’s call it A.php.

Pathname for A.php: Localhost/A.php

A.php contains a form that extracts input from the client then processes it by calling a function – b(). b() kicks off some processing which nests down to a function f(). Once at f(), we then want to go back to the client and ask for some more input, before branching:

Code: Select all

 
function f() {
Require_once ‘path_to_G/G.php’;
$result = prompt_user($options);
If ($result == ‘a’) {
// do A
} else {
// do B
}
} // end f()

Code: Select all

 
prompt_user() is in G.php:

Code: Select all

 
<?php
function prompt_user($options) {
?>
HTML form here
<?php
if (isset($_POST[‘submit’])) {
// Capture users’ selection
$response = $_POST[‘choice’];
return $response;
}
>?

Code: Select all

 
If I try to execute this code, prompt_user() simply returns a null value without waiting for a response from the client. The form is displayed (the URI shows ‘localhost/A.php’) but when the “Submit” button is pushed, the client browser loads ‘path_to_G/G.php’ and quits.

How do I get my form displayed, and capture what’s POSTed without prompt_user() returning prematurely?

I’ve also tried first redirecting to G.php before trying to throw up the form, but then the program stack is lost and I can’t get the $response back to the calling function. (f()) Any ideas?
WebbieDave
Forum Contributor
Posts: 213
Joined: Sun Jul 15, 2007 7:07 am

Re: Displaying a form from a nested function.

Post by WebbieDave »

slipstream180 wrote:prompt_user() simply returns a null value without waiting for a response from the client
You are programming for the web. There is no waiting for user input before continuing. A visitor requests a web page and you send it to them (which contains a form). Then, they click submit and it sends the values they've entered in the form to a PHP page that you program to inspect those values (those values are placed by PHP into the $_POST array). This PHP page then responds with perhaps some more html.

That's the web in a nutshell: a series stateless calls and responses.
slipstream180
Forum Newbie
Posts: 2
Joined: Thu Jul 17, 2008 2:25 pm

Re: Displaying a form from a nested function.

Post by slipstream180 »

Got it. So, is the approach here all wrong - there's no way to get a POSTed value back into my nested function, or can I re-code it in some way to get what I need from the client? I've got to believe there's some way of "getting there from here"...
Post Reply