StefanRSA Posted October 14, 2009 Share Posted October 14, 2009 Hi, I have a form that submit to itself with a file upload field and two other hidden field values. If I put a submit button on the page, all is working fine. I don't like the submit button as its wasting space. So I want to use <input size="10" type="file" name="img1" onchange="image.submit();"> If I add print_r($_FILES); I do get the array but the if(isset($_POST['submit'])){} does nothing... My form is as follow..... <form method="POST" name="image" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data" > <table> <input id="ad" name="ad" value="<?php echo $adnr;?>" type="hidden"/> <input id="user" name="user" value="<?php echo $user;?>" type="hidden" /> <tr><td><input size="10" type="file" name="img1" onchange="image.submit();"></td></tr> <tr><td><input type="submit" name="upload" value="Upload"></td></tr> </table> </form> Link to comment https://forums.phpfreaks.com/topic/177664-solved-how-to-get-values-from-_post-with-onchangeformsubmit/ Share on other sites More sharing options...
Adam Posted October 14, 2009 Share Posted October 14, 2009 That's because you have no input named "submit". The following would work: if (isset($_FILES['img1'])) { Link to comment https://forums.phpfreaks.com/topic/177664-solved-how-to-get-values-from-_post-with-onchangeformsubmit/#findComment-936785 Share on other sites More sharing options...
StefanRSA Posted October 14, 2009 Author Share Posted October 14, 2009 Thanks Mr Adam! Now, my files gets uploaded but nothing else happen... It must be because none of my field values gets posted? Any suggestions? Link to comment https://forums.phpfreaks.com/topic/177664-solved-how-to-get-values-from-_post-with-onchangeformsubmit/#findComment-936837 Share on other sites More sharing options...
simshaun Posted October 14, 2009 Share Posted October 14, 2009 The submit button may be a waste of space, but its there for a reason. What if a person accidentally chooses the wrong file? Anyways, you could always do this: <?php $post = array(); $errors = array(); if (count($_POST) > 0 && isset($_FILES['img1'])) { $post['ad'] = $_POST['ad']; $post['user'] = $_POST['user']; // Error checking $field = 'img1'; if ($_FILES[$field]['name'] == '') { // Error $errors[$field] = 'Required'; } // End error checking // If no errors if (count($errors) == 0) { // Handle Upload // Perform db queries // Redirect } } Link to comment https://forums.phpfreaks.com/topic/177664-solved-how-to-get-values-from-_post-with-onchangeformsubmit/#findComment-936851 Share on other sites More sharing options...
mrMarcus Posted October 14, 2009 Share Posted October 14, 2009 first off, to make your life much easier now and in the future, you need to understand what things do what and why. for example, it's obvious you do not know what this does: if(isset($_POST['submit'])){} .. i've seen this more and more over the last little while. $_POST['submit'] is not a function specific to forms, etc., it is seen often because most people name the button that is usually pushed to submit a form, submit .. it just makes sense. BUT, you can name it whatever you want, or you can check to see if other variables have been passed .. it's like saying, "if the input field with the name 'submit' has been passed, then show the content". so, looking at your form, <input type="submit" name="upload" value="Upload">, you could use: if (isset ($_POST['upload'])) {} since 'upload' is the name of the input. that is all. Link to comment https://forums.phpfreaks.com/topic/177664-solved-how-to-get-values-from-_post-with-onchangeformsubmit/#findComment-936856 Share on other sites More sharing options...
StefanRSA Posted October 14, 2009 Author Share Posted October 14, 2009 Now I am a bit confused... My code is still not working so I am pasting my complete script. What am I doing wrong? <?php if (isset($_FILES['upload'])) { error_reporting(0); $pictot = 4; $change=""; $imgNumb=1; define ("MAX_SIZE","1500"); function getExtension($str) { // To get the image extention $i = strrpos($str,"."); if (!$i) { return ""; } $l = strlen($str) - $i; $ext = substr($str,$i+1,$l); return $ext; } $errors=0; // Set error as 0 do{ if($_FILES["img$imgNumb"]["tmp_name"]!=''){ $image =$_FILES["img$imgNumb"]["name"]; $uploadedfile = $_FILES["img$imgNumb"]['tmp_name']; if ($image){ $filename = stripslashes($_FILES["img$imgNumb"]['name']); $extension = getExtension($filename); $extension = strtolower($extension); // Verify Image Extention if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")){ $change='<div class="msgdiv">Unknown Image extension </div> '; $errors=1; } else{ $size=filesize($_FILES["img$imgNumb"]['tmp_name']); if ($size > MAX_SIZE*1024){ // Check image size $change='<div class="msgdiv">You have exceeded the size limit!</div> '; $errors=1; } if($extension=="jpg" || $extension=="jpeg" ){ $uploadedfile = $_FILES["img$imgNumb"]['tmp_name']; $src = imagecreatefromjpeg($uploadedfile); } else if($extension=="png"){ $uploadedfile = $_FILES["img$imgNumb"]['tmp_name']; $src = imagecreatefrompng($uploadedfile); } else { $src = imagecreatefromgif($uploadedfile); } echo $scr; list($width,$height)=getimagesize($uploadedfile); $newwidthX=400; $newheightX=($height/$width)*$newwidthX; if($newheightX > 520) { $newheight=520; $newwidth=($newwidthX/$newheightX)*$newheight; } else { $newheight = $newheightX; $newwidth = $newwidthX; } $tmp=imagecreatetruecolor($newwidth,$newheight); $newwidth1=130; $newheight1=($height/$width)*$newwidth1; $tmp1=imagecreatetruecolor($newwidth1,$newheight1); if ($imgNumb == 1){ $newwidth2=65; $newheight2=($height/$width)*$newwidth2; $tmp2=imagecreatetruecolor($newwidth2,$newheight2); } imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height); imagecopyresampled($tmp1,$src,0,0,0,0,$newwidth1,$newheight1,$width,$height); if ($imgNumb == 1){ imagecopyresampled($tmp2,$src,0,0,0,0,$newwidth2,$newheight2,$width,$height); } /// SET IMAGE NAMES HERE!!!!!!!!!!!!!!!!!!!!!!!!! //$image_name=time().'.'.$extension; $Unique=microtime(); // We want unique names, right? $image_name=md5($Unique).'.'.$extension; $dbimgname=$imgNumb.'_'.$image_name; $filename = '../ad_images/test/'.$imgNumb.'_'.$image_name; $filename1 = '../ad_images/test/130/'.$imgNumb.'_'.$image_name; if ($imgNumb == 1){ $filename2 = '../ad_images/test/65/'.$imgNumb.'_'.$image_name; } imagejpeg($tmp,$filename,70); imagejpeg($tmp1,$filename1,70); if ($imgNumb == 1){ imagejpeg($tmp2,$filename2,70); } imagedestroy($src); imagedestroy($tmp); imagedestroy($tmp1); if ($imgNumb == 1){ imagedestroy($tmp2); } }} } $imgNumb++; } while($_FILES["img$imgNumb"][name]); //If no errors registred, print the success message if(isset($_POST['upload']) && !$errors){ $change=' <div class="msgdiv"> <img src="../ad_images/test/130/'.$dbimgname.'" border="0px" /> </div>'; echo 'The Image!!!<br>'.$change; } else if(isset($_POST['upload']) && $errors){ echo 'Error!!! '.$change.' - Try Again!'; ?> <form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data" > <table> <tr><td><input size="10" type="file" name="img1"></td></tr> <tr><td><input type="submit" name="upload" value="Upload"></td></tr> </table> </form> <? } } else { print_r($_FILES); ?> <form method="POST" name="image" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data" > <table> <tr><td><input size="10" type="file" name="img1" onchange="image.submit();"></td></tr> <tr><td><input type="submit" name="upload" value="Upload"></td></tr> </table> </form> <?} ?> Link to comment https://forums.phpfreaks.com/topic/177664-solved-how-to-get-values-from-_post-with-onchangeformsubmit/#findComment-936861 Share on other sites More sharing options...
mrMarcus Posted October 14, 2009 Share Posted October 14, 2009 without any further symptoms to the issue, how can anybody help you? you post your 'entire code', say, "somethings wrong with it", and expect people to just solve it. this is a free board meaning people take their own time to help, so the less time we can spend trying to decipher what it is you're going on about, the better and more efficiently we can help. just off-hand, without going any further til you post what error you're getting or what is happening when you try your script, i can see that you have two (2) method="post" in your <form> .. that won't stop your script from working though. Link to comment https://forums.phpfreaks.com/topic/177664-solved-how-to-get-values-from-_post-with-onchangeformsubmit/#findComment-936870 Share on other sites More sharing options...
StefanRSA Posted October 14, 2009 Author Share Posted October 14, 2009 Sorry MrMarkus... I was just thinking that it will help... When I run the script the images gets uploaded but the section if(isset($_POST['upload']) && !$errors){ $change=' <div class="msgdiv"> <img src="../ad_images/test/130/'.$dbimgname.'" border="0px" /> </div>'; echo 'The Image!!!<br>'.$change; } does not fire off. This section must display the uploaded image and does, if I only work with the button and remove the onchange="image.submit();" from the file field. But with onchange="image.submit();" the image gets uploaded but nothing else happen... Just a blank screen Link to comment https://forums.phpfreaks.com/topic/177664-solved-how-to-get-values-from-_post-with-onchangeformsubmit/#findComment-936876 Share on other sites More sharing options...
mrMarcus Posted October 14, 2009 Share Posted October 14, 2009 for starters, try indenting your code .. makes it excruciating trying to debug when you have close brackets all over the place, and everything starts at the same point. second, read my other post about using isset() .. you're (again), starting you script with: if (isset($_FILES['upload'])) { while there is no input file field with that name, therefore, absolutely no script will execute. using $_FILES is used on the image input, so $_FILES['img1'] would work, or $_POST['upload'] would work. and why you have error_reporting off is another story. take error_reporting(0); out the IF statement, and place it atop the script all by itself .. and change the (0) to (1); Link to comment https://forums.phpfreaks.com/topic/177664-solved-how-to-get-values-from-_post-with-onchangeformsubmit/#findComment-936878 Share on other sites More sharing options...
StefanRSA Posted October 14, 2009 Author Share Posted October 14, 2009 GREAT! Sorry it took so much effort MrMark! It has solved my problem! THANKS AGAIN!!! Link to comment https://forums.phpfreaks.com/topic/177664-solved-how-to-get-values-from-_post-with-onchangeformsubmit/#findComment-936886 Share on other sites More sharing options...
mrMarcus Posted October 14, 2009 Share Posted October 14, 2009 no worries .. glad to hear it's working. now, go brush up on some of the simple things like isset() and Superglobals Link to comment https://forums.phpfreaks.com/topic/177664-solved-how-to-get-values-from-_post-with-onchangeformsubmit/#findComment-936889 Share on other sites More sharing options...
StefanRSA Posted October 14, 2009 Author Share Posted October 14, 2009 I have to brush up on PHP! I am still a newbie! lol! Link to comment https://forums.phpfreaks.com/topic/177664-solved-how-to-get-values-from-_post-with-onchangeformsubmit/#findComment-936893 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.