Solarpitch Posted August 19, 2007 Share Posted August 19, 2007 Hi Guys, When a user submits a form on my page and there are errors, the page returns the errors and re-populates the correct fields - So the user doesnt have to complete them again. I using the below code to do that... <input style="width:200px; " name="userfile" type="file" id="menu_textbox" value="<?print $file_path; ?>" /> However, for the file upload field above it doesnt seem to work and the user must select the file they wish to upload from there computer again. Should the code for this be a little different or is there something I am missing? Quote Link to comment https://forums.phpfreaks.com/topic/65692-re-populating-text-fields-for-image-upload/ Share on other sites More sharing options...
simcoweb Posted August 19, 2007 Share Posted August 19, 2007 You can try this: <input style="width:200px;" name="userfile" type="file" id="menu_textbox" value="<? if(isset($_POST['submit'])) echo $_POST['userfile']; ?>"> Quote Link to comment https://forums.phpfreaks.com/topic/65692-re-populating-text-fields-for-image-upload/#findComment-328243 Share on other sites More sharing options...
plutomed Posted August 19, 2007 Share Posted August 19, 2007 I think if you use javascript:history.go(-1) ans your link back it does it automatically I think Quote Link to comment https://forums.phpfreaks.com/topic/65692-re-populating-text-fields-for-image-upload/#findComment-328254 Share on other sites More sharing options...
Solarpitch Posted August 20, 2007 Author Share Posted August 20, 2007 You can try this: <input style="width:200px;" name="userfile" type="file" id="menu_textbox" value="<? if(isset($_POST['submit'])) echo $_POST['userfile']; ?>"> I tried this and it still doesnt seem to work. The userfile text field still clears when the form returns errors instead of holding the value that the user selected. Quote Link to comment https://forums.phpfreaks.com/topic/65692-re-populating-text-fields-for-image-upload/#findComment-329249 Share on other sites More sharing options...
simcoweb Posted August 20, 2007 Share Posted August 20, 2007 Well, then let me ask you a question. What is the name of your 'submit' button in your form code? Quote Link to comment https://forums.phpfreaks.com/topic/65692-re-populating-text-fields-for-image-upload/#findComment-329256 Share on other sites More sharing options...
Solarpitch Posted August 20, 2007 Author Share Posted August 20, 2007 I use a hidden field to submit the value of the form... <input type="hidden" name="process" value="4"> <input type="submit" name="Submit" value="Submit Ad" onClick="return validate(this);"> Quote Link to comment https://forums.phpfreaks.com/topic/65692-re-populating-text-fields-for-image-upload/#findComment-329260 Share on other sites More sharing options...
simcoweb Posted August 20, 2007 Share Posted August 20, 2007 Ok, you have this: name="Submit" but in my code I had: name="submit" with lower case S. Change it to uppercase and give it a try. Quote Link to comment https://forums.phpfreaks.com/topic/65692-re-populating-text-fields-for-image-upload/#findComment-329263 Share on other sites More sharing options...
Solarpitch Posted August 20, 2007 Author Share Posted August 20, 2007 Umm.. Still no joy. It must be something to do with the properties of the type="file" textfield. But there is a way, cant understand why it wont work. Quote Link to comment https://forums.phpfreaks.com/topic/65692-re-populating-text-fields-for-image-upload/#findComment-329268 Share on other sites More sharing options...
MadTechie Posted August 20, 2007 Share Posted August 20, 2007 can you post the page ? in code tags (#) Quote Link to comment https://forums.phpfreaks.com/topic/65692-re-populating-text-fields-for-image-upload/#findComment-329278 Share on other sites More sharing options...
Solarpitch Posted August 20, 2007 Author Share Posted August 20, 2007 Not the page, its massive. Heres the important parts... [b]// FILE CODE[/b] function generate_rand($l){ $c= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; srand((double)microtime()*1000000); for($i=0; $i<$l; $i++) { $rand.= $c[rand()%strlen($c)]; } return $rand; } $uploaddir = "user_images/"; $maxfilesize = 1048576; //1 megabyte $filename = $_FILES['userfile']['name']; $filesize = $_FILES['userfile']['size']; $filetmpname = $_FILES['userfile']['tmp_name']; $valid = array (".jpg",".gif",".png"); if ($filename) { $error = ""; if ($filesize == 0) { $error_file .= "The submitted file was invalid.<br />"; $execute = "no"; } $type = strtolower(strstr($filename, '.')); if (!in_array($type, $valid)) { $error_file .= "The submitted file was of invalid type.<br />"; $execute = "no"; } if ($filesize>$maxfilesize) { $error_file .= "The submitted file was larger than a Megabyte.<br />"; $execute = "no"; } $randnum = generate_rand(10); $randnum .= $type; $file_exists = true; while ($file_exists) { if (file_exists("$uploaddir$randnum")) { $randnum = generate_rand(10); $randnum .= $type; }else{ $file_exists = false; } } if ($error_file == "") { if (move_uploaded_file($filetmpname, "$uploaddir$randnum")) { chmod("$uploaddir$randnum", 0644); $file_complete = ""; } else { $file_complete = "Your file could not be uploaded."; $execute = "no"; } }else{ echo ""; } }else{ echo ""; } [b]// EXECUTE FORM CODE[/b] if (($_POST['process'] == 4) & ($execute != "no")) { ..... do insertion to database stuff } [b]// HTML FORM PART[/b] <form name="myform" method="post" action="upload_ad.php"> ... <input type="hidden" name="MAX_FILE_SIZE" value="1048576)"> <input style="width:200px; " name="userfile" type="file" id="menu_textbox" value="<? if(isset($_POST['process'])) { echo $_POST['userfile']; } ?>" /> Quote Link to comment https://forums.phpfreaks.com/topic/65692-re-populating-text-fields-for-image-upload/#findComment-329290 Share on other sites More sharing options...
MadTechie Posted August 20, 2007 Share Posted August 20, 2007 Erm your trying to set a form file element.. thats which is always readonly so can't be done as its a security risk.. Quote Link to comment https://forums.phpfreaks.com/topic/65692-re-populating-text-fields-for-image-upload/#findComment-329295 Share on other sites More sharing options...
Solarpitch Posted August 20, 2007 Author Share Posted August 20, 2007 Ah I see what you mean! Is there anyway around this. User's wont be aware that they will need to select the file again once there was an error on the form, not best practice obviously. Quote Link to comment https://forums.phpfreaks.com/topic/65692-re-populating-text-fields-for-image-upload/#findComment-329297 Share on other sites More sharing options...
MadTechie Posted August 20, 2007 Share Posted August 20, 2007 theirs aworkaround but due to the security risks most have been coved as their classed as exploits and as any that may know, i couldn't say as they are a major secuirty risk. think about it your viewing a page and any file on your HDD could be taken without you knowing.. this is why its read only Quote Link to comment https://forums.phpfreaks.com/topic/65692-re-populating-text-fields-for-image-upload/#findComment-329302 Share on other sites More sharing options...
Solarpitch Posted August 20, 2007 Author Share Posted August 20, 2007 I see, I'll think of something. Just wonder how other sites implement this Quote Link to comment https://forums.phpfreaks.com/topic/65692-re-populating-text-fields-for-image-upload/#findComment-329308 Share on other sites More sharing options...
MadTechie Posted August 20, 2007 Share Posted August 20, 2007 implement what exaclty (got an example?) Quote Link to comment https://forums.phpfreaks.com/topic/65692-re-populating-text-fields-for-image-upload/#findComment-329311 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.