...

View Full Version : HELP PLEASE !!!! How do I set a variable from input box



porsche21
08-09-2007, 06:48 PM
This code works if I set $init = "2" perfectly fine but thats not what I want. I want to be able to insert initials in the text box and have them sent to my database table maint.responses when I submit the form. What should I do to accomplish this?


<?php
$GLOBALS['phpgw_info']['flags']['currentapp'] = 'apps/maint';
include('../../header.inc.php');

if (($_POST['form_submit']) || ($_POST['submit']))
$add_discrep = FALSE;
else
$add_discrep = TRUE; // submit and add discrepencies

// top navigation menus
if (! $add_discrep)
top_menu();


$user = $GLOBALS['phpgw_info']['user']['account_id'];
if ($user == 1)
{
echo "You must be logged into your account to use this application<BR>";
exit;
}

//echo "POST:"; print_r($_POST);

// check if info was passed from new_form.php
if (($_POST['template']) && (is_numeric($_POST['template'])))
{
$template_id = $_POST['template'];
$new = TRUE; // this is a new form
}
else
$new = FALSE; // form in progress, will need to look up previous responses


// just starting form, get the template id and display the form
if ($_POST['submit'])
show_form($template_id, $new);
// if form was submitted, check for errors, then reshow or process
else if (($_POST['form_submit']) || ($_POST['add_discrep']))
{
//check for errors
$errors = checkForErrors($_POST);
if ($errors['num'] != 0)
show_form($template_id, $new, $errors['errorlist']);
else
{
process_data($_POST, $add_discrep);
}
}
else if ($_REQUEST['id'] && (is_numeric($_REQUEST['id'])))
{
$log_id = $_REQUEST['id'];
$new = FALSE;
$log_info = getLogInfo($log_id);
show_form($log_info['template_id'], $new);

}
// if error - bad input
else
{
echo 'Unknown Form';
exit;
}

$GLOBALS['phpgw']->common->phpgw_footer();


/**************************************************************************************************** ************************************************************/
function show_form($template_id, $new, $errorlist='')
{
$log = getTemplateInfo($template_id);
$tasks = getTasks($template_id);

if (! $new)
{
if (is_numeric($_REQUEST['id']))
{
$log_id = $_REQUEST['id']; // this may need to change to $_REQUEST
$log_info = getLogInfo($log_id);
$log_info['date_started'] = reorder_date($log_info['date_started']);
$commonhiddenvars = '<input type="hidden" name="log_id" value='.$log_id.'><BR>';
// get the previous responses for this log#
}
else
{
echo "Unknown log ID<BR>";
exit;
}
}
else
{
//$date_started = date('m/d/Y');
$log_info = array(
'site_id' => null,
'date_started' => date('m/d/Y'),
'template_id' => $template_id);

}

$sites = getSites(false, true); // true for only sites assigned to this tech
foreach ($sites as $key=> $value)
{
$sitelist .= "<option value='$key'";
if ($log_info['site_id'] == $key)
$sitelist .= " SELECTED";
$sitelist .= ">";
$sitelist .=$value;
$sitelist .= "</option>\n";
}
$commonhiddenvars .= '<input type="hidden" name="template" value='.$log_info['template_id'].'><BR>
<input type = "hidden" name="new" value ='.$new.'>';
$GLOBALS['phpgw']->template->set_file('list_table','show_form.tpl');
$GLOBALS['phpgw']->template->set_block('list_table','task_list','list');


$GLOBALS['phpgw']->template->set_var(
array ( "errorlist" => $errorlist,
"commonhiddenvars"=> $commonhiddenvars,
"log_name"=> $log['log_name'],
"site_list"=> $sitelist,
"date_started"=> $log_info['date_started']
));

foreach ($tasks as $group_id => $task)
{
// Set the task group name aligned to left and bold
$GLOBALS['phpgw']->template->set_var(
array( 'task' => nameGroup($group_id),
'task_id' => '&nbsp;',
'align' => 'left',
'attrib_beg' => '&nbsp;<b>',
'attrib_end' => '</b>',
'response' => '&nbsp;',
'init' => '<input type="text" size="3" name="init">',
'date_completed' => '&nbsp;'
));
$GLOBALS['phpgw']->template->parse('list', 'task_list', true);

// set each task
foreach($task as $task_info => $value)
{
if ($_POST[$task_info])
$checked = 'CHECKED ';
else
$checked = null;
$GLOBALS['phpgw']->template->set_var(
array ( "task_id"=> $task_info,
"task"=> $value,
'align' => 'left',
'attrib_beg' => '&nbsp;&nbsp;&nbsp;',
'attrib_end' => '',
"response"=> '<input type="checkbox" name="'.$task_info.'" size="20" value="'.$task_info.'" '.$checked.' tabindex="2">',
"init" => $init,
"date_completed"=> '',
));
$GLOBALS['phpgw']->template->parse('list', 'task_list', true);
}
}

if ($new)
$d_date = $discrep = $d_init = $d_fixed = '';
else
{
//get the discrepencies for this log # if editing info
$d_date = $discrep = $d_init = $d_fixed = '';
}
$GLOBALS['phpgw']->template->set_block('list_table','discreps','list2');
$GLOBALS['phpgw']->template->set_var(
array ( "d_date"=> $d_date,
"discrep"=> $discrep,
'd_init' => $d_init,
'd_fixed' => $d_fixed));
$GLOBALS['phpgw']->template->parse('list2', 'discreps', true);
$GLOBALS['phpgw']->template->pparse('out','list_table', true);
}

