prestashop file upload won't work - php

I am trying to implement file upload in Order Detail page.
Created form
<form action="" method="post" id="uploadForm" enctype="multipart/form-data">
  <label for="fileUpload">{l s='File to upload:'}</label>
  <input type="hidden" name="MAX_FILE_SIZE" value="2000000" />
  <input type="file" name="fileUpload" id="fileUpload" />
<div class="submit">
<input type="hidden" name="id_order" value="{$order->id|intval}" />
<input type="submit" class="unvisible" name="submitMessage" value="{l s='Send'}"/>
<button type="submit" name="submitMessage" class="button btn btn-default button-medium"><span>{l s='Send'}<i class="icon-chevron-right right"></i></span></button>
</div>
</form>
and added this to OrderDetailController.php
if (Tools::isSubmit('submitMessage')) {
$idOrder = (int)Tools::getValue('id_order');
$msgText = Tools::getValue('msgText');
if (isset($_FILES['fileUpload']['name']) && !empty($_FILES['fileUpload']['name']) && !empty($_FILES['fileUpload']['tmp_name']))
{
$this->errors[] = Tools::displayError('Works');
}
...
Now when i submit form - it saves message and completely ignores file.
Does anyone know why?

Solved myself. Answered here
It was ajax issue not sending $_FILES[] data

I'm assuming you placed this into order-detail.tpl of default template.
You put your file input into a separate form that doesn't do anything since it does not have a submit button. The comments form on that page is a completely different one that's why your input is ignored.
What you want to do is place your html code without <form> tags into a proper form that submits message. The form is at the bottom of order-detail.tpl.
<form action="{$link->getPageLink('order-detail', true)|escape:'html':'UTF-8'}" method="post" class="std" id="sendOrderMessage">
<h3 class="page-heading bottom-indent">{l s='Add a message'}</h3>
<p>{l s='If you would like to add a comment about your order, please write it in the field below.'}</p>
<p class="form-group">
<label for="id_product">{l s='Product'}</label>
<select name="id_product" class="form-control">
<option value="0">{l s='-- Choose --'}</option>
{foreach from=$products item=product name=products}
<option value="{$product.product_id}">{$product.product_name}</option>
{/foreach}
</select>
</p>
<p class="form-group">
<textarea class="form-control" cols="67" rows="3" name="msgText"></textarea>
</p>
<!-- Your html snippet -->
<p class="form-group">
<label for="fileUpload">{l s='File to upload:'}</label>
<input type="hidden" name="MAX_FILE_SIZE" value="2000000" />
<input type="file" name="fileUpload" id="fileUpload" />
</p>
<div class="submit">
<input type="hidden" name="id_order" value="{$order->id|intval}" />
<input type="submit" class="unvisible" name="submitMessage" value="{l s='Send'}"/>
<button type="submit" name="submitMessage" class="button btn btn-default button-medium"><span>{l s='Send'}<i class="icon-chevron-right right"></i></span></button>
</div>
</form>

Related

How to set selected file name in place of "No file Chosen" in form input type file using PHP [duplicate]

I have a html form to upload files. After uploading if user need to edit the details the form is shown again with the data values populated from the database. How could i possibly show previous values of the uploaded files in the input type=file.
To be more specific this is my form:
<form>
<div class="form-row">
<input id="equipmentname" type="text" value="<?php if(isset($equipment)){ echo $equipment[0]->equipmentname;} ?>" name="equipmentname" />
</div>
<div class="form-row">
<input type="file" id="userfile" name="userfile" size="20" onChange="getImage(event);" />
</div>
<div class="form-row action_btnContainer">
<input id="createEquipmentBtnCreate" name="file_upload" type="submit" value="Add" class="btnCreate" onclick="checkEquipmentCreateForm(event);" />
<input id="createEquipmentBtnCancel" type="button" value="Cancel" class="btnCancel" onclick="closeSitePopUp('EquipmentCreateForm');" />
</div>
</form>
Like i have echoed the 'equipmentname' in its textfield How could i possibly show the previous upload file value in its respective inputfield. Any suggestions are welcome. Thanks.
You can't pre-populate a file form field.
A possible solution is to have an extra field or bit of html to "show" the value.
For example, assuming your file upload is an image and you are storing the path in your database, add an image element to show the image.
<form>
<div class="form-row">
<input id="equipmentname" type="text" value="<?php if(isset($equipment)){ echo $equipment[0]->equipmentname;} ?>" name="equipmentname" />
</div>
<div class="form-row">
<input type="file" id="userfile" name="userfile" size="20" onChange="getImage(event);" />
</div>
<?php
if (strlen($equipment[0]->imagePath) > 0) {
?>
<img src="<?php $equipment[0]->imagePath; ?>" alt="image preview" />
<?php
}
?>
<div class="form-row action_btnContainer">
<input id="createEquipmentBtnCreate" name="file_upload" type="submit" value="Add" class="btnCreate" onclick="checkEquipmentCreateForm(event);" />
<input id="createEquipmentBtnCancel" type="button" value="Cancel" class="btnCancel" onclick="closeSitePopUp('EquipmentCreateForm');" />
</div>
</form>
If the upload was a doc file, then you could show a thumbnail image that represents a doc file, there are plenty about.

Spoofing different methods within same form laravel

So within a single form I want to be able to change the request type between PATCH and DELETE depending on which button has been selected. Here's my code (simplified for readability):
<form id="edit-task-form" method="POST" action="">
<input type="hidden" name="_method" value="PATCH">
#csrf
<div>
<input type="text" id="modal-task-title" name="task_title" placeholder="Title"/>
</div>
<div>
<textarea name="task_desc" id="modal-task-desc" placeholder="Task description." rows="5"></textarea>
</div>
<div class="input flex-center-parent">
<input type="button" value="Delete" id="delete-task"/>
<input type="submit" value="Save" id="save-task"/>
</div>
</form>
I'm thinking I could just change the value onclick() with javascript but wondering if anyone knows a more laravel-like way for doing this?
You could use HTML buttons instead of inputs and set the name attribute of the buttons to _method allowing you to have form values assigned to them.
<div class="input flex-center-parent">
<button type="submit" name"_method" value="DELETE" id="delete-task">Delete</button>
<button type="submit" name"_method" value="PATCH" id="save-task">Save</button>
</div>

Echo the value inside html input type=file

I have a html form to upload files. After uploading if user need to edit the details the form is shown again with the data values populated from the database. How could i possibly show previous values of the uploaded files in the input type=file.
To be more specific this is my form:
<form>
<div class="form-row">
<input id="equipmentname" type="text" value="<?php if(isset($equipment)){ echo $equipment[0]->equipmentname;} ?>" name="equipmentname" />
</div>
<div class="form-row">
<input type="file" id="userfile" name="userfile" size="20" onChange="getImage(event);" />
</div>
<div class="form-row action_btnContainer">
<input id="createEquipmentBtnCreate" name="file_upload" type="submit" value="Add" class="btnCreate" onclick="checkEquipmentCreateForm(event);" />
<input id="createEquipmentBtnCancel" type="button" value="Cancel" class="btnCancel" onclick="closeSitePopUp('EquipmentCreateForm');" />
</div>
</form>
Like i have echoed the 'equipmentname' in its textfield How could i possibly show the previous upload file value in its respective inputfield. Any suggestions are welcome. Thanks.
You can't pre-populate a file form field.
A possible solution is to have an extra field or bit of html to "show" the value.
For example, assuming your file upload is an image and you are storing the path in your database, add an image element to show the image.
<form>
<div class="form-row">
<input id="equipmentname" type="text" value="<?php if(isset($equipment)){ echo $equipment[0]->equipmentname;} ?>" name="equipmentname" />
</div>
<div class="form-row">
<input type="file" id="userfile" name="userfile" size="20" onChange="getImage(event);" />
</div>
<?php
if (strlen($equipment[0]->imagePath) > 0) {
?>
<img src="<?php $equipment[0]->imagePath; ?>" alt="image preview" />
<?php
}
?>
<div class="form-row action_btnContainer">
<input id="createEquipmentBtnCreate" name="file_upload" type="submit" value="Add" class="btnCreate" onclick="checkEquipmentCreateForm(event);" />
<input id="createEquipmentBtnCancel" type="button" value="Cancel" class="btnCancel" onclick="closeSitePopUp('EquipmentCreateForm');" />
</div>
</form>
If the upload was a doc file, then you could show a thumbnail image that represents a doc file, there are plenty about.

Why does my form with empty upload form take so long? What could be the reason?

I have a form with a simple file upload input like:
<div class="contenttitle radiusbottom0">
<h2 class="finfo"><span>Uploader</span></h2>
</div>
<p>
Chose your file: <input type="file" name="uploaded_file" />
</p>
the rest as well, is a very simple form. Now when I execute (submit) this form it takes about 30 seconds to process it. Even when I don't choose any file to upload.
When I remove the file input from the form then it takes 1 second max. I add it back and it takes a lot of time again. The action page does not matter. What could be the problem?
here is the complete form:
<div class="boxwhite">
<form class="stdform" enctype="multipart/form-data" method="post" action="">
<div class="two_third">
<p>
<label>Header</label>
<input type="text" name="title" id="firstname" class="longinput" value=""/>
</p>
</div>
<div class="one_third">
<p>
<label>Status</label>
<select name="status">
<option value="1" <?= $content[status]==1?'selected':'' ?> >Active</option>
<option value="0" <?= $content[status]==0?'selected':'' ?> >Pasive</option>
</select>
</p>
</div>
<br clear="all" />
<input type='hidden' name='parent_id' value='3' />
<div class="contenttitle radiusbottom0">
<h2 class="finfo"><span>Uploader</span></h2>
</div>
<p>
Chose your file: <input type="file" name="uploaded_file" />
</p>
<BR />
<input type="submit" name="submit" value="Add" class="" />
<button class="cancel radius2">Cancel</button>
</form>
</div>
(this files are on a windows server)
I have moved the work to a linux server. Everything works fine now.

JQuery AJAX equivalent of form's submit file

I have a form, like this
<form id="picuploadform" enctype="multipart/form-data" method="post" action="">
<div class="row">
<label for="fileToUpload">Select a File to Upload</label><br />
<input type="file" name="fileToUpload" id="fileToUpload" />
<input type="hidden" id = "picssn" name="picssn" value="qweweqwq">
</div>
<div class="row" style="width:150px;">
<input type="submit" value="Upload" />
</div>
</form>
Which when I click the submit button I have PHP on this page to process it just fine. $_FILES for receiving file and $_POST for reading picssn that came with the hidden input. But due to problem with Jquery mobile I can't use submit or .submit() now. So I want to use
$.post('myphpfile.php', {picssn:$('#picssn').value(), file: $('#fileToUpload').xxxxxxxxxxxxxxxxx });
instead. But I have no idea how to grab the file out of that form and send it this way. Is there some method that can replace xxxxxxxxxxxxxx ? Thanks!
On your form element, you need to disable the ajax submit, and then fix your other code.
Try this:
<form id="picuploadform" enctype="multipart/form-data" method="post" action="#" data-ajax="false">
<div class="row">
<label for="fileToUpload">Select a File to Upload</label><br />
<input type="file" name="fileToUpload" id="fileToUpload" />
<input type="hidden" id = "picssn" name="picssn" value="qweweqwq">
</div>
<div class="row" style="width:150px;">
<input type="submit" value="Upload" />
</div>
</form>
You cant upload directly via ajax. Check this perhaps:
http://blueimp.github.com/jQuery-File-Upload/ or uploadify.com

Categories