Reputation: 159
I have a file upload form that I want to be submited automatically when the file has been selected. I am using Javascript onchange event to do it.
<form action="form.php" method="post" enctype="multipart/form-data">
<input type="file" name="filename" onchange="javascript:this.form.submit();">
</form>
This code seems to work as it is, but I have a PHP if statement that processes the file uploaded when a $_POST['submit']
happens (using the the submit button)
<?php
if(isset($_POST['submit'])){
$photo = Photograph::find_by_userid($user_id);
$photo = new Photograph();
$photo->attach_file($_FILES['photo_upload']);
}
?>
But since the point of this is to remove the submit button <input type="submit" name="submit"/>
the PHP if code is not being triggered, ergo the file is not being uploaded. Do you know how can I make the if statement work when not using a submit button and using an auto submit with Javascript?
Thanks in advance!
This is the whole code..
<?php require_once("../includes/initialize.php"); ?>
<?php include_layout_template("header.php"); ?>
<?php include_layout_template("edit_header.php"); ?>
<?php
if(!$session->is_logged_in()){
redirect_to("login.php");
}else{
}
$id=$session->user_id;
$user=User::find_by_id($id);
$user_id = $user->id;
if(isset($_POST['submit'])){
$photo = Photograph::find_by_userid($user_id);
if($photo){
$photo->destroy();
$photo = new Photograph();
$photo->user_id = $user_id;
$photo->attach_file($_FILES['photo_upload']);
if($photo->save()){
redirect_to("edit_photo.php");
$session->message = "Photo uploaded succesfully";
} else{
$message = join("<br/>", $photo->errors);
}
}
$photo = new Photograph();
$photo->user_id = $user_id;
$photo->attach_file($_FILES['photo_upload']);
if($photo->save()){
redirect_to("edit_photo.php");
$session->message = "Photo uploaded succesfully";
} else{
$message = join("<br/>", $photo->errors);
}
}
$photo = Photograph::find_by_userid($user_id);
?>
<section class="edit_section">
<h2>Upload/Change <br/> Your Photo</h2>
<p><?php echo form_errors($errors); ?></p>
<p class="messages"><?php echo $message ?></p>
<form class="edit_form" id="form" action="edit_photo.php" enctype="multipart/form-data" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="1048576" />
<ul>
<li>
<img src="<?php if(isset($photo)){echo $photo->image_path();} ?>" width="250px" />
<label for="photo_upload">Upload/Change <br/> your photo</label>
<input type="file" id="photo_upload" name="photo_upload" style="background:none; border:none;" value="upload" onchange="javascript:this.form.submit();"/>
<!--input type="submit" name="submit" value="Upload" /-->
</li>
</ul>
</form>
<section style="margin-left: 400px;" >
<p class="button"><a href="delete_photo.php?id=<?php if(isset($photo)) {echo $photo->id; }?>">Delete it</a></p>
</section>
</section>
<?php include_layout_template("footer.php"); ?>
Upvotes: 1
Views: 170
Reputation: 10371
One solution is to add a hidden field to the form, this field will be recognized by PHP, for example, let's call it "my_flag" :
<form action="form.php" method="post" enctype="multipart/form-data">
<input type="file" name="filename" onchange="javascript:this.form.submit();">
<input type="text" name="my_flag" hidden />
</form>
Now, on the PHP side, you do :
<?php
if ( isset( $_POST['my_flag'] ) )
echo "flag";
?>
The echo "flag";
is just for testing, you replace it by your code.
Upvotes: 2