Php: $_POST is not set after form is sent to iframe - php

I have just spent 4 hours researching and nothing has fixed my problem, so here I am. I am trying to design my own little chunk file uploader, and all is working quite well.
I have a main upload page that lets you set a file to upload. It then automatically cuts the first chunk out of the bytes of the file, and puts it into a form, along with some other bits of information:
<form id="hiddenform" name="hiddenform" action="SecretChunkUploader.php" target="iframe" enctype="multipart/form-data" method="post">
<hidden id="Bytes" name="Bytes" value="" />
<hidden id="Pass" name="Pass" value="<?php echo $_POST['Pass'];?>" />
<hidden id="FileName" name="FileName" value="" />
<hidden id="PackageNumber" name="PackageNumber" value="" />
</form>
Every <hidden> has its value correctly sent when the form is submitted through this javascript command:
document.forms["hiddenform"].submit();
The form is submitted to an iframe:
<iframe id="iframe" name="iframe" onload="" style="display:block"></iframe>
When submitted, the iframe navigates to the page specified in the form's action attribute.
Everything works well, except for when the form is received. The page loads, but there is no post data, and the variables for post are not set.
Here is the code for SecretChunkUploader.php:
<?php
echo "Password: ".$_POST["Pass"]."<br/>";
echo "FileName: ".$_POST["FileName"]."<br/>";
echo "PackageNumber: ".$_POST["PackageNumber"]."<br/>";
echo "Bytes: ".$_POST["Bytes"];
?>
The loaded page from SecretChunkUploader.php looks like:
Password:
FileName:
PackageNumber:
Bytes:
I have tried testing isset() and it returned false for all of the post variables.
What on Earth am I doing wrong? I have tested and know that the form is fully working, it just doesn't pass the values onwards.
Thanks in advance for any help!

Instead of:
<hidden id="Bytes" name="Bytes" value="" />
Try:
<input type="hidden" id="Bytes" name="Bytes" value="" />
Same for all the others, of course.

Related

Trying to pass three parameters from one php file to another

I am trying to pass three parameters from one php file to another. Two of those parameters are in variables that are already determined long before the button is clicked to call the second php file, but one will be taken from a text box at the time the button is clicked.
So far I have the following (snippet) in the first php file. The two parameters that are in the existing variables show up in the URL just fine, but I can't figure out how to get the student number to be included. The URL just has "studentNumber=?&club=..."
Thanks!
<input type="text" id="studentNum" placeholder="Student Number">
<input type="button" value="Add Student" onclick="window.location = '<?php $url = 'http://npapps.peelschools.org/editor/add.php?studentNumber='.$_GET["StudentNum"].'&club='.$club.'&type='.$type.''; echo $url;?>'" />
Is it really necessary to use window.location? I would encourage you to use something like this
function doSubmit() {
document.getElementById("myformid").submit();
}
<form id="myformid" action="receivingPHP.php" method="POST">
<input id="studentnr" type="text" value="42" />
<button onclick="doSubmit()">Send</button>
</form>
Of course there is no receivingPHP.php file on the StackOverflow servers, so if you try this script you will reach a white page (close it in the top right corner where it says close)
If you use $_GET["StudentNum"], it must come from an HTML-form or a html-link:
example
or
<form method="GET"><input name="StudentNum" value="1337"></form>
Good luck
The URL of your current page needs to have had studentNum present as a query parameter to be able to use $_GET. For example, if current page URL =
http://npapps.peelschools.org/myotherpage.php?studentNum=100
then you can $_GET["studentNum"]. Also, if you are accessing this URL via ajax
http://npapps.peelschools.org/myotherpage.php
then it must be passed as a data parameter.
Find out what the URL of the page is where you have the HTML that you have shown, and if studentNum has not been passed as a query parameter or data parameter from however you get there (e.g. an anchor tag href) then add that parameter to the URL.
Ended up reworking it so that all the information was sent in a form rather than trying to embed it in a button. The secret came from w3schools where I figured out how to hide the known parameters in a hidden input element in the form, as follows:
<form action="add.php" method="GET">
<input name="studentNo" type="text" placeholder="Student Number" />
<input name="club" type="hidden" value="<?php echo htmlspecialchars($club); ?>" />
<input name="type" type="hidden" value="<?php echo htmlspecialchars($type); ?>" />
<input type="submit" value="Add Student" />
</form>

Facebook iframe - how to send form (POST method)?

