upload files only after submit - php

I'm trying to use dropzonejs in order to upload multiple files.
I want to upload them only when i submit the form (post the page)
how can i do that?
<?PHP
if ($_POST)
print_r($_FILES);
?>
here is my form:
<form method='post' action='index.php' enctype='multipart/form-data'>
<input type='text' name='title' value="" class="form-control" />
<textarea name="msgText" class="form-control" ></textarea>
<label class="control-label col-sm-1">Attached File:</label>
<div class="dropzone"></div>
<input type="submit" value="Submit" class="btn btn-success form-control">
</form>

Add name attribute to submit imput
<input type="submit" value="Submit" name="button" class="btn btn-success form-control">
Then in PHP:
if(isset($_POST['button'])){
...some code...
}

Related

upload file when post the form using dropzonejs

If i have simple form, and i want to use dropzonejs for uploat multiple files, can i upload them when i post the form?
if i post the form below and print $_FILES i don't get any data...
I looked at other topics here but i got no answer to my question
<?PHP
if ($_POST)
print_r($_FILES);
?>
HTML
<form method='post' action='index.php' enctype='multipart/form-data'>
<input type='text' name='title' value="" class="form-control" />
<textarea name="msgText" class="form-control" ></textarea>
<label class="control-label col-sm-1">Attached File:</label>
<div class="dropzone"></div>
<input type="submit" value="Submit" class="btn btn-success form-control">
</form>

Here variable 'pass' can't parse to $_post['action']=='edit'

