iam new in drupal world and i tried to make a form that has 3 buttons
1- reset --> clean all feilds in form (work well)
2- submit--> insert data from my form in db (work well too)
3- show pending request --> retrive the data from table (here is the problem!!!!!!!!!)

here is what i made

<?php
function request_menu() {
  $items['request/form'] = array(
    'title' => t('ADD New request'),
    'page callback' => 'request_form',
    'access arguments' => array('access content'),
    'description' => t('ADD New request'),
    'type' => MENU_CALLBACK,
  );
  return $items;
}

function request_form() {
  return drupal_get_form('request_my_form');
}

function request_my_form($form_state) {

  $form['requestinfo'] = array(
    '#type' => 'fieldset',
    '#title' => t('Request Info'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  );
  $form['requestinfo']['first'] = array(
    '#type' => 'textfield',
    '#title' => t('Name'),
    '#default_value' => $GLOBALS[user]->name, // changed
    '#description' => "Please enter your name.",
    '#size' => 20,
    '#maxlength' => 20,
  );
	  
	$form['requestinfo']['vdate'] = array(
    '#type' => 'textfield',
    '#title' => t('Vacation Date'),
    '#default_value' => $form_state['values']['vdate'], // changed
    '#description' => "Please enter Date.",
    '#size' => 20,
    '#maxlength' => 20,
  );
  
  $form['requestinfo']['vprice'] = array(
    '#type' => 'textfield',
    '#title' => t('Vacation Price'),
    '#default_value' => $form_state['values']['vprice'], // changed
    '#description' => "Please enter Price.",
    '#size' => 20,
    '#maxlength' => 20,
  );
  
  $form['clear'] = array(
    '#type' => 'submit',
    '#value' => 'Reset form',
    '#validate' => array('my_module_my_form_clear'),
  );
   $form['showp'] = array(
    '#type' => 'submit',
    '#value' => ' Show Pending Requests',
  );
 
  $form['next'] = array(
    '#type' => 'submit',
    '#value' => 'submit',
  );
  return $form;
}



function request_my_form_new_name($form, &$form_state) {
  $form_state['storage']['new_name'] = TRUE;
  $form_state['rebuild'] = TRUE; // Will cause the default submit function
                                 // to be skipped.
}

function request_my_form_clear($form, &$form_state) {
  unset ($form_state['values']);  // ensures fields are blank after reset
                                  // button is clicked
  unset ($form_state['storage']); // ensures the reset button removes the
                                  // new_name part

  $form_state['rebuild'] = TRUE;
}

function request_my_form_submit($form, &$form_state) {
  // Handle page 1 submissions
  if ($form_state['clicked_button']['#id'] == 'edit-next') 
  {
  $form_state['storage']['page_one_values'] = $form_state['values'];
    db_query("INSERT INTO {requests} (name, v_date, v_price) VALUES ('%s','%s','%s' )", 
	$form_state['storage']['page_one_values']['first'],
	$form_state['storage']['page_one_values']['vdate'],
	$form_state['storage']['page_one_values']['vprice']); //page 1
	
	
	drupal_set_message(t('Your form has been saved.'));
	}
	
	else if ($form_state['clicked_button']['#id'] == 'edit-showp') 
	  {
		$obj = db_fetch_object( db_query(
			"SELECT v_date FROM {requests}" ) );
		$form['requests'] = array(
			'#type' => 'textfield',
			'#title' => t('Pending Requests'),
			'#default_value' => $obj->v_date,
			'#size' => 20,
			'#maxlength' => 255
		);
		return $form;
		//$form_state['rebuild'] = TRUE;
	  
		}
    
  
 }

?>

Comments

ericduran’s picture

Since your only getting one field you should use db_result instead.

     $obj = db_fetch_object( db_query(
            "SELECT v_date FROM {requests}" ) );
        $form['requests'] = array(
            '#type' => 'textfield',
            '#title' => t('Pending Requests'),
            '#default_value' => $obj->v_date,
            '#size' => 20,
            '#maxlength' => 255
        );

should be

      $v_date = db_result( db_query(
            "SELECT v_date FROM {requests}" ) );
        $form['requests'] = array(
            '#type' => 'textfield',
            '#title' => t('Pending Requests'),
            '#default_value' => $v_date,
            '#size' => 20,
            '#maxlength' => 255
        );

Eric

amira’s picture

thanks ericduran for ur replay,

i do what u suggest but i didnot find any thing change.the problem still in the show pending request button.

here is what i made.

<?php
function request_menu() {
  $items['request/form'] = array(
    'title' => t('ADD New request'),
    'page callback' => 'request_form',
    'access arguments' => array('access content'),
    'description' => t('ADD New request'),
    'type' => MENU_CALLBACK,
  );
  return $items;
}

function request_form() {
  return drupal_get_form('request_my_form');
}

function request_my_form($form_state) {

  $form['requestinfo'] = array(
    '#type' => 'fieldset',
    '#title' => t('Request Info'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  );
  $form['requestinfo']['first'] = array(
    '#type' => 'textfield',
    '#title' => t('Name'),
    '#default_value' => $GLOBALS[user]->name, // changed
    '#description' => "Please enter your name.",
    '#size' => 20,
    '#maxlength' => 20,
  );
     
    $form['requestinfo']['vdate'] = array(
    '#type' => 'textfield',
    '#title' => t('Vacation Date'),
    '#default_value' => $form_state['values']['vdate'], // changed
    '#description' => "Please enter Date.",
    '#size' => 20,
    '#maxlength' => 20,
  );
 
  $form['requestinfo']['vprice'] = array(
    '#type' => 'textfield',
    '#title' => t('Vacation Price'),
    '#default_value' => $form_state['values']['vprice'], // changed
    '#description' => "Please enter Price.",
    '#size' => 20,
    '#maxlength' => 20,
  );
 
  $form['clear'] = array(
    '#type' => 'submit',
    '#value' => 'Reset form',
    '#validate' => array('my_module_my_form_clear'),
  );
   $form['showp'] = array(
    '#type' => 'submit',
    '#value' => 'Show Pending Requests',
  );

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



function request_my_form_new_name($form, &$form_state) {
  $form_state['storage']['new_name'] = TRUE;
  $form_state['rebuild'] = TRUE; // Will cause the default submit function
                                 // to be skipped.
}

function request_my_form_clear($form, &$form_state) {
  unset ($form_state['values']);  // ensures fields are blank after reset
                                  // button is clicked
  unset ($form_state['storage']); // ensures the reset button removes the
                                  // new_name part

  $form_state['rebuild'] = TRUE;
}

function request_my_form_submit($form, &$form_state) {
  // Handle page 1 submissions
  if ($form_state['clicked_button']['#id'] == 'edit-next')
  {
  $form_state['storage']['page_one_values'] = $form_state['values'];
    db_query("INSERT INTO {requests} (name, v_date, v_price) VALUES ('%s','%s','%s' )",
    $form_state['storage']['page_one_values']['first'],
    $form_state['storage']['page_one_values']['vdate'],
    $form_state['storage']['page_one_values']['vprice']); //page 1
   
   
    drupal_set_message(t('Your form has been saved.'));
    }
   
    else if ($form_state['clicked_button']['#value'] == 'Show Pending Requests')
      {
        $v_date = db_result( db_query(
            "SELECT v_date FROM {requests}" ) );
        $form['requests'] = array(
            '#type' => 'textfield',
            '#title' => t('Pending Requests'),
            '#default_value' => $v_date,
            '#size' => 20,
            '#maxlength' => 255
        );
        
        return $form;
        //$form_state['rebuild'] = TRUE;
     
        }
   
 
}
?>

ericduran’s picture

What exactly do you want to do when Show pending request is press?

Right now there's not really any logic for that button. Do you want to list all the request?

If so you should just add another menu item that does all the logic.

Here's an example:


function request_menu() {
  $items['request/form'] = array(
    'title' => t('ADD New request'),
    'page callback' => 'request_form',
    'access arguments' => array('access content'),
    'description' => t('ADD New request'),
    'type' => MENU_CALLBACK,
  );
   $items['request/pending'] = array(   //add the new menu item
    'title' => t('Show Pending Request'),
    'page callback' => 'request_form_2', //call new form function
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
  );
  return $items;
}

//new form function 
function request_form_2() {
  return drupal_get_form('show_pending_request');
}

//does all the logic for the page.
function show_pending_request(){
 $sql = db_query("SELECT v_date FROM {requests}");
 
 while ($obj = db_fetch_object($sql)){
        $form['requests']['test'] = array(
            '#type' => 'textfield',
            '#title' => t('Pending Requests'),
            '#default_value' => $obj->v_date,
            '#size' => 20,
            '#maxlength' => 255
        );
        }
        return $form;

}

Eric

amira’s picture

thanks ericduran for help,
but when i typed in url http://localhost/drupal/?q=request/pending i found page not found...
and i need when the view button is pressed i redirect to a form that u call pending that list all the pending requests .


else if ($form_state['clicked_button']['#value'] == 'view') 

{

//redirect to a form where i can list all pending request

}
ericduran’s picture

you need to make sure your menu cache is clear.

You can do this by going to the module page 'admin/build/modules/list'

Regarding the redirect

else if ($form_state['clicked_button']['#value'] == 'view')
{
$form['#redirect'] = 'request/pending';
}

Eric

amira’s picture

ericduran thank u very much for helping me,
i did what u recommended but no change happened . i run update.php to update my module and no change happened too ....i don't know why...?

amira’s picture

hayyyyy.....i found it worked suddenly but when i write request/pending in url...but the button "view" button didn't work ...so the problem in form redirection .....another problem i found in the new form only the last record in the table ....although we put it in while loop....so where is the problem u guess...?

thanks in advance