hi all,
i have a problem while developing multi-form..

1) i have a form that list all requests that stored in db.
2) beside each record retrieved i show 2 other links (edit and delete)
3) and add button for adding new request.
3) when user clicked to edit link ,i redirect to a form that retrieve all data from db in the corresponding textboxes then i allow user to save changes....all i need now that i need to get the new values to update record ...how could i do this acording to my code..????

here iam asking ...how to handel the clicking on this button (save the changes) to update my recorde in db.

here is the code that i made in the module


<?php

  // $Id$
  /**
  * @file
  * The theme system, which controls the output of Drupal.
  *
  * The theme system allows for nearly all output of the Drupal system to be
  * customized by user themes.
  */
  function request_menu() {
   $items['admin/settings/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_NORMAL_ITEM,
  );
  
  
  $items['request/ADD'] = array(
    'title' => t('ADD New request'),
    'page callback' => 'request_form',
    'access arguments' => array('access administration pages'),
    'description' => t('to add new request click here'),
    'type' => MENU_NORMAL_ITEM,
  );
  
  $items['request/edit'] = array(
    'title' => t('Edit ur request'),
    'page callback' => 'request_form_5',
    'access arguments' => array('access administration pages'),
    'description' => t('to Edit ur contents'),
    'type' => MENU_NORMAL_ITEM,
  );
  
 
   $items['request/price'] = array(   //add the new menu item
    'title' => t('Show prices'),
    'page callback' => 'request_form_3', //call new form function
    'access arguments' => array('access content'),
    'type' => MENU_NORMAL_ITEM,
  );
  
  $items['request/delete'] = array(   //add the new menu item
    'title' => t('Delete Request'),
    'page callback' => 'request_form_4', //call new form function
    'access arguments' => array('access content'),
    'type' => MENU_NORMAL_ITEM,
  );

  $items['request/insert'] = array(   //add the new menu item
    'title' => t('update Request'),
    'page callback' => 'request_form_6', //call new form function
    'access arguments' => array('access content'),
    'type' => MENU_NORMAL_ITEM,
  );

  return $items;
  }
  function  request_form() {
  return drupal_get_form('request_my_form');
  }
  function request_form_2() {
  return drupal_get_form('show_pending_request');
  }   
   function request_form_3() {
  return drupal_get_form('show_price');
  }   
  function request_form_4() {
  return drupal_get_form('delete_request');
  }   
  function  request_form_5() {
   
	  return drupal_get_form('edit_form');
	  
  }
   function  request_form_6() {
   
	  return drupal_get_form('insert_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['next'] = array(
    '#type' => 'submit',
    '#value' => 'Save >>',
  );
  return $form;
  }
  function show_pending_request() {
  
  $sql = db_query("SELECT v_date ,id FROM {requests} where name='".$GLOBALS[user]->name."'");  
  
  while ($obj = db_fetch_object($sql))
  {
 
    $form['request'][]=array(   
  
    '#value' => l($obj->v_date,'http://localhost/drupal?q=request/price&'.'vid/'.$obj->id),
  );
  //$img_path = ("/sites/all/modules/request/edit.png");
 //$img = theme_image("/sites/all/modules/request/edit.png");//here i tried to show the link as icon but it doesn't appear.
 //$form['request'][]=array(     
  '#value' => $img,
	);
	
	$form['request'][]=array(     
  '#value' => l("Edit",'http://localhost/drupal?q=request/edit&'.'vid/'.$obj->id, array('html' => TRUE)),
	);
	
	$form['request'][]=array(     
  '#value' => l("	Delete		", 'http://localhost/drupal?q=request/delete&'.'vid/'.$obj->id),
	
	);
	
	$form['request'][]=array(     
  '#value' =>"<br>",
	);	
  }
  	$form['request'][]=array(     
  '#value' =>"<br>",
	);	
		$form['request'][]=array(     
  '#value' =>"<br>",
	);	
  $form['request'][]=array(     
  '#value' => l(ADD,'http://localhost/drupal?q=request/ADD'),
  '#type' =>'item',
	
	);
  return $form;
  }
