Upload a file, but not inside a form - php

Upload a file, but not inside a form
For example :
<input type="file" name="taskuploadfile" />
<input type="button" name="taskupload" value="Task Upload" onclick="taskupload()" />
Using Ajax for javascript to php.
Can I get temp path of file in php or Is it possible ?

Use a library that can do it the best way that you can not reach even after a month of coding.
One of the best examples is JqUploader.
Here is the example: http://pixeline.be/experiments/jqUploader/test.php

You can use the File API to read the data and then send it via XHR as per an example on MDN or by using XHR2 as per HTML 5 Rocks' example.
These methods do have limited browser support though, so you are probably still better off using a real form and submitting to an iframe for the time being.

Since it looks like you are using dojo (from the tags you put in your post)... why not using the HTML5 multi-file upload widget ? It has a plugin to do ajax uploads...
More on that here : http://dojotoolkit.org/documentation/tutorials/1.6/uploader/
Reference documentation here : http://dojotoolkit.org/reference-guide/dojox/form/Uploader.html

It is not possible to do a file upload with ajax. The only trick is to use an IFRAME. And since you want to POST something to the server, you have to use a form tag.

Related

How do I make an html file include itself without refreshing the page each time a button or link is clicked?

Beginner's question here :
file #1 : file1.html (one dataset)
//header, body and some other stuff
<form action="some_logic.php">
<input type="text" name="data1" placeholder="data1">
<input type="text" name="data2" placeholder="data2">
<input type="submit" value="Ok!">
</form>
//end of the html
file #2 : (I don't even know what this file should be : php? html? js?)
//usual stuff
include 'file1.html'; //OR
<!--#include file="file1.html"-->
#logic to replicate file1.html with a button
I need to have the file1's code repeated once each time the user clicks on a button (or a link or whatever) and I would like this to work without refreshing the page (or at least without questionning the server over and over and without deleting the current datas). The server uses php7.
I need this because I do not know in advance how much datasets the user will create.
It would be awesome if this could be done with javascript! (since I doubt this can be achieved with php/html only) Although I have never used js until now.
You can fetch data from server without refreshing the webpage using JavaScript Ajax. Here you can create an event listener to send a request to the server. The data can be plain text, html, json, xml or whatever you want. Just create Ajax request for events. Then update the webpage using the response data.

Retrieving text from HTML5 to write to a file with PHP?

I was wondering if there was a way to retrieve info from a certain element in HTML and then write it to a file using PHP ?
For example:
<div id="info_needed">text needed</div>
<button id="submit info"> Click to write to PHP file</button>
How would I retrieve the "text needed" and write it to a file using php? (I understand I will have to use the Write() command, but I am completely lost on how to get the info.
Any help would be awesome.
Thanks guys!
Use jQuery to pull the text out. $('#id').text()
Use jQuery.ajax() to post to a PHP file that does the processing. i.e. capture the posted text.
You can use jQuery.post(). Make sure you set data parameter. {value: sometext} Your PHP script can then use file_put_contents() in append mode to append the text to the file.
P.S.: You could have a success function that indicates the action was successful.

Upload an image with PHP using JQUERY Object

I have an image (a captcha specifically) that I want to upload to my server using PHP (or if possible and better, only JQUERY)
I created an object with this code:
var newIMG = $("img[name='captchaImage']").clone();
But I cannot send it via jQuery's ajax method or else. (Even tried to manually add but no idea how to send an object as file)
What should be the simplest PHP / jQuery code to send this object?
Thanks in advance,
You can use jsupload plugin for this purpose.
One cannot upload files using AJAX..
You can also use an iframe to simulate uploading the files..
only jquery file upload is not possible you need php
and you can use the jQuery File Upload to upload file via ajax and jquery

how to upload file php through jquery and smarty

i need to upload a file doc or pdf or rich text through jquery and smarty
i try to pass the file in jquery is given below
window.location.href= sitePath+'download?p='+$('#file').val();
but the p doesn't have value. how to get the file path or name and how can stored in server
in controller i write just pass the query to model is given below
$model=new download();
$id=$model->upload($param);
and i can't develop the model code....
please help me
You can't send files like that.
You should either submit a form with enctype="multipart/form-data" or use some AJAX uploader (which uses form submit to iframe and then stores a file using PHP script).
EDIT: Some references
http://www.w3schools.com/php/php_file_upload.asp - tutorial on how to upload a file using PHP
http://valums.com/ajax-upload/ - example of ajax uploader.
EDIT2: The problem is not with your model code but with the way you try to submit a file.

How to upload and read text/csv file without submitting?

I have this form and I would like to read the uploaded file and then fill out the form using this read information without refreshing the page.
For example the first word might be "Bob" and so I would want that to go in my input text "First_name." I've been trying to searching online for a way to do this using JQuery or Ajax but I can't seem to find a solution.
Can this be done using the two methods previously mentioned? If so and if not can someone point me to a link or to where I can learn how to do this? The instances I have found include where one uses JQuery to upload the file and display the size without refresh (which is not exactly what I want).
I have also found how one can use an iFrame but this again is not what I want. I suppose I could always just submit the part of the page containing the textfile related information and show the same form but with the filled out information. But I feel as if this is kind of sloppy and I want to know if there is a better way.
Thanks.
Firefox has a method to do this, the File and FileList API provide a way to get at the files selected by a file input element and have a text retrieval method.
A very basic example:
NB. Not all browsers support this code.
[I think Chrome, Firefox and Opera do at time of writing.]
HTML:
<form>
<input type="file" name="thefile" id="thefile" />
</form>
<div id="text"></div>
JS (using jQuery):
$(document).ready(function() {
$('#thefile').change(function(e) {
if (e.target.files != undefined) {
var reader = new FileReader();
reader.onload = function(e) {
$('#text').text(e.target.result);
};
reader.readAsText(e.target.files.item(0));
}
return false;
});
});
Demo: http://jsfiddle.net/FSc8y/2/
If the selected file was a CSV file, you could then process it directly in javascript.
.split() will be useful in that case to split lines and then fields.
the only way I know would be to submit the form to a hidden iframe. this will upload teh file without refreshing the page. you can then use any returned info using javascript. this is what they use for fake ajax style image uploads that let you preview an image before uploading. the truth is it already has been uploaded via a hidden iframe. unfortunately however iframes are not xhtml 1.0 compliant.
something like this article may help:
http://djpate.com/2009/05/24/form-submit-via-hidden-iframe-aka-fake-ajax/
The question you might ask is :
why should I use this method instead of real ajax ?
Well they’re is numereous answer to that but one good reason it that
is doesnt require any type of ajax libs and you can start using it
even if you never used ajax before.
So here it goes.
<form method=”post” action=”formProcess.php” target=”hiddenIFrame”>
<input type=”text” name=”test” /> </form>
<iframe style=”width:0px;height:0px;border:0px;” name=hiddenIFrame />
This is just a normal form but you’ll notice the target in the form
tag, this tells the form to submit in the iframe instead of the
current page.
It’s works exactly as the target attribut on the A tag.
Also the iframe is hidden from the user using
style=”width:0px;height:0px;border:0px;”
now the file formProcess.php is not different from your normal form
processing file but if you want do something on the main page you have
to use JS like that :
window.parent.whatEverYouWannaDoInParentForm();
You can also upload file with this method !
Please checkout the formphp for full example.
Cheers !
Nb : You will see the status bar acts like the page is reloading but
it’s really not.

Categories