"grab" Post form response with php (response is a txt file) - php

I have a form that posts a file to a site that I don't have control and that site responds with a dynamically generated text file based on the uploaded file. That response is downloaded by the client after the form is submitted. My question is: how can I grab this text file and copy it to my server before it gets to the user?
Possible solution 1: is there a way to change this file's headers and put its content to some hidden div or iframe?
Possible solution 2: I think the best way is that I create a script that gets the form data and then reposts it to the external site and then gets the response and writes this response to my site in a txt file. Unfortunately all my attempts to get this working have failed.
Example of solution 2:
<form action="mysite.com/respostScriptThatGrabsResponse.php" method="post">
<input type="file" name="filename"/>
<input type="submit" value="submit"/>
</from>

Theory #2 would be the way to go. You could use cURL to post the data to the external site. The most likely obstacle preventing success would be the external site requiring cookie data that only the client would have.

Related

File uploading should not stop after leaving the page

I am working on application that require file uploads, the files can be from 10-150 MB , it is working fine.
But once the file transfer is initiated and in between user leaves the pages and comes back to that pages, we have to start the uploading again.
So is there any way through which file transfer should not stop even in case we leave the page.
I am using, jQuery , bootstrap and PHP for the application.
Nope. You can't send an upload request and leave the page peforming that request. Its the opposite of a download.
Most sites use a call to "window.open" on a button titled "upload" to open new window where the user can select the file to upload. The page in this window would be solely used for the upload process. When this page is submitted with ajax, a spinner of sorts would appear until that upload request is complete.
On the other hand (avoiding window.open), I assume you're using a form, so you could start the upload on another tab or something with target="_blank" on your form, but it may be a challenge to give information on what is occurring (the upload) in this new tab...
<form target="_blank" action="upload.php">
<input type="file" name="upload1" ></input>
<input type="submit" name="" value="">
</form>

Input directly to the same page

I'm trying to find a way to input an image directly onto the same page, but I can't figure it out.
The image doesn't need to be saved when navigating away from the page.
I've tried:
<form action="#" method="post">
but I still can't figure out how to actually put it where I want it.
This might be really simple and I'm just overthinking it, but I've been googling for hours with no result.
You need to leave empty form action and specify enctype
Example:
<form action="" method="POST" enctype="multipart/form-data">
<input type="file">
<input type="submit">
</form>
This will push image into $_FILES array in php. For uploading images you need to view http://www.w3schools.com/php/php_file_upload.asp
If I understand you correctly, basically what you are trying to do is impossible. (without ajax)
The basic way a web-site works is:
browser asks for a page
server sends back page.
If you're trying to change the page after it has been sent to the browser (i.e. add an image), then you need to do some work behind the scenes.
browser asks for a page
server sends back page with some javascript in it that allows fetching additional data from the server.
javascript asks for additional data from the server.
additional data gets added to the page.
Since javascript has no access to the local machine outside the browser sandbox you'll have to get the user/javascript to send the image to the server, and the server then has to send it back to your javascript (which can then embed it in your page)
jquery or similar is probably the way to go.
You'll need a page/script on your server to handle the upload and serving of the image file.
Alternative
A simpler method might be to have a file input field on a form in your initial page with no image, when the page is submitted, check the $_FILES variable (see move_uploaded_file) and now serve the same page this time with the uploaded file displayed. You could then delete the image file from your server if you need it only once.

Same file value in 2 file inputs form

I'm trying to populate a second file input with the selected file from the first.
I've looked into this and, for security reasons, it's not possible to get the full path from one file input - only the file name. This isn't of any help as the other file input won't be able to upload as it won't be able to find the file.
Example:
<input type="file" id="1" /> - User browse the file by clicking on this input
<input type="file" id="2" /> - This input gets the value from the other file input
The reason I want to do this is because I am building a web app in Wordpress that allows users to upload a photo to Facebook but I also what the same photo to be attached to the wp post. I don't want them to have to select the file for each input.
Is there any way to achieve this using PHP and jQuery?
Due to security restrictions, you cannot programatically set the value of file input type fields, it is not possible.
Also, I don't see why you need to upload the same file twice? If you wish to save the same file in multiple locations, you can copy the first file into the second location too, isnt it ?
Please rephrase the question explaining why you want to upload the same file twice so a better answer can be given.
It sounds like you are approaching this the wrong way. Modern browsers prevent you manipulating the value of file inputs for security reasons. What you are suggesting is not possible.
You should be uploading the file to one single location and the sending it to Facebook/WordPress once it is uploaded. If the method of submitting the photo to either must be via POST, then you can use cURL to send that file.
Here is an example: Send a file via POST with cURL

Submit information to url, but also open PDF