<form action="" method="post">
<div class="col-md-2 ">
<input type="type" value="<?php echo $row['rcode'];?>" name="pass" id="pass"/>
<input type="submit" name="edit" class="btn btn-primary" value="edit"/>
<input type="submit" name="delete" class="btn btn-danger" value="Delete"/>
</div>
</form>
then
if($_POST['action']=='edit'){
echo $rcode1=$_REQUEST['pass'];
$sqlup="select * from transport_details where rcode='$rcode1'";
There is no input named action. Your current code is unreachable.
The $_POST assoc gets its keys by attribute name. E.g.
<input type="text" name="authorName" />
<?php
echo $_POST['authorName'];
?>
Your code should be like (in PHP file)
if (isset($_POST['edit'])) { ... }
OR (in HTML file)
<form action="" method="post">
<div class="col-md-2 ">
<input type="type" value="<?php echo $row['rcode'];?>" name="pass" id="pass"/>
<input type="submit" name="action" class="btn btn-primary" value="edit"/>
<input type="submit" name="action" class="btn btn-danger" value="Delete"/>
</div>
</form>
You can do this in your php...
if (isset($_POST['edit'])) {
echo $rcode1=$_POST['pass'];
$sqlup="select * from transport_details where rcode='".$rcode1."'";
}
assuming your html is like this...
<form action="" method="post">
<div class="col-md-2 ">
<input type="type" value="<?php echo $row['rcode'];?>" name="pass" id="pass"/>
<input type="submit" name="action" class="btn btn-primary" value="edit"/>
<input type="submit" name="action" class="btn btn-danger" value="Delete"/>
</div>
</form>
you should be able to get the value

prestashop file upload won't work

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>

html submit button not handling action .php

I'm trying to call a php file in a html form, as shown in my code, but the send button is not functioning, any ideas ?
<form id="contact-form" method="post" action="form_send.php">
<fieldset>
<label class="name">
<input type="text" value="Your Name" onFocus="if(this.value=='Your Name'){this.value=''}" onBlur="if(this.value==''){this.value='Your Name'}">
</label>
<label class="phone">
<input type="text" value="Telephone" onFocus="if(this.value=='Telephone'){this.value=''}" onBlur="if(this.value==''){this.value='Telephone'}">
</label>
<label class="email">
<input type="email" value="Email" onFocus="if(this.value=='Email'){this.value=''}" onBlur="if(this.value==''){this.value='Email'}">
</label>
<label class="message">
<textarea onFocus="if(this.value=='Message'){this.value=''}" onBlur="if(this.value==''){this.value='Message'}">Message</textarea>
</label>
<div class="btns"> <a class="button" onclick="clearFunction()" >Clear</a> <a class="button" type="submit" name="submit" value="Send Form" >Send</a> </div>
</fieldset>
</form>
you are use <a> tag to submit the form
replace <a class="button" type="submit" name="submit" value="Send Form" >Send</a> with <input class="button" type="submit" name="submit" value="Send Form" >
You are not naming any inputs in your form..
<input **name='name'** value='Your name'>
<input **name='phone'** value='Phone'>
and so on..
for proccessing:
if(isset($_POST['submit'])){
$name=$_POST['name'];
$phone=$_POST['phone'];
$xxx=$_POST['xxx'];
echo $name, $phone; //echoing data.
}
and so on.
Don't forget to sanitize your data before submitting..(ANTI-SQL INJECTION)
and instead of your OnBlur and OnFocus solution: placeholder='Your name'

Doing PHP Submit and Fileupload from same button

I am writing a page where a user can fill out form data, select two files for upload, and then hit Submit to pass the form data into the PHP page, and also the two files will be uploaded.
The problem is the submit button seems to be able to only be either a 'submit' or a 'File_Upload'. It can't do both... or can it?
Here is the gyst of HTML file (Just enough to get the point across... I hope)
<form class='form' name="frm_new_session" method="POST" enctype="multipart/form-data">
<div class='frm_row'>
<label>Title</label>
<input id="titleF" type='text' name="title" size='50' />
<input type="hidden" name="_chkuser" value="1"/>
</div>
<div class='frm_row'>
<label>Description</label>
<textarea id="descF" rows='3' cols='53' name="desc"></textarea>
</div>
<div class='frm_row'>
<label>Image</label>
<input id="imageF" type="file" name="uploadedimage">
<input type="hidden" name=MAX_FILE_SIZE" value='50000000'/>
</input> <br />
</div>
<div class='frm_row'>
<label>Session Media</label>
<input type='radio' id="mediaF" name="media" />
Upload: <input type='file' name="fileupload">
</input> <br />
<input type='radio' id="mediaF" name="media" />
Enter media URL:
<input type='text' size='75' name="mediaFile"/>
</input>
</div>
<div class='frm_row'>
<input type="submit" value="Save Session"/>
<div class='btn' type="submit" style='float: left;'>
<a href='#' id='btn_save_session'><span>Save Session</span></a>
<div class='kill_clear'></div>
</div>
</form>
I know I can do this using multiple forms, but I would like to avoid that and enjoy having just a single 'Save' button.
Any suggestions?
i don't understand the problem, the info will be in the $_POST array and the files in the $_FILES array.
however, you are missing a double quote in MAX_FILE_SIZE, i tested the following code, and it works :D
<?php
print_r($_POST);
print_r($_FILES);
?>
<form class='form' name="frm_new_session" method="POST" enctype="multipart/form-data">
<div class='frm_row'>
<label>Title</label>
<input id="titleF" type='text' name="title" size='50' />
<input type="hidden" name="_chkuser" value="1"/>
</div>
<div class='frm_row'>
<label>Description</label>
<textarea id="descF" rows='3' cols='53' name="desc"></textarea>
</div>
<div class='frm_row'>
<label>Image</label>
<input id="imageF" type="file" name="uploadedimage">
<input type="hidden" name="MAX_FILE_SIZE" value='50000000'/>
</input> <br />
</div>
<div class='frm_row'>
<label>Session Media</label>
<input type='radio' id="mediaF" name="media" />
Upload: <input type='file' name="fileupload">
</input> <br />
<input type='radio' id="mediaF" name="media" />
Enter media URL:
<input type='text' size='75' name="mediaFile"/>
</input>
</div>
<div class='frm_row'>
<input type="submit" value="Save Session"/>
<div class='btn' type="submit" style='float: left;'>
<a href='#' id='btn_save_session'><span>Save Session</span></a>
<div class='kill_clear'></div>
</div>
</form>
<form action="/add-news.php" method="post" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="5242880" />
<input type="text" name="title" />
<input type="file" name="image" />
<textarea rows="40" cols="50" name="content"></textarea>
</form>
Try adding hidden field MAX_FILE_SIZE where value is max size of files in bytes.
It works for me.
What you mean by: "It can't do both", what are the indications?

Categories