By amira on
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
The first statement says: If
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.
Hope this helps!
thanks timpiche for ur
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
Can you please post your code
Can you please post your code with the new sql added so I can take a look?
thanks timpiche again for
thanks timpiche again for trying to help me,
I dont know if you have a
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!!
$id--> is still
$id--> is still null........!!!! so no change happened