// form that show the description of the record depend on its id that i appended on the query string(vid)
  function show_price() {
  
   $cgi = $_SERVER['REQUEST_METHOD'] == 'GET' ? $_GET : $_POST;
   foreach ($cgi as $key => $val) {

    if ($key != "vid/" && $key != "q" && !is_array($val)) 
	{
       $query_string = $key;
    }
  }
  $new=substr($query_string,4,strlen($query_string));
  $sql = db_query("SELECT v_price FROM requests where id='".$new."'");  
  
  while ($obj = db_fetch_object($sql))
  {
  $form['request'][] = array(
    '#type' => 'fieldset',
	 '#title' => t('Vacation Prices'),
	'#value' => $obj->v_price,
   
  );
  
  }
  
  return $form;
  }
  
  
  function edit_form() {
  
   $cgi = $_SERVER['REQUEST_METHOD'] == 'GET' ? $_GET : $_POST;
   foreach ($cgi as $key => $val) {

    if ($key != "vid/" && $key != "q" && !is_array($val)) 
	{
       $query_string = $key;
    }
  }
  $new=substr($query_string,4,strlen($query_string));
  $sql = db_query("SELECT id, v_price, v_date FROM requests where id='".$new."'");  
  
  while ($obj = db_fetch_object($sql))
  {
 $form['editform'] = array(
    '#type' => 'fieldset',
    '#title' => t('Request Info'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  );  
  $form['editform']['first'] = array(
    '#type' => 'textfield',
    '#title' => t('Name'),
    '#default_value' => $GLOBALS[user]->name, // changed
    '#description' => "Please enter your name.",
    '#size' => 20,
    '#maxlength' => 20,
  );
  $form['editform']['vdate'] = array(
    '#type' => 'textfield',
    '#title' => t('Vacation Date'),
    '#default_value' => $obj->v_date, // changed
    '#description' => "Please enter Date.",
    '#size' => 20,
    '#maxlength' => 20,
  );
  $form['editform']['vprice'] = array(
    '#type' => 'textfield',
    '#title' => t('Vacation Price'),
    '#default_value' => $obj->v_price, // changed
    '#description' => "Please enter Price.",
    '#size' => 20,
    '#maxlength' => 20,
  );
  $form['next'] = array(
    '#type' => 'submit',
    '#value' => 'Save',
  );
    $form_state['storage']['page_two'] = TRUE;
   
  $form['editform'][]=array(     
  '#value' => l("	SAVE", 'http://localhost/drupal?q=request/insert&'.'vid/'.$obj->id, array()),
	);
  }
 
  return $form;
  }
  
   function delete_request($form_state) {
	 $cgi = $_SERVER['REQUEST_METHOD'] == 'GET' ? $_GET : $_POST;
   foreach ($cgi as $key => $val) {

    if ($key != "vid/" && $key != "q" && !is_array($val)) 
	{
       $query_string = $key;
    }
  }
  $new=substr($query_string,4,strlen($query_string));
  $sql = db_query("delete FROM requests where id='".$new."'"); 
  if($sql)
  {
	
	drupal_goto($path = 'http://localhost/drupal?q=admin/settings/request/pending', $query = NULL, $fragment = NULL, $http_response_code = 302);
	drupal_set_message(t('Your Record is Dleted ok'));
  }
  
  }
  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 insert_form($form_state) {
	 $cgi = $_SERVER['REQUEST_METHOD'] == 'GET' ? $_GET : $_POST;
   foreach ($cgi as $key => $val) {

    if ($key != "vid/" && $key != "q" && !is_array($val)) 
	{
       $query_string = $key;
    }
  }
  $new=substr($query_string,4,strlen($query_string));
  
   if (isset($form_state['storage']['page_two'])) {
    $vdate = $form_state['values']['vdate'];
	 $vprice = $form_state['values']['vprice'];
     drupal_set_message(t($vdate.$vprice));
    //return;
  }

  $sql= db_query("update requests set v_date='".$vdate."', v_price='".$vprice."' where id=".$new."");

  if($sql)
  {
	
	drupal_goto($path = 'http://localhost/drupal?q=admin/settings/request/pending', $query = NULL, $fragment = NULL, $http_response_code = 302);
	drupal_set_message(t('Your Record is Updated ok'));
  }
  
  }
  
  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_goto($path = 'http://localhost/drupal?q=admin/settings/request/pending', $query = NULL, $fragment = NULL, $http_response_code = 302);
		  drupal_set_message(t('Your form has been saved.'));
		   unset ($form_state['storage']); 
 
 
  }
 
  }
 
  
