Hi all,

I have a form which passes some value to the submit function based on a parameter. This submit function then links the user to some page. My code looks like this.

/*
 * form to create a button which links to view page
 */
function scribblar_button_form(&$form_state, $appointment){

    $form['appointment'] = array(
        '#type' => ' hidden',
        '#value' => $appointment
    );

    $begin = db_fetch_array(db_query("SELECT begin FROM {appointment} WHERE id ='%s'", array($appointment)));
    if(strtotime($begin['begin']) <= (time()+300)){
        $form['submit'] = array(
          '#type' => 'submit',
          '#value' => t('Ga naar de les'));
    }else{
        $form['submit'] = array(
          '#type' => 'submit',
          '#disabled' => TRUE,
          '#value' => t('Ga naar de les'));
    }
    
    return $form;
} 

function scribblar_button_form_submit($form, &$form_state){
    drupal_goto('/scribblar/view/appointment/'.$form_state['values']['appointment']);
}

However, for some reason I can't seem to put my finger on, $form_state['values']['appointment'] doesn't seem to exist. I first thought it had sth to do with me referincing $form_state etc. in the wrong way. So I messed around with the function parameters a bit but this didn't solve it either. I use pretty much the same code (put param in hidden field then use in submit function) in another module and this works just fine.

Any help would be greatly appreciated. Thanks in advance,
Bouke

Comments

adaddinsane’s picture

Well, I would have thought it would work as well.

However you don't need to use a field at all do this:

$form['#appointment'] = $appointment;

And then

drupal_goto('/scribblar/view/appointment/'.$form['#appointment']);

Hm. I doubt you should have the leading '/' on the URL anyway - bad form.

When you say "doesn't seem to exist" what do you mean? Do you not know? Have you done dpm($form_state); - assuming you have the Devel module enabled.

Are you sure that the value is going into the form in the first place?

Steve Turnbull, D8 developer

Like Science Fiction, Fantasy or Steampunk stories? Check out https://taupress.com/book-list

Bouke’s picture

Hmm, it seems the hidden field isn't created. $appointment is definitely passed to the form function. The hidden field just isn't created.
Unfortunately $form['#appointment'] = $appointment; doesn't work either... Any clues as to why this is happening (or better said isn't happening)

adaddinsane’s picture

When you say "doesn't work" please describe what is and what is not happening.

Steve Turnbull, D8 developer

Like Science Fiction, Fantasy or Steampunk stories? Check out https://taupress.com/book-list

nevets’s picture

At the start of scribblar_button_form() you could add

drupal_set_message("scribblar_button_form(..., $appointment)");

to make sure $appointment is set.

Personally, I would not use a form for this but a link that is conditionally output.

Also, I would use type 'value' instead of 'hidden'.

jaypan’s picture

As nevets said, you should use #type 'value'.

Contact me to contract me for D7 -> D10/11 migrations.

adaddinsane’s picture

As I said - you don't have to use either. I'm not really sure "should" comes into it.

But the problem is not the choice of data storage - it's the fact nothing appears to be working which means either the data is not coming into the routine, or the submit function is not getting called.

Try adding:

$form['#submit'][] = 'scribblar_button_form_submit';

Just to ensure the submit function gets called.

Steve Turnbull, D8 developer

Like Science Fiction, Fantasy or Steampunk stories? Check out https://taupress.com/book-list

jaypan’s picture

You can hack it other ways like you've suggested, but the proper way is using #type = 'value'. This #type is made for passing values, which is exactly what he wants to do.

So I would say he should be using #type = 'value'.

Contact me to contract me for D7 -> D10/11 migrations.

adaddinsane’s picture

Well we could disagree until the cows come home - '#type' is for rendering not storing a value. Drupal renders what's in the array, the fact that it generates a form is of no relevance to the renderer. All the form related stuff is handled separately to the rendering (which I know you know) and 'value' does not reveal the value to the outside, which is fine and good, but since you can put the value into the form structure anyway, it's redundant and involves additional, unnecessary, processing.

