A hypothetical example:

user comes to http://www.mywebsite/result first time and he sees a form there, he clicks Yes or No whatever, after the form submit he comes back to the same URL. Now if he clicked Yes earlier, show the form again else do not show the form.

Hook Menu

function search_result_menu() {
   $items = array();  
  $items['result'] = array(
        'title' => 'View Summary Result',
        'description' => 'View Result',
        'page callback' => 'search_result_display',
	'access arguments' => array('View Result'),
        'type' => MENU_CALLBACK,
    );

    return $items;
}

This displays the page content that creates a form

function search_result_display() {
...
//create the form
if($show_form_again) {
       drupal_get_form('confirmation_form');
}
..
}

Create a form with just Yes No buttons

function confirmation_form() {
    $form['fieldset']['cancel'] = array(
    '#type' => 'submit',
    '#value' => t('Yes'),
    );
	

  $form['fieldset']['submit'] = array(
        '#type' => 'submit',
        '#value' => t('No')
   );
    return $form;
}

Check what was clicked by the user and set a variable's value;

function confirmation_form_submit($form, &$form_state) {	
	if($form_state['values']['op'] == t('Yes')) {
             $show_form_again=true;
	} elseif($form_state['values']['op'] == t('No')) {
            $show_form_again=false;
	}
}

I don't know if I am sounding stupid but my requirement is complex so instead of writing so much I've given this hypothetical example to understand this implementation.

Thanks and Regards

Comments

jaypan’s picture

Now if he clicked Yes earlier

How much earlier? Any time in the past? Or you mean while on the same page?

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

I am learning’s picture

while on the same page, should I use session variables? or Drupal has another way to handle such things?

jaypan’s picture

Yep:

//form definition:

function my_form($form_state)
{
  $form['yes'] = array
  (
    '#type' => 'submit',
    '#value' => t('Yes'),
  );
  $form['no'] = array
  (
   '#type' => 'submit',
    '#value' => t('No'),
  );
  return $form;
}
function my_form_submit($form, &$form_state)
{
  if($form_state['values']['op'] == t('Yes'))
  {
    $form_state['storage']['ok'] = TRUE;
  }
  else
  {
    unset($form_state['storage']);
  }
}

if $form_state['storage'] is set, the form will automatically be rebuilt. The value I saved to it was entirely arbitrary, just to show an example.

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

I am learning’s picture

Thanks Jay, unfortunately in both the cases the form is appearing, I simply added

$form_state['storage']['ok'] = TRUE;
//And
unset($form_state['storage']);

in if else respectively.

jaypan’s picture

Sorry, I wasn't really thinking. Your original form definition should be:

function my_form($form_state)
{
  if(isset($form_state['storage']) && $form_state['storage']['ok'])
  {
    $form['yes'] = array
    (
      '#type' => 'submit',
      '#value' => t('Yes'),
    );
    $form['no'] = array
    (
      '#type' => 'submit',
      '#value' => t('No'),
    );
  }
  else
  {
    $form['already_submitted'] = array
    (
      '#value' => t('You already clicked yes'),
    );
  }
  return $form;
}

Your submit function should be as is.

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

I am learning’s picture

Thank you so much.
Regards