Greetings everyone.

I have a form created by my own module that creates the form as I intended, but whether you enter a value or not it loops back to the first form page when I hit submit. I have experimented with _validate, _add_to_cart, and _submit. Nothing shows as executing. I have looked at tutorial after tutorial and have gone over a few videos as well. It's frustrating me to no end and I know it has to be something simple I am missing. I am obviously on a learning curve here, but already know my future of coding lies in Drupal. Help me out please!

Here's the code - Shows menu creation and the submit function. Help!

function farrowmedical_menu() {

$items['fmi/display'] = array(
'title' => t('Farrow Medical product display'),
'description' => 'Show the Product',
'page callback' => 'drupal_get_form',
'access arguments' => array('access_fmi'),
'page arguments' => array('farrowmedical_form'),
'type' => MENU_LOCAL_TASK,
);

}

function farrowmedical_form($form_state) {

$title = explode("/", $_GET['q']); // Title holds the name of the product we are looking at. (page name)
$result = db_query("SELECT * FROM `{fmi_products}` WHERE `{category}` = '{$title[2]}' order by weight asc");
while ($data = db_fetch_object($result)) { $arrey[$data->nid] = $data->model; }

if($arrey) {
$form['nid'] = array(
'#type' => 'select',
'#title' => t("Farrow Medical $title[2]"),
'#options' => $arrey,
'#description' => t("$title[2]"),
);

$form['qty'] = array(
'#type' => 'textfield',
'#title' => t('Quantity'),
'#size' => 5,
'#maxlength' => 15,
'#description' => t('Enter the quntity you wish to purchase.'),
);
$form['submit'] = array( '#type' => 'submit', '#value' => t('Submit'));
}

else {
$form['sku'] = array(
'#type' => 'select',
'#title' => t("Farrow Medical $title[2]"),
'#options' => $arrey,
'#description' => t("There is no product to display"), ); // Yes this is Lame. Improvement planned.
$form['submit'] = array('#type' => 'submit', '#value' => t('Return'));
}
return $form;
}

function farrowmedical_submit($form, &$form_state) {
set_drupal_message(t('If you are reading this *something* worked...'));
$ignorethis=farrowmedical_add_to_cart($nid, $qty);
return $form;
}

Thanks in Advance!

Comments

Anonymous’s picture

In Drupal 6, the default behavior for the forms system on submit is to return the user to the page that the form was displayed on.

Read this paragraph carefully, from the Forms API Quickstart.

To determine where the user should be sent after the form is processed, the _submit function can place a path or URL in $form_state['redirect'] which will be the target of a drupal_goto; every form is redirected after a submit. If you store nothing in $form_state['redirect'], the form will simply be redirected to itself after a submit. It is polite to use drupal_set_message() to explain to the user that the submission was successful.

You need to explicitly redirect the form using $form_state['redirect'] if you want to direct the user to a new page after the submit is finished. For example, you can redirect to page node/$nid in your example the following way:

function farrowmedical_submit($form, &$form_state) {
  set_drupal_message(t('If you are reading this *something* worked...'));
  $ignorethis=farrowmedical_add_to_cart($nid, $qty);
  $form_state['redirect'] = base_path() . 'node/' . $nid;
}

No need to have a return value from submit handler.

DLBaker’s picture

Hey thanks for the response.

I read that exact paragraph a few times, which is why I inserted the drupal_set_message() to verify if it was getting that far. I replaced my code with exactly what you provided. I get neither the drupal message nor get forwarded anywhere. Even when it was looping back to the form construct (Proper terminology?) it wouldn't display the contents of drupal_set_message(). as a note, when '#required' => TRUE, does work as intended and gives me the red text boxes when no value has been entered.

The code snippet you gave was copied religiously and I even tried hard coding the address to make sure there wasn't a bad address anywhere. No joy.

So So frustrating, but I appreciate input!

- Dan.

chrisshattuck’s picture

Hi guys,

What I typically do is use drupal_goto() to redirect in the submit function. It seems odd to me - even though the docs appear to state it - that a change to $form_state in the submit handler would do anything (I guess I've never tried). But, drupal_goto() would work fine as well.

Cheers,
Chris

Build a Module.com - The definitive video guide to Drupal development

Learn virtually any aspect of Drupal on BuildAModule, where I've recorded over 2200 video tutorials.

Anonymous’s picture

yep, Chris is right that drupal_goto will work just fine, though redirect does ends up calling drupal goto down the stack.

I just plugged this code into a submit handler that I use and it worked just fine:

function farrowmedical_submit($form, &$form_state) {
  drupal_set_message(t('If you are reading this *something* worked...'));
  // $ignorethis=farrowmedical_add_to_cart($nid, $qty);
  $nid = 1;
  $form_state['redirect'] = 'node/' . $nid;
}

note a couple of changes from your & my previous posts:
1) I believe you meant to say "drupal_set_message" instead of "set_drupal_message"
2) no need for base_path() in the redirect assignment, it assumes it is setting the redirect on base path

the code works just fine, outputs the message and redirects to node/1

Doug

DLBaker’s picture

I really appreciate the feedback so far.

It's still not working. One thing I noticed is that *no matter* what I do within the submit function, nothing displays from drupal_set_message. Not even when I had the function name incorrect. As a Test I placed drupal_set_message() at the top of my farrowmedical_form() and it displays Twice there. Labeling one 1 and the set_message in the _submit function as 2 I got two copies of 1 at the top of the page. mispelling the function name at this location gives me the white screen of death. but not from within _submit. Something is definitely sideways here.

It's as though that function is completely ignored. drupal_goto('/cart'); doesn't have any effect either. Could it be in my form function definition thats not calling submit?

Thanks for all the help again. it's starting to look like it wasn't just me going in circles. Any further help is greatly appreciated.

- Dan.

DLBaker’s picture

Well after thinking of testing the validate form I noticed something.

'test_form_validate'

Looking further down I saw:

'test_form_submit'

Looking at my code:

'farrowmedical_submit'

Changed this to:

'farrowmedical_form_submit'

Saved and reloaded and I am that much wiser, and feeling rather dense. I did get a lot of useful information in this exchange so the thread hasn't been a waste. Thank you gentlemen, so very much! :)

- Dan.

Jaypan’s picture

There is a reason for this. In the Drupal API, there exists hook_form() and hook_submit(). These are used for creating forms when you are defining a new node/content type with your module. Hook_form() is automatically called when creating a new node, and so no URL needs to be set in hook_menu(). And hook_submit() is automatically called after hook_form().

However, you created a URL with the callback to a function that was structured like hook_form(), but wasn't actually an implementation of hook_form(). When using a callback function for a URL, you need to append _submit to the function name - which you obviously figured out.

Anyways, I just thought I would give you some background on that so you would know why this happened the way it did.

DLBaker’s picture

I appreciate the input tremendously. And in hindsight, it makes perfect sense.

I plan on porting all my other projects over to drupal in the near future!

Peace,
- Dan.