My code is below. Lines that are not indented are debug lines or things I've been trying to get this to work. Basically, the 'filename' hidden input's value is not setting so on submit, the code that checks the $_POST values is returning an error because 'filename' is missing. This is within a CMS in a module that allows adding media. Either a video file can be uploaded (uploading the file itself to the server works, it's when I try to submit to save my data info to the mySQL database that the error occurs), or the option to include iframe info to embed a video from a secondary source such as youtube. Embedding video iframe info works, uploading a video file works, but saving data if a video file is uploaded does not. Any suggestions? This has been bugging me for about a week.

<div id="content_left">
  <div class="section">
    <div class="title_wrapper">
      <div class="title_wrapper_content" id="tabmenu">
        <h2><?=($media['id']) ? "Edit Media" : "Add Media";?></h2>
        <ul class="section_menu right">
          <li><a href="#details"><em>Details</em></a></li>
          <li><a href="#files"><em>Files</em></a></li>
        </ul>
      </div>
    </div>
    <div class="section_content">
      <div class="section_content_inner">
        <? if(isset($errors)): ?>
<pre><? print_r($_POST); ?></pre>
          <div class="section" style="padding:10px; margin-bottom:0px;">
            <ul class="system_messages">
              <li class="red"><span class="ico"></span>
                <strong class="system_title">Please correct the following:</strong>
                <ul>
                  <? foreach($errors as $key => $value): ?>
                    <li style="padding:0px; margin:0px; list-style-type:none;">
                      <?=inflector::humanize($value)?>
                    </li>
                  <? endforeach; ?>
                </ul>
              </li>
            </ul>
          </div>
        <? endif; ?>
        <form action="/<?=url::current()?>" class="search_form" method="post" name="aemedia">
          <fieldset id="details">
            <div class="forms">
              <div class="row">
                <label><span class="asterisk">*</span> Category:</label>
                <div class="inputs">
                  <span class="input_wrapper select_wrapper" style="width:300px">
                    <select name="category_id" class="input-select" >
                      <option value="">Select a Category</option>
                      <? foreach ($categories as $cat): ?>
                        <option value="<?=$cat['id']?>" <?=($media['category_id'] == $cat['id']) ? "selected" : ""?>>
                          <?=($cat['level'] > 0) ? str_repeat("&nbsp;", ($cat['level']) * 5)."|__ " : ""?>
                          <?=ucwords(inflector::humanize($cat['name']))?>
                        </option>
                      <? endforeach; ?>
                    </select>
                  </span>
                </div>
              </div>
              <div class="row">
                <label><span class="asterisk">*</span> Title:</label>
                <div class="inputs">
                  <span class="input_wrapper">
                    <input class="text" name="title" type="text" value="<?=$media['title']?>" />
                  </span>
                </div>
              </div>
              <div class="row" style="overflow:visible;">
                <label>Embed Video:</label>
                <div class="inputs">
                  <textarea name="embed" class="text" style="width:400px; height:200px;" ><?=$media['embed']?></textarea>
                </div>
              </div>
              <div class="row" style="overflow:visible;">
                <label>Abstract:</label>
                <div class="inputs">
                  <textarea name="article" class="text" style="width:400px; height:100px;" ><?=$media['article']?></textarea>
                </div>
              </div>
            </div>
          </fieldset>
          <fieldset id="files">
            <div class="forms">
              <div class="rows">
                <div id="uploader">
                  <input type="file" name="mediaUpload" id="Filedata" accept="*.avi;*.mov;*.mp3;*.mpeg;*.mpg;*.wmv;|Media Files" maxlength="1" title="ftype:file" class="/media/media/" disabled="disabled" />
                </div>
                <ul id="file_list">
                  <li style="text-align:center; padding:20px; font-size:14px; list-style:none; display:<?=(count($files)==0) ? "block" : "none"?>;" id="no_files">There are currently no files uploaded.</li>
                  <? if(count($files) != 0): ?>
                    <? foreach ($files as $file): ?>
                      <li style="border-bottom:1px solid #F1F1F1; list-style:none; padding:5px;" id="file_<?=$file['id']?>">
                        <input type="hidden" name="files[<?=$file['id']?>][id]" value="<?=$file['id']?>">
                        <input type="hidden" name="files[<?=$file['id']?>][path]" value="<?=$file['path']?>">
<? if ($media['filename'] == "") $media['filename'] = $file['path']; ?>
<? $filepath = $file['path']; ?>
                        <div style="float:left;">
                          <?=util::get_file_icon($file['path'])?>
                          <div class="actions_menu" style="text-align:center;">
                            <ul>
                              <li><a class="delete" href="javascript:delete_file('<?=$file['id']?>', 'media', '<?=$file['path']?>')">Delete</a></li>
                            </ul>
                          </div>
                        </div>
                        <div style="float:left; padding:0px 10px;">
                          <?=basename($file['psth'])?>
                          <?="(".@filesize(DOCROOT.$file['path'])." Bytes)"?><br />
                          <textarea name="files[<?=$file['id']?>][description]" style="height:60px;"><?=$file['description']?></textarea>
                        </div>
                        <div style="clear:both;"></div>
                      </li>
                    <? endforeach; ?>
                  <? endif; ?>
                </ul>
              </div>
            </div>
          </fieldset>
          <fieldset>
            <div class="forms">
              <div class="row">
                <div class="inputs">
                  <input type="submit" class="button" value="Save"> or <a href="/media/manage/" style="font-size:16px;">Cancel</a>
                  <!--input type="hidden" name="filename" value="<?=$media['filename'] != "" ? $media['filename'] : $file['path'] ?>"-->
<!--input type="hidden" name="filename" value="<?=$files[$file['id'][path]]?>"-->
<input type="hidden" name="filename" value="<?=$filepath?>">
                </div>
              </div>
            </div>
          </fieldset>
        </form>
      </div>
    </div>
  </div>
</div>
<div id="content_right">
  <h3>Quick Tip</h3>
  <p>Use this form to add/edit media postings on the website.</p>
  <p>Use the Embed Video field to embed media from another source (ie. YouTube.com, etc.).</p>
  <p>Use the Abstract field to give a brief description of the media.</p>
  <p><strong>Note : <span class="asterisk">*</span> When embedding video code, please make the width no more than 300px.</strong></p>
</div>

Recommended Answers

All 3 Replies

Member Avatar for diafol

FOr uploading files, your form tag needs the relevant enctype attribute ("multipart/form-data").

commented: yes +5

As I said, the files get uploaded to the server without issue. It's saving the necessary info to the database that isn't working. The 'filename' hidden input should be the pathname where the file was uploaded to, but it's not getting set.

Not sure if this will help any, but here's the JS/jQuery code that uploads the file. The alert pop-up showing resp is showing the filename of the file uploaded.

if ($('#Filedata').length != 0){
		var controller	= $("#Filedata").attr("name");
		var accept 		= $("#Filedata").attr('accept').split('|');
		var multi   	= ($('#Filedata').attr('maxlength') == 0) ? true : false;
		var scriptData	= $("#Filedata").attr('title');
	}
	
	if ($('#uploader').length != 0){
		var folder 		= $("#Filedata").attr('class');
		$('#file_list').sortable({handle: 'img'});
		$('#uploader').uploadify({ 
		  'uploader':  '/assets/js/jquery/uploadify/uploadify.swf', 
			'script':  '/uploader/upload/', 
			'folder':  folder,
		   'fileExt':  accept[0], 
		  'fileDesc':  accept[1],
		'scriptData':  {'data' : scriptData},
		 'buttonImg':  '/assets/js/jquery/uploadify/button.png',
		 'cancelImg':  '/assets/js/jquery/uploadify/cancel.png',
			 'wmode':  'transparent',
			 'multi':  multi,
			  'auto':  true,
			 'width':  90,
			'height':  21,
		   'onError':  function (event, queueID, fileObj, errorObj) {   
			   //alert("ERROR Event " + event);
			   //alert("ERROR Queue ID " + queueID);
			   //alert("ERROR File NAME " + fileObj.name);
			   alert("ERROR: " + errorObj.type + " -> " + errorObj.info);   
		   },
		  'onComplete':  function(event, queueID, fileObj, response) {
				//alert("RESPONSE -> " + response);
			  	var resp = jQuery.parseJSON(response);
				//alert("COMPLETE Event " + event);
				//alert("COMPLETE Queue ID " + queueID);
				//alert("COMPLETE File NAME " + fileObj.name);
				//alert("RESPONSE -> " + myObject);
				alert("RESPONSE -> " + resp['filename']);
				if (resp['filename'] != ""){
					var file_path = folder+resp['filename'];
					$('#no_files').css('display', 'none');
					$("#file_list")
					.fadeIn("slow")
					.append('<li style="border-bottom:1px solid #F1F1F1; list-style:none; padding:5px;" id="file_'+queueID+'"><input type="hidden" name="files['+queueID+'][id]" value="'+queueID+'"><input type="hidden" name="files['+queueID+'][path]" value="'+file_path+'"><div style="float:left;">'+resp['thumbnail']+'<div class="actions_menu" style="text-align:center;"><ul><li><a href="javascript:delete_file(\''+queueID+'\', \'' +controller+ '\', \''+file_path+'\');" class="delete">Delete</a></li></ul></div></div><div style="float:left; padding: 0px 10px;">'+resp['filename']+' ('+fileObj.size+' Bytes)<br /><textarea name="files['+queueID+'][description]" style="height:60px;"></textarea></div><div style="clear:both;"></div></li>')
					.sortable({handle: 'img'});
				} else {
					alert("ERROR: " + resp['error']);
				}
		   }
		 });
	}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.