There's no should in this instance, IMO.

If you want to have the last word, feel free, I won't comment on this again.

Steve Turnbull, D8 developer

Like Science Fiction, Fantasy or Steampunk stories? Check out https://taupress.com/book-list

jaypan’s picture

You could also pass data in $_SESSION variables, the $_GET variables, cookies and probably 4-5 other ways. They will all work, but that's beside the point. #type of 'value' was made for passing values. That's its whole purpose for existing. Sure you can pass the data other ways, but they weren't made for that, and therefore results could be unpredictable. Whereas with #type 'value', you can count on what will happen, since it's being used for what it was made to be used for.

Contact me to contract me for D7 -> D10/11 migrations.

dennishu’s picture

Well, according to the form API reference:

Note that as of Drupal 6, you can also simply store arbitrary variables in $form['#foo'] instead, as long as '#foo' does not conflict with any other internal property of the Form API.

http://api.drupal.org/api/drupal/developer--topics--forms_api_reference....

adaddinsane’s picture

For completeness: in Drupal 7 you can now store anything you like in $form_state (no need to use $form['storage'] either).

@dennishu: I am vindicated :-)

Steve Turnbull, D8 developer

Like Science Fiction, Fantasy or Steampunk stories? Check out https://taupress.com/book-list

goofus’s picture

Hi,
You mapped a url to your request right? Drupal refers to the request mapper as a "menu".

  $items['my_module/my_path/%'] = array(
    'load arguments'=>array(3),
    'page callback' => 'drupal_get_form',
    'page arguments'=>array('scribblar_button_form',2),
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
   );

So in the example above you would request http://MYSITE/my_module/my_path/my_appointement_string.

The key above is the use of load arguments and page arguments.

So a couple questions. Have you checked the html source that Drupal is generating is there a input field (hidden) for appointment. What is an actual appointment value?

adaddinsane’s picture

He said in the reply to my first posting there's no field. So there's something fundamentally screwy going on which has nothing to do with the apparent problem.

Steve Turnbull, D8 developer

Like Science Fiction, Fantasy or Steampunk stories? Check out https://taupress.com/book-list

goofus’s picture

That's why we need to establish if a "value" for appointment is sent and received. The "field" won't be created if you are passing NULL to the form constructor.

The request var must be mapped and passed to the form constructor. Thus the question about menu callbacks. How (if at all) is the appointment value being routed to the form constructor? My guess is, it's not.

Next, a quick reality check. What is a sample value for "appointment". Why assume it's a valid value.

adaddinsane’s picture

Agreed. But we seem to be talking among ourselves.

Steve Turnbull, D8 developer

Like Science Fiction, Fantasy or Steampunk stories? Check out https://taupress.com/book-list

Bouke’s picture

Sorry for the late reply, I've been kinda tied up over here.

As adaddinsane said the problem is not in actually handling the form data. The hidden/value, whatever is best, field just isn't outputted at all. (I've changed it to value btw) There's not the slightest trace in the html.

The appointment however is correctly passed to the form function. So a NULL value from the constructor isn't the case either. A sample value for appointment would really be any numer. Like 5 or 10 or whatever.

I've been reading through all the post and tried most of the offered solutions but unfortunately neither seem to be working. However as I was writing this post I tried adding a test (text) field to see if fields were actually created at all. This was the case so I tried changing the value of the value field to some random string and miraculously this worked as well. I then changed it back to the variable and it seems that even though my code hasn't changed the problem is solved.

I've got no idea what happened but I'm glad it's working. Thank you all for your help very much.

adaddinsane’s picture

Form elements of type "value" don't generate HTML- their value gets added to the form values when the form is submitted so it looks like they were in the form.

It could have been a very subtle typo (e.g. variable misspelled so you think you're putting the value in place, but you're not - then you entered it correctly the second time).

Steve Turnbull, D8 developer

Like Science Fiction, Fantasy or Steampunk stories? Check out https://taupress.com/book-list