UploadAddForm.php

4 posts by 2 authors in: Forums > CMS Builder
Last Post: November 5, 2012   (RSS)

Hi,

When I upload a file to the form, the options I have already selected from the dropdowns are reset.

Is there a way I can get the options to save once the file has been uploaded? The data already entered into my text fields are being saved fine.

I am inserting the dropdowns using the following code:

<select name="currency">
<?php echo $optionsCurrency ?>
</select>



Thank you,
Greg

Re: [Jason] UploadAddForm.php

Hi Jason,

Here's the code main code:

<?php
// This form requires: CMSB v2.04 Build 1
require_once "../cmsAdmin-demo/lib/viewer_functions.php";
$tableName = 'listings';
$recordNum = null; // you must set either $recordNum or $preSaveTempId to null
$preSaveTempId = @$_REQUEST['preSaveTempId'] ? $_REQUEST['preSaveTempId'] : uniqid('x');
$errorsAndAlerts = '';

// get field options for Category
$fieldname = 'category';
$selectedValue = '';
$valuesAndLabels = getListOptions($tableName, $fieldname);
$optionsCategory = getSelectOptions($selectedValue, array_keys($valuesAndLabels), array_values($valuesAndLabels));

// get field options for Manufacturer
$fieldname = 'manufacturer';
$selectedValue = '';
$valuesAndLabels = getListOptions($tableName, $fieldname);
$optionsManufacturer = getSelectOptions($selectedValue, array_keys($valuesAndLabels), array_values($valuesAndLabels));

// get field options for Currency
$fieldname = 'currency';
$selectedValue = '';
$valuesAndLabels = getListOptions($tableName, $fieldname);
$optionsCurrency = getSelectOptions($selectedValue, array_keys($valuesAndLabels), array_values($valuesAndLabels));

// get field options for Age
$fieldname = 'age';
$selectedValue = '';
$valuesAndLabels = getListOptions($tableName, $fieldname);
$optionsAge = getSelectOptions($selectedValue, array_keys($valuesAndLabels), array_values($valuesAndLabels));

// get field options for Condition
$fieldname = 'condition';
$selectedValue = '';
$valuesAndLabels = getListOptions($tableName, $fieldname);
$optionsCondition = getSelectOptions($selectedValue, array_keys($valuesAndLabels), array_values($valuesAndLabels));

### upload actions
if (@$_REQUEST['submitForm'] && !preg_match("/multipart\/form-data/", @$_SERVER['CONTENT_TYPE'])) { die("Upload Error: &lt;form&gt; tag must have enctype=\"multipart/form-data\""); }
// print_r($_FILES);
foreach (getUploadInfoArrays() as $uploadInfo) { // add uploads
$errorsAndAlerts .= saveUpload($tableName, $uploadInfo['_fieldname'], $recordNum, $preSaveTempId, $uploadInfo, $newUploadNums);
}
if (@$_REQUEST['removeUpload']) { // delete upload
$uploadNum = @$_REQUEST['removeUpload'];
removeUpload($uploadNum, $recordNum, $preSaveTempId);
}

### insert record
$onlyUploadAction = @$_REQUEST['submitUpload'] || @$_REQUEST['removeUpload']; // don't save the record when the user uploads a file
if (@$_REQUEST['submitForm'] && !$onlyUploadAction) {

// error checking
// if (!@$_REQUEST['product']) { $errorsAndAlerts .= "Please specify product name!<br/>\n"; }

// update record
if (!@$errorsAndAlerts) {
mysqlStrictMode(false); // disable Mysql strict errors for when a field isn't defined below (can be caused when fields are added later)
$query = "INSERT INTO `{$TABLE_PREFIX}$tableName` SET
createdDate = NOW(),
createdByUserNum = '" .intval( @$CURRENT_USER['num'] ). "',
updatedDate = NOW(),
updatedByUserNum = '" .intval( @$CURRENT_USER['num'] ). "',
product = '" .mysql_escape( @$_REQUEST['product'] ). "',
category = '" .mysql_escape( @$_REQUEST['category'] ). "',
manufacturer = '" .mysql_escape( @$_REQUEST['manufacturer'] ). "',
description = '" .mysql_escape( @$_REQUEST['description'] ). "',
currency = '" .mysql_escape( @$_REQUEST['currency'] ). "',
list_price = '" .mysql_escape( @$_REQUEST['list_price'] ). "',
sale_price = '" .mysql_escape( @$_REQUEST['sale_price'] ). "',
open_to_offers = '" .mysql_escape( @$_REQUEST['open_to_offers'] ). "',
age = '" .mysql_escape( @$_REQUEST['age'] ). "',
`condition` = '" .mysql_escape( @$_REQUEST['condition'] ). "',
warranty = '" .mysql_escape( @$_REQUEST['warranty'] )."'";

mysql_query($query) or die("MySQL Error:<br/>\n". htmlspecialchars(mysql_error() ) . "\n");

$recordNum = mysql_insert_id();

// adopt temp uploads
adoptUploads($tableName, $preSaveTempId, $recordNum);
removeExpiredUploads(); // erase old expired u

$errorsAndAlerts = "Your record has been added! <br/>\n";
$_REQUEST = array();


}
}
?>



I am then inserting the HTML as follows:<?php include ("includes/user-AddForSale.php"); ?>
<!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>

