Jump to content

[SOLVED] how to get values from $_Post with onchange="form.submit();"?


StefanRSA

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.