I have very simple form (the file is called message.php):
<?php
print_r($_POST);
?>
<form method="post" target="_top" action="<?php echo CANVAS_URL;?>message.php">
<input type="text" name="your_name" />
<input type="hidden" name="signed_request" value="<?php echo $_REQUEST['signed_request'];?>" />
<input type="submit" name="send" />
</form>
I found one solution of this issue - put into the form hidden input with the signed_request - I did it but unfortunately I am still facing with this problem -- I cannot retrieve sent POST data.
If I change the method to method="get", everything is working well, but I would need to data from POST.
Could anyone help me, how to solve this problem? Thanks!
Try this. I don't believe you need to use target in FB canvas aps anymore. Also a form ID would be good.
<form method="POST" id="my_form" action="message.php">
<input type="text" name="your_name" />
<input type="hidden" value="<?php print $_POST["signed_request"] ?>" name="signed_request" />
<input type="submit" name="submit" />
</form>
POSTing to Canvas URLs (as in http://apps.facebook.com/namespace) is simply not supported.
But why post to the top window instead of simply staying within the iframe? It's way better as it doesn't require the entire page to be reloaded, only the iframe.

Poco HTML Form POST data coming through empty

I have a desktop application passing POST data (text and an image file) onto a PHP file on an Apache Webserver. For some reason the POST data is coming through empty. Does anyone have any idea what could be causing this?
All I am trying for now is to display the POST data using:
<?php print_r($_POST); ?>
But I'm getting nothing. No errors and no data. The strange thing is this works perfectly on my old server - but with my new host the $_POST var is coming through empty so I'm confident it isn't an issue with the app.
The desktop app was built by someone else using Poco to send the HTML Form data so I don't know too much about it but I know if definitely works as it is currently working on my current other webserver.
Weirder still, when I don't attach an image everything works as expected. As soon as an image is attached (<100kb) I get no data whatsoever.
The Poco HTML Form is using multipart/form-data as the encoding type when an file is attached and application/x-www-form-urlencoded when there is no attachment (default).
I'm stumped here and wondering if anyone knows what could be going on or at least what I should be looking to try at?
Thanks in advance.
Edit: I also forgot to mention that it works if I submit it from another webpage without issue.
Double edit: Here is the equivalent of the form in HTML:
<form enctype="multipart/form-data" action="http://DOMAIN.COM/savedata.php" method="POST">
name: <input type="text" name="name" /><br />
email: <input type="text" name="email" /><br />
gender: <select name="gender"><option value="0">Female</option><option value="1">Male</option></select><br />
photo: <input name="photo" type="file" /><br />
<input type="submit" value="Upload File" /></form>

i get error when i try to upload a file?

I keep getting an error:Notice: Undefined index: on line 35
line 35:
$handle = new Upload($_FILES['my_field']);
this is my input field
<input type="file" size="32" name="my_field" value="" />
I do not understand this error, thanks!!!
EDIT:
<form name="upload" id="upload" enctype="multipart/form-data" method="post" action="actions/upload.php" />
<p><input type="file" size="32" name="my_field" value="" /></p>
<p class="button"><input type="hidden" name="action" value="image" />
<br>
<input style="margin-left:224px;" type="submit" name="submit" value="upload" />
Update: The OP is doing an Ajax request - well that obviously can't work with a File upload.
Old answer:
I think I found it.
Look closely at this tag:
<form name="upload" id="upload" enctype="multipart/form-data" method="post"
action="actions/upload.php" />
the closing /> closes the form. Everything that comes afterwards, is not inside that form - it's inside a new one that the browser probably generates to deal with the broken markup. That new form is not enctype=multipart/form-data.
Did you use enctype="multipart/form-data" on the form element?
This seems to be the only reason that the the key my_field isn't set on $_FILES.
Edit: If your file is bigger than post_max_size, you also get an empty $_FILES array.
See also: Apache/PHP: $_FILES Array mysteriously empty
In order to upload WITH an ajax submission you can use an IFRAME with the upload part in there.
Send ajax form submission
Using javascript trigger submit() on the upload form in the IFRAME
Have the returning page in the iframe trigger a javascript complete response function in the main page
A bit complicated but would work. You would need some mechanism to tie them together, for instance if you are submiting info about the image, have step 1 return the id of the database row where that info is stored so that the IFRAME upload form can submit that id so it knows where to store the picture.

JavaScript injected form field does not appear in POST data

I have a form that generates new input fields via JavaScript on click.
the inputs are successfully added to the FORM with the desired naming convention.
<input type="text" name="util_name0" id="util_name0" value="" /><br/>
<input type="button" onClick="newUtil(this)" value="Add New" />
newUtil() adds:
<input type="text" name="util_name1" id="util_name1" value="" />
however after posting, print_r($_POST) only lists 'util_name0'.
Normally i'd paste some code, but that's all i really need to do at this point...
form is in an include called from parent.php.
Javascript is called in parent.php
JS:
function newUtil(el) {
var newval = util_count++;
$('#qty').attr('value', newval);
$(el).before('-------------<br />
<div class="newUtilField">
<label for="util_type'+newval+'">Type (i.e. gas, electric...) '+newval+'</label><br />
<input type="text" name="util_type'+newval+'" id="util_type'+newval+'" value="" /><br /><br />
<label for="util_name'+newval+'">Company Name</label><br />
<input type="text" name="util_name'+newval+'" id="util_name'+newval+'" value="" /><br /><br />
<label for="util_number'+newval+'">Company Number</label><br />
<input type="text" name="util_number'+newval+'" id="util_number'+newval+'" value="" />
</div><br /><br />');
}
After execute the js code that adding the new field, inspect the new element using Firebug (in Firefox) or Web Inspector (in safari and google chrome). If the new field is outside the form tag, then it will not be included to form submit.
If you can, please provide the structure of form and it's fields. Also, make sure that the page contain no other error, and the tag is well balanced, all open tag have been closed in the right place. Misplace closing tag might yield error and unexpected behaviour.
Is this behaving differently in IE than it is in Firefox? Run an HTTP sniffer (like Fiddler) on it check the HTTP post headers carefully.
Would recommend creating proper objects rather than creating the elements like that.
Create elements and inject them one by one.
EDIT : check link http://domscripting.com/blog/display/99

Categories