<?php
  // This is a sample form for adding a record and it's uploads

  require_once "../lib/viewer_functions.php";
  $tableName       = 'every_field_multi';
  $recordNum       = null; // you must set either $recordNum or $preSaveTempId to null
  $preSaveTempId   = @$_REQUEST['preSaveTempId'] ? $_REQUEST['preSaveTempId'] : uniqid('x');
  $errorsAndAlerts = '';

  ### insert record
  if (@$_REQUEST['submitForm']) {

    // error checking
    if (!@$_REQUEST['title'])    { $errorsAndAlerts .= "Please specify title!<br/>\n"; }

    // update record
    if (!@$errorsAndAlerts) {
      $colsToValues = [
        'createdDate='     => 'NOW()',
        'createdByUserNum' => @$CURRENT_USER['num'],
        'updatedDate='     => 'NOW()',
        'updatedByUserNum' => @$CURRENT_USER['num'],
        'title'            => @$_REQUEST['title'],
      ];
      $newRecordNum = mysql_insert($tableName, $colsToValues, true);

      // adopt temp uploads (IMPORTANT - DON'T FORGET THIS STEP!!!)
      adoptUploads($tableName, $preSaveTempId, $newRecordNum);
      removeExpiredUploads(); // erase old expired uploads

      // display thanks message and clear form
      $errorsAndAlerts = "Thanks, we've added your record!";
      $_REQUEST      = array();
    }
  }

?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<style type="text/css">
  body, td { font-family: arial }
</style>
</head>
<body>

<h1>Add Record</h1>

<?php if (@$errorsAndAlerts): ?>
  <div style="color: #C00; font-weight: bold; font-size: 14px;"><?php echo $errorsAndAlerts; ?></div>
<?php endif ?>

<hr/>

<form method="post" action="<?php echo $_SERVER['SCRIPT_NAME']; ?>">
<input type="hidden" name="submitForm" value="1" />
<input type="hidden" name="num" value="<?php echo $recordNum ?>" />
<input type="hidden" name="preSaveTempId" value="<?php echo htmlencode($preSaveTempId) ?>" />

<table border="0" cellspacing="0" cellpadding="2">
 <tr>
  <td valign="top">PreSaveTempId</td>
  <td><?php echo htmlencode($preSaveTempId) ?></td>
</tr>
 <tr>
  <td valign="top">Title</td>
  <td><input class="text-input medium-input" type="text" name="title" value="<?php echo htmlencode(@$_REQUEST['title']) ?>" size="30" /></td>
</tr>
 <tr>
  <td valign="top">Uploads</td>
  <td>

    <?php /* TODO: Add security check in uploadForm2_iframe.php to limit access to only allowed uploads */ ?>
    <iframe src='uploadForm3_iframe.php?table=<?php echo $tableName ?>&amp;field=upload&amp;num=<?php echo $recordNum ?>&amp;preSaveTempId=<?php echo htmlencode($preSaveTempId) ?>'
            height='100' width='600' frameborder='0' scrolling='no'>
    </iframe><br/>
  </td>
</tr>
</table><br/>
<hr/>
<input class="btn btn-primary" type="submit" name="submitForm" value="Add Record &gt;&gt;" />

</form>

</body>
</html>
