slipstream180 Posted July 20, 2008 Share Posted July 20, 2008 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: function f() { Require_once ‘path_to_G/G.php’; $result = prompt_user($options); If ($result == ‘a’) { // do A } else { // do B } } // end f() prompt_user() is in G.php: <?php function prompt_user($options) { ?> HTML form here <?php if (isset($_POST[‘submit’])) { // Capture users’ selection $response = $_POST[‘choice’]; return $response; } >? 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? Quote Link to comment Share on other sites More sharing options...
joquius Posted July 20, 2008 Share Posted July 20, 2008 First of all you need to make sure G.php is included for displaying the form and getting the post values. The way you wrote this is somewhat reminiscent of a javascript pop up. You do understand the differences right? Anyway paste the form code in so we can see why the forms going to G.php. Quote Link to comment Share on other sites More sharing options...
DarkWater Posted July 21, 2008 Share Posted July 21, 2008 It's not going to wait for the client. Simply put. You may be completely misunderstanding what PHP is. Quote Link to comment Share on other sites More sharing options...
slipstream180 Posted July 21, 2008 Author Share Posted July 21, 2008 Here's all of prompt_user(), including HTML code: function prompt_user($message) { if ($_POST['submit'] == null) { ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html> <head> <title>Prompt User</title> </head> <body> <form id="message_form" action="path_to_G/G.php" method="post" name="msg_form" > <div> <table border="0"> <tr> <td align="right" valign="top"><b>Choose an option</b></td> <td valign="top" align="left"> <select name="election" id="user_select" > <?php if (isset($message['options'])) { foreach ($message['options'] as $option => $label) echo "<option value=\"$option\">$label</option>\n"; } // end if ?> </select></td> </tr> <tr> <td align="right" valign="top"><b></b></td> <td valign="top" align="left"> <input name="submit" value="Submit" type="submit" /></td> </tr> </table> </div> </form> </body> </html> <?php } else { echo "election: " . $_POST['election'] . "<br />"; return $_POST['election']; } } // end prompt_user So, I am POSTing to G.php. However, if I POST to A.php, it calls a function a(), which does the same kind of thing prompt_user() does. (extract POSTed values and pass them on to another function). As a result, trying to return $_POST['election'] to A.php results in a functional error. Quote Link to comment Share on other sites More sharing options...
joquius Posted July 21, 2008 Share Posted July 21, 2008 G.php is just a file with a function, and if there is nothing to run that function in G.php you'll just get a blank page. Your post should be leading the user to your main script which should be running the G.php function when (1) you want to display the form and (2) when you want to process the form data from a post. You need to differentiate between display the form and posting. For example: <?php require_once ("filewithformfunctions.php"); if (!isset ($_POST['mysubmitbutton')) display_form(); else $res = process_form(); if (isset ($res)) do_stuff_with_res($res); ?> Just an example. Apart from this, if you have a form which needs different functions for displaying/processing information it's sometimes better to have it as an object. Quote Link to comment Share on other sites More sharing options...
slipstream180 Posted July 22, 2008 Author Share Posted July 22, 2008 OK. I think I understand that code snippet and that displaying a form and grabbing the resulting POSTs are two different things. However, does that mean there's really no way to return the POSTed values back to my calling function? (If I try something like your code snippet there, the function executes the code before the POSTed variable is captured and a NULL is returned to the calling function) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.