/**************************************************************************************************** ************************************************************/
function process_data($values, $init, $add_discrep)
{

$tasks = getTasks($values['template']);
//echo "in process data<BR>";
echo "INPUT: "; print_r($values);

if ($values['new'] == TRUE)
{
$start_log ="INSERT INTO maint.logs (template_id, date_started, date_lastchange, status, site_id,lastchanged_by) "
. "VALUES ($values[template], '". date("m/d/Y") ."', '". date("m/d/Y") ."', $values[status], $values[site], "
. $GLOBALS['phpgw_info']['user']['account_id'] .")";


$GLOBALS['maint']->db->query($start_log);
/*echo "<br>Log successfully submited<br>";*/
echo "<BR>new log query: $start_log<BR>";
//$GLOBALS['maint']->db->query("SELECT CURRVAL(maint.logs.log_id) as log_id FROM maint.logs;");
$GLOBALS['maint']->db->query("SELECT currval('maint.logs_log_id_seq') as log_id;");


if ($GLOBALS['maint']->db->next_record(PGSQL_ASSOC))
$log_id = $GLOBALS['maint']->db->f("log_id");
else
$log_id = 1; // after an item in the table, change this to an error

foreach ($tasks as $group_id => $task)
{ //echo "task: "; print_r($task );
foreach($task as $task_info => $value)
{

if ($values[$task_info])
{
/*var_dump($init);
die($init);*/
//input into responses
$sql = "INSERT INTO maint.responses ";
$sql .= "(log_id, task_id, response, lastchanged_by, date_lastchanged, group_id, group_init) ";
$sql .= "VALUES ($log_id, $values[$task_info], 'CHECKED', ". $GLOBALS['phpgw_info']['user']['account_id'] .", '". date("m/d/Y") . "',$group_id, $init)";
echo $sql."<BR>";
$GLOBALS['maint']->db->query($sql);
} //end if
} //end foreach task
} // end foreach tasks
} // end if new log
else
{
foreach ($tasks as $group_id => $task)
{// echo "task: "; print_r($task );
foreach($task as $task_info => $value)
{
if ($values[$task_info])
{
//update responses
$sql = "UPDATE maint.responses ";
$sql .= "set response = 'CHECKED', ";
$sql .= "lastchanged_by = ". $GLOBALS['phpgw_info']['user']['account_id'] .", ";
$sql .= "date_lastchanged = '". date("m/d/Y")."', ";
$sql .= "group_id = $group_id";
$sql .= "group_init = $init";
$sql .= " WHERE task_id = $values[task_info] AND log_id = $log_id;";
echo $sql."<BR>";
$GLOBALS['maint']->db->query($sql);
} //end if
} //end foreach task
} // end foreach tasks
} // end else not new

//echo "<BR>sql: $sql<BR>";
//echo "Log has been submitted.<BR>";

if ($add_discrep)
Header("Location: " . $GLOBALS['phpgw']->link("add_discrep.php?log_id=".$log_id));

}

/**************************************************************************************************** ************************************************************/
function checkForErrors($data)
{
$errors = array ('num' => 0, //set num of erros to 0 as default
'errorlist' => '');

if ($data['site'] == -1)
{
$errors['num']++;
$errors['errorlist'] .= 'You must choose a site.<BR>';
}
return $errors;
}
?>

mlseim
08-09-2007, 08:46 PM
I don't know if there's enough information here to answer this.

Maybe an MySQL expert may see something.

I just wonder if $init is set-up in your database as an integer
instead of a string ... thus it accepts "2" but not "MS".
(just a stab in the dark by a non-MySQL expert).



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum