johnmerlino Posted August 6, 2011 Share Posted August 6, 2011 Hey all, I have a situation where there's multiple forms on a single page and when the user clicks the button, I don't want them to be navigated away from page but rather the transaction occurs on same page. Hence, everything occurs in a file called file_generator.php. I have these two forms: <form action="" method="post" enctype="multipart/form-data"> <tr> <td>Upload the data</td> <td><input type="file" name="file" /> </td> </tr> <tr> <td><input type="submit" name="upload" value="Generate List" /></td> </tr> </form> <form action="" method="post"> <tr> <td>Add Content:</td> <td><textarea rows="2" cols="20"></textarea></td> </tr> <tr> <td>Get the output:</td> <td><input type="submit" name="download" value="Get List" /></td> </tr> </form> Then when the form gets submitted it checks which form was submitted: foreach($_POST as $name => $value){ switch($name){ case 'upload': upload_data(); break; case 'download': download_data(); break; } } So if the user wants to upload a file, this function gets called: function upload_data(){ $file = $_POST['file']; if(isset($file)){ if(file_is_valid($file)){ echo "true"; } } else { echo "No file chosen"; } } For some reason, it triggers the else above, even though I selected a file, and that $file variable therefore should be set. Thanks for response Link to comment https://forums.phpfreaks.com/topic/244066-name-variable-not-being-sent-to-server-from-form/ Share on other sites More sharing options...
mikesta707 Posted August 6, 2011 Share Posted August 6, 2011 file data is located in the $_FILES super global, not $_POST. check out this tutorial to read about uploading files: http://www.tizag.com/phpT/fileupload.php Link to comment https://forums.phpfreaks.com/topic/244066-name-variable-not-being-sent-to-server-from-form/#findComment-1253416 Share on other sites More sharing options...
AyKay47 Posted August 6, 2011 Share Posted August 6, 2011 To add to Mikesta's statement which is also a valid point, "file_is_valid" isn't a predefined PHP function, so unless you have created this function...it will always return FALSE... I think you want is_file instead...? Link to comment https://forums.phpfreaks.com/topic/244066-name-variable-not-being-sent-to-server-from-form/#findComment-1253417 Share on other sites More sharing options...
johnmerlino Posted August 12, 2011 Author Share Posted August 12, 2011 Yeah this was silly error. I didnt realize I was using $_POST rather than $_FILE. Link to comment https://forums.phpfreaks.com/topic/244066-name-variable-not-being-sent-to-server-from-form/#findComment-1256604 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.