<?php if (@$errorsAndAlerts): ?>
<div style="color: red; font-weight: bold; font-size: 16px;"><br/>
<?php echo $errorsAndAlerts; ?>
</div>
<?php endif ?>

<h1>Add For Sale Advert</h1>
<hr/>

<form method="post" name="uploadForm" action="?preSaveTempId=<?php echo $preSaveTempId ?>" enctype="multipart/form-data">
<input type="hidden" name="submitForm" value="1" />
<input type="hidden" name="num" value="<?php echo $recordNum ?>" />
<input type="hidden" name="preSaveTempId" value="<?php echo $preSaveTempId ?>" />
<input type="hidden" name="removeUpload" id="removeUpload" value="0" />

<table border="0" cellspacing="0" cellpadding="2">
<tr>
<td valign="top">Category</td>
<td><select name="category">
<?php echo $optionsCategory ?>
</select></td>
</tr>
<tr>
<td valign="top">Product</td>
<td><input class="text-input medium-input" type="text" name="product" value="<?php echo htmlspecialchars(@$_REQUEST['product']) ?>" size="30" /></td>
</tr>
<tr>
<td valign="top">Manufacturer</td>
<td><select name="manufacturer">
<?php echo $optionsManufacturer ?>
</select></td>
</tr>
<tr>
<td valign="top">Description</td>
<td><textarea class="text-input medium-input" rows="4" cols="50" name="description" value="<?php echo htmlspecialchars(@$_REQUEST['description']); ?>" size="30" /><?php echo htmlspecialchars(@$_REQUEST['description']) ?></textarea></td>
</tr>
<tr>
<td valign="top">Currency</td>
<td><select name="currency" value="<?php echo htmlspecialchars(@$_REQUEST['currency']) ?>">
<?php echo $optionsCurrency ?><?php echo htmlspecialchars(@$_REQUEST['currency']) ?>
</select></td>
</tr>
<tr>
<td valign="top">List Price</td>
<td><input class="text-input medium-input" type="text" name="list_price" value="<?php echo htmlspecialchars(@$_REQUEST['list_price']); ?>" size="30" /></td>
</tr>
<tr>
<td valign="top">Sale Price</td>
<td><input class="text-input medium-input" type="text" name="sale_price" value="<?php echo htmlspecialchars(@$_REQUEST['sale_price']); ?>" size="30" /></td>
</tr>
<tr>
<td valign="top">Open to Offers</td>
<td><input type="checkbox" name="open_to_offers" value="1" <?php checkedIf(1, @$_REQUEST['open_to_offers']); ?>" />Tick to display "All offers will be considered"</td>
</tr>
<tr>
<td valign="top">Age</td>
<td><select name="age">
<?php echo $optionsAge ?>
</select></td>
</tr>
<tr>
<td valign="top">Condition</td>
<td><select name="condition">
<?php echo $optionsCondition ?>
</select></td>
</tr>
<tr>
<td valign="top">Warranty</td>
<td><input class="text-input medium-input" type="text" name="warranty" value="<?php echo htmlspecialchars(@$_REQUEST['warranty']); ?>" size="30" /></td>
</tr>
<tr>
<td valign="top">Photo Uploads</td>
<td>

<?php // load uploads
$fieldName = 'uploads';
$tempUploads = getUploadRecords($tableName, $fieldName, $recordNum, $preSaveTempId);
?>

<?php foreach ($tempUploads as $upload): ?>
<?php if ($upload['hasThumbnail']): ?>
<img src="<?php echo $upload['thumbUrlPath'] ?>" width="<?php echo $upload['thumbWidth'] ?>" height="<?php echo $upload['thumbHeight'] ?>" alt="" /><br/>
<?php endif ?>

<?php echo $upload['filename'] ?>
<a href="#" onclick="document.uploadForm.removeUpload.value='<?php echo $upload['num'] ?>';document.uploadForm.submit();">remove</a><br/><br/>

<?php endforeach ?>

<?php if (!$tempUploads): ?>
There are no uploads yet.<br/><br/>
<?php endif ?>


Upload Files<br />
<input type="file" name="<?php echo $fieldName ?>[]"/><br/>
<!--<input type="file" name="<?php echo $fieldName ?>[]"/><br/>-->
<input type="submit" name="submitUpload" value="Upload" /><br/>

</td>
</tr>
</table><br/>
<hr/>
<input class="button" type="submit" name="submitForm" value="Add Record &gt;&gt;" />

</form>
</body>
</html>
Many thanks for your help. I really appreciate it.

Regards,
Greg

Re: [gversion] UploadAddForm.php

By Jason - November 5, 2012

Hi Greg,

I see. So the problem is maintaining the values in your dropdown fields after an image has been uploaded. Is that right?

If so, here is a different method to getting list field information that may help keep your code a little cleaner. It uses the getListOptions() function.

For example, here is how you can set up your category drop down:

<select name="category">
<option value = "">-Select-</option>
<?php foreach (getListOptions($tableName, 'category') as $value => $label): ?>
<option value = "<?php echo htmlspecialchars($value);?>" <?php selectedIf($value, @$_REQUEST['category']);?>> <?php echo htmlspecialchars($label);?> </option>
<?php endforeach ?>
</select>


The selectedIf() function sets the selected = "selected" attribute of any option that matches what is currently in the category field of the $_REQUEST array.

Hope this helps
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/