?>

Comments

timpiche’s picture

The first statement says:
If there are no stored values, insert a new record into the db

The second statement says:
If the value already has an id, replace the info in the db with the new info.

This code is copied directly out of on eof my custom modules.


	if (!$form_state['values']['id']['storage']) {
		db_query(
		"INSERT INTO {YOUR DB TABLE} (YOUR, DB, FIELDS) VALUES ('%s', '%s', '%s')",
		$form_state['values']['id'],
		$form_state['values']['XXX'],
		$form_state['values']['XXX'],
		$form_state['values']['XXX'],
		);
		}
	else {
		$id = $form_state['values']['id'];
		db_query(
		"UPDATE {YOUR DB TABLE} SET XXX = '%s', XXX = '%s', XXX = '%s' WHERE id = $id",
		$form_state['values']['XXX'],
		$form_state['values']['XXX'],
		$form_state['values']['XXX']
		);
	}
	drupal_set_message(t('%id saved.', array('%id' => $form_state['values']['id'])));
	$form_state['redirect'] = "YOUR URL TO REDIRECT TO";
}

Hope this helps!

amira’s picture

thanks timpiche for ur concern,

i tried 2 statements that u recommended but i found that, it added ok but in case of edit "else" it didn't make any thing ..just i found the page becomes empty :S:S...which is the same problem i faced...do u know why..?

thanks in advance

timpiche’s picture

Can you please post your code with the new sql added so I can take a look?

amira’s picture

thanks timpiche again for trying to help me,


 function request_my_form_submit($form, &$form_state) {
  // Handle page 1 submissions
   $flage=true;
   if (!$form_state['values']['id']['storage']) {
   
         db_query("INSERT INTO {requests} (name, v_date, v_price) VALUES ('%s','%s','%s' )",
		    $form_state['values']['first'],
		    $form_state['values']['vdate'],
			$form_state['values']['vprice']);
		  drupal_set_message(t('Your form has been saved.'));
		  $flage=false;
        }
 else {
  
        $id = $form_state['values']['id'];

        db_query(
        "UPDATE {requests} SET v_date = '%s',v_price = '%s' WHERE id = $id ",
        $form_state['values']['vdate'],
        $form_state['values']['vprice']
        );
		  drupal_set_message(t('%id updated.', array('%id' => $form_state['values']['id'])));
		 

    }

   drupal_goto($path = 'http://localhost/drupal?q=admin/settings/request/pending', $query = NULL, $fragment = NULL, $http_response_code = 302);
}
timpiche’s picture

I dont know if you have a function to call the $id variable, so try pasting the code below into your module. I have added the function to call the id from your database.

Hope this works!!



function _request_id_load($id) {
	$row = db_fetch_object(db_query("SELECT * FROM {requests} WHERE id =%s", $id));
	return $row;
}

function request_my_form_submit($form, &$form_state) {
	$id_load = _request_id_load($id);
	// Handle page 1 submissions
	$flage=true;
	if (!$form_state['values']['id']['storage']) {

		db_query("INSERT INTO {requests} (name, v_date, v_price) VALUES ('%s','%s','%s' )",
		$form_state['values']['first'],
		$form_state['values']['vdate'],
		$form_state['values']['vprice']);
		drupal_set_message(t('Your form has been saved.'));
		$flage=false;
	}
	else {



		db_query(
		"UPDATE {requests} SET v_date = '%s',v_price = '%s' WHERE id = $id_load ",
		$form_state['values']['vdate'],
		$form_state['values']['vprice']
		);
		drupal_set_message(t('%id updated.', array('%id' => $form_state['values']['id'])));


	}

	drupal_goto($path = 'http://localhost/drupal?q=admin/settings/request/pending', $query = NULL, $fragment = NULL, $http_response_code = 302);
}


amira’s picture

$id--> is still null........!!!! so no change happened