submitting and processing a form!
Hello, I thought I grocked the new form API, but I have just been pulling my hair trying to do something that I thought would be fairly easy. I guess I still deserve my handle 'beginner' :/ . In the code below, I create a hook_menu() callback to the function formtest() with a form. I want to be able to print some information according to the form that has just been submitted (here, a simple text string), then reprint the form below that. I know that $_POST['op'] is set when I submit (and the process die()s when I uncomment that line), but I can't get the information submitted to be displayed above the form. I have tried using the function formtest_submit() but returning a value there doesn't get the expected result (it seems it's a path callback). I have searched the extensive documentation, but all there is is how to build a form, not how to process and display the information. Am I, for the third time this week, missing something obvious and simple? Augustin. <?php function test_menu() { $items[]= array( 'path' =>'test/formtest', 'title'=>'test form api', 'access'=>user_access('administer test'), 'callback'=>'formtest', 'type'=>MENU_NORMAL_ITEM ); return $items; } function formtest() { $out = 'beginning of output<br />'; if (isset($_POST['op'])) { $out .= 'text entered: ' . $_POST['text']. '<br />'; // the line above doesn't display, no matter what! //die(); } $form = array(); $form['text'] = array( '#type' => 'textfield', '#title' => t('Enter some text'), ); $form['submit'] = array('#type' => 'submit', '#value' => t('Enter')); $out .= drupal_get_form('formtest', $form); return $out; } ?> -- http://www.wechange.org/ Because we and the world need to change. http://www.reuniting.info/ Intimate Relationships, peace and harmony in the couple.
Try looking in $_POST['edit'], that seems to be where things get put (but look at the source of the page that shows the form, it'll be clear there I think). On 7/4/06, Augustin (Beginner) <drupal.beginner@wechange.org> wrote:
I know that $_POST['op'] is set when I submit (and the process die()s when I uncomment that line), but I can't get the information submitted to be displayed above the form.
-- Alan Dixon, Web Developer http://alan.g.dixon.googlepages.com/
Augustin (Beginner) wrote:
I thought I grocked the new form API, but I have just been pulling my hair trying to do something that I thought would be fairly easy. I guess I still deserve my handle 'beginner' :/ .
In the code below, I create a hook_menu() callback to the function formtest() with a form. I want to be able to print some information according to the form that has just been submitted (here, a simple text string), then reprint the form below that. I know that $_POST['op'] is set when I submit (and the process die()s when I uncomment that line), but I can't get the information submitted to be displayed above the form.
I have tried using the function formtest_submit() but returning a value there doesn't get the expected result (it seems it's a path callback).
That is exactly right. I always use this to send the user's browser to a path with all of the search terms in the URL. For example, search.module sends people to .../type/search+query. The following request will be a GET. Then a page can be served in the usual way, not needing to worry about $_POST. arg(n), hook_menu()'s callback arguments, and/or automatically passed callback arguments may be used. And the resulting page is bookmark-able. (or you can return either NULL or FALSE, I forget which, from formtest_submit() and turn off redirection and use $_POST wherever you need it) -- Neil Drumm http://delocalizedham.com/
That is exactly right. I always use this to send the user's browser to a path with all of the search terms in the URL. For example, search.module sends people to .../type/search+query. The following request will be a GET.
Then a page can be served in the usual way, not needing to worry about $_POST. arg(n), hook_menu()'s callback arguments, and/or automatically passed callback arguments may be used.
And the resulting page is bookmark-able.
(or you can return either NULL or FALSE, I forget which, from formtest_submit() and turn off redirection and use $_POST wherever you need it)
NULL, so that $redirect will fail the isset test. Here is the code where it's tested (form.inc line 201): // Since we can only redirect to one page, only the last redirect will work $redirect = call_user_func_array($function, $args); if (isset($redirect)) { $goto = $redirect; }
On 7/4/06, Augustin (Beginner) <drupal.beginner@wechange.org> wrote:
In the code below, I create a hook_menu() callback to the function formtest() with a form. I want to be able to print some information according to the form that has just been submitted (here, a simple text string), then reprint the form below that. I know that $_POST['op'] is set when I submit (and the process die()s when I uncomment that line), but I can't get the information submitted to be displayed above the form.
I have tried using the function formtest_submit() but returning a value there doesn't get the expected result (it seems it's a path callback).
That's correct. I have searched the extensive documentation, but all there is is how to
build a form, not how to process and display the information.
Am I, for the third time this week, missing something obvious and simple?
You need to save the data somewhere. There are a number of possibiilites: - save it in $_SESSION, - redirect to a path with the text attached to the url), - save it in a variable or other db table, - disable redirecting. (there may be more options) BTW: your data is in $_POST['edit']['text'] not $_POST['text'] So your example code becomes, if you disable redirects (last option): <?php function test_menu($may_cache = FALSE) { $items = array(); $items[] = array( 'path' => 'test/formtest', 'title' => t('Test form api'), 'access' => TRUE, 'callback' => 'test_formtest', 'type' => MENU_NORMAL_ITEM, ); return $items; } function test_formtest() { $form = array(); if (!empty($_POST['edit']['text'])) { $form['entered-text'] = array( '#type' => 'item', '#title' => t('Entered text'), '#value' => $_POST['edit']['text'], ); } $form['text'] = array( '#type' => 'textfield', '#title' => t('Enter some text'), ); $form['submit'] = array( '#type' => 'submit', '#value' => t('Enter'), ); return drupal_get_form('test_formtest', $form); } function test_formtest_submit($form_id, $form_values) { return FALSE; } ?> Depending on your case, you may need to test inside _submit() which button is pressed (using $_POST['op']) and redirect to another page depending on the button (eg "Preview" or "Submit"). You may prefer to save the data in $_SESSION if you want the data to be available again when they go to the form again, the _submit() then becomes (with some small changes to the form obviosuly): <?php function test_formtest_submit($form_id, $form_values) { $_SESSION['test_formtest'] = $form_values; } ?> Hope this helps, Robrecht
participants (5)
-
Alan Dixon -
Augustin (Beginner) -
John VanDyk -
Neil Drumm -
Robrecht Jacques