EDIT: This privately published page is what I need to work with. Password: stackOF
My client desires is to have her Wordpress blog show a MailChimp form on her home page as a gateway to a .pdf. I need the following behavior to occur when the user clicks "Submit":
execute the included MailChimp's javascript file; this ensures the form was properly filled, and then performs the sign-up to the newsletter list (don't need help with this part)
then show the user an informational PDF for download or viewing
EDIT: The logical order was flipped from when I originally posted this. The script should execute, and only if the script gets executed properly should the PDF show to the user
Note:
My experience level with HTML and PHP is 3/4, and with JS I am 2/4 EDIT: (seems more like 1/4 at this point lol). If my research is correct, PHP (server-side language) would be used to do that which the client wants.
Additional validation is not necessary beyond what MailChimp's script provides (it ensures that user has submitted a completed form) in this case (the client says it's ok if the e-mail isn't valid at all).
The .pdf URL and content is static, and simply needs to be shown, not generated.
----RESEARCH----
I know that the Mailchimp form uses the following line to actually submit the information, but I want to do the action mentioned below, as well as open the aforementioned .pdf:
<form action="http://*BLAH*.us2.list-manage.com/subscribe/post?u=*BLAHBLAH*&id=*BLAHBLAHBLAH*" method="post" id="mc-embedded-subscribe-form" name="mc-embedded-subscribe-form" class="validate" target="_blank">
I am reading on other sites that I can conceivably point "action" to a .php file, but if there is a way to do this with javascript - since its using the .js file that I created for that already anyways, then I would be most happy. Barring that, I'll take what I can get..
You can try the following:
add an onsubmit handler on the mailchimp form like below:
<form onsubmit="runMyStuff(this)" action="http://*BLAH*.us2.list-manage.com/subscribe/post?u=*BLAHBLAH*&id=*BLAHBLAHBLAH*" method="post" id="mc-embedded-subscribe-form" name="mc-embedded-subscribe-form" class="validate" target="_blank">
Your javascript function:
function runMyStuff( o ) {
// open the pdf file in a separate window
window.open( PATH_TO_YOUR_PDF_URL );
// now, submit the form
o.submit();
}
Hope this helps.
You might possibly create a page that includes your script and have an iframe that points to the URL of the PDF.

upload and process user selected file in html, javascript and php

I want to have a user select a file and then have php put the contents in db. Now the last part (processing the file in php) is easy. But is there a way I can process a user selected file whithout a new page load?
If I use the following:
<FORM ACTION="upload.php" METHOD="post" ENCTYPE="multipart/form-data">
<INPUT TYPE="file" NAME="somefile"><BR />
<INPUT TYPE="submit" NAME="submit" VALUE="Upload">
</FORM>
Page upload.php automaticaly loads after which I can insert the uploaded file in a database.
I would like to use a combination of javascript, php and xajax to process the file. I don't think something like this is possible:
<FORM ACTION="javascript:xajax_proces_file()" METHOD="post" ENCTYPE="multipart/form-data">
<INPUT TYPE="file" NAME="somefile"><BR />
<INPUT TYPE="submit" NAME="submit" VALUE="Upload">
</FORM>
Because the file is not uploaded when function xajax_process_file() is called. Or is it? I think I do not fully grasp the principle of uploads with javascript, html and php.
Any help and or clarification is much appreciated.
It may help to think of this as a two step process.
First, the user fills in the form and submits it - step one.
Second ( which is the default action ) the specified target file takes the input from the form and uses it to do whatever. You can almost think of a form "action" as a link - the default action of a link click is to display the result of the link. The same goes for a form action - display the result of a form action.
Now, it's possible via JavaScript to disable the default action of an element for a particular event. It is also possible via JavaScript to access a browsers HTTP mechanism to send/receive HTTP request (which is what every page request is - whether from your URL bar or a page link or a Google search result).
And that is what AJAX in simple terms is - using JavaScript to use a browsers HTTP mechanism to send requests to a web server and possible receive a response back without the use of a traditional click event. You then combine this with the use of JavaScript to "turn off" default actions and instead follow the action specified by you to get information from a server and add it to the page without ever having to refresh the page.
Many times to prevent the defualt action from taking place for a certain element, you return false in your code. The same goes for your form. Using javascript:
form.onSubmit = function() {
blah blah blah.....Use ajax to send the information to the form handler
return false; //Prevents the defualt action of the submit event
}
If you are really new to AJAX, I suggest you check out this tutorial and then this one. Lastly, I would recommend using a Javascript framework like jQuery to help you - it is awesome and does alot of great stuff, but also has easy and built in functionality for AJAX.
Here is another tutorial to do a form submit with no page refresh (uses jquery).
an alternative is to make the form directs the action to an iframe, after processing the query in the iframe, proceed by JS to clear the form of the father

Categories