Form Posts Text Inputs But Not File - php

I am trying to submit file inside a form to Laravel backend. It posts all the other text fields but not file (when I dd($request->all()))
<form class="form-horizontal" role="form" method="POST" action="" enctype="multipart/form-data">
<input id="item_price" type="item_price" class="form-control" name="item_price" value="">
<!-- other inputs -->
<input type="file" id="product_image" name="product_image" onchange="previewImage(this);"/>
<img id="previewing" src="{{asset('noImage.gif')}}" style="width: 100%; height: 100%" />
</form>
item_price seems on dd($request->all()) and also other fields are seen, but no file input
When I display the image with jQuery (previewImage) before submitting the form, it shows the photo. But after I submit the form, on Laravel side it shows all the other fields and values but not any for file (when I use dd($request->all()).
On php.ini, max-post-size is 100M.
jQuery('#previewing').click(function() {
uploadImageClicked();
});
function uploadImageClicked() {
jQuery('#product_image').click();
}
dd($request->hasFile('product_image') returns false

In Laravel (which should be tagged in your question) it's not part of the Request class with the other form values, it's in the file method.
See https://laravel.com/docs/5.3/requests#retrieving-uploaded-files
$file = $request->file("image"); is what you're looking for.

Related

Geting the file object from a form in Symfony

I have a scenario where I have to make use of a form the old fashioned why like this;
<form action="{{ path('admin_app_address_import') }}" method="post" name="form_import">
<div class="form-group">
<label for="inputFile">File input</label>
<input type="file" id="inputFile" accept="text/csv" name="inputFile">
<p class="help-block">Select a CSV file to import.</p>
</div>
</form>
And then get the parameters from the form like this inside my action;
public function importAction(Request $request) {
$file = $request->files->get('inputFile');
var_dump($file);exit;
//...
}
But I keep on getting null. How can I get the file I try to read from? I don't need to store it in a database whatsoever, I just need to read the content to upload that to the database. I'm making a CSV import action, but I can't seem to get the file object.
When doing var_dump($request->request->all()); I do get the filename, but that won't work right?
When allowing file uploads, the encoding type of the form must be set to multipart/form-data:
<form ... enctype="multipart/form-data">

ENCTYPE MULTIPART form Data

I want to know if it's possible to have a 'switch system' on the enctype of a form I explain : I have a form with a no required $_Files.. If I have an uploader file, everything is working but if I've not, I' receive an empty mail but with the good headers. I think its because of the enctype and I'don't know how to have an action on.
For many of our every day UI problems, jQuery can be one handy solution...
I have prepared this simple html form:
<form id="myform" method="POST" action="myActionFile.php">
<div>First Name <span>*</span><input class="form-control" type="text" id="firstname" name="firstname"></div>
<div>Last Name <span>*</span><input class="form-control" type="text" id="lastname" name="lastname"></div>
<div>Your File: <input class="form-control" type="file" id="myfile" name="myfile"></div>
<div><button class="btn pull-right" id="submit">SEND</button></div>
</form>
As you can see in the "form" tag the 'enctype' is not included. That will be taken care of by javascript (jQuery actually):
$('#submit').on('click', function(e){
e.preventDefault();
if($('#firstname').val().length < 1){$('#firstname').css('background-color', '#ffc9c9').prop('placeholder', 'Required');}
if($('#lastname').val().length < 1){$('#lastname').css('background-color', '#ffc9c9').prop('placeholder', 'Required');}else{
var fileData = $('#myfile').val();
if(fileData != ""){ //if file has been selected:
$('#myform').prop('enctype', 'multipart/form-data').submit();
}else{ //if NO file selected:
$('#myform').prop('enctype', 'text/plain').submit();
}
}
});
The above code
checks if the required fields (firstname, lastname) are not blank
checks the "file" input -if it is blank or not- and assigns the form the proper "enctype"
and finally, submits the form
Here is the fiddle: http://jsfiddle.net/8y2y5z4s/

Forms action - Error 404 - not found

I am trying to send a email using forms and PHP. When I click on the send button I get t the error Error occurred: 404 - not found. submit.php file is in same dir as the webpage. Why can't it find the submit.php. My html code.
<form action="submit.php" id="form1" method="post">
<fieldset>
<label class="input">Date:<span><span><input type="text" id="datepicker" value=""/></span></span></label>
<label class="input">Enter Your Name:<span><span><input name="name"/></span></span></label>
<label class="input">Enter Your Email:<span><span><input name="email"/></span></span></label>
<label class="input">Subject:<span><span><input name="subject"/></span></span></label>
<label class="text">Enter Your Message:<span><span><textarea name="comment" cols="0" rows="0"></textarea></span></span></label>
<label class="input">Enter Code:<img src="php/captcha.php"><span><span><input type="text" name="vercode" /></span></span></label>
<label class="butt"><strong><b>clear</b></strong>
<strong><b>send</b></strong></label>
</fieldset>
When using <a>'s to use as form submit buttons, I would suggest using a JavaScript function and put it in the onclick of the anchor. Like this:
function doSubmit(form_name) {
// check if the object exists
if (document.getElementById(form_name)) {
// submit the form
document.getElementById(form_name).submit();
}
// prevent the link from clicking
return false;
}
<strong><b>send</b></strong>
The same can be done for the reset button.
In this way, you never navigate your browser to a "javascript:...etc.." location (that does not exist). I'm not sure if this is the case here, but just as a best practice advise then. :)

Image upload form not posting input value when auto submitting form

I am trying to create an avatar uploader, but I want to be able to click only one button that opens the browsing window and then automatically calls the upload method from the AvatarUpload class.
The problem I have identified is that the form doesn't seem to be posting anything.
Any help would be great!
var_dump(isset($_POST['uploaded']));
if( isset($_POST['uploaded']) )
{
$img = new AvatarUpload();
$img->startUpload();
}
else
{
?>
<form method="post" enctype="multipart/form-data" name="uploadAvatar">
<p>
<input type="file" name="uploaded" id="file" onchange="this.form.submit()" />
<p>
</form>
<?php
}
?>
when you upload files they are listed under the $_FILES, not $_POST.

how to submit a from with validation in PHP?

I want to submit this form through PHP. with validation for required field and validation for phone number and email field also
<form action="" method="" id="get-protected">
<div class="row requiredRow">
<label for="txt_FirstName">
First Name</label>
<input id="txt_FirstName" type="text" class="required" title="First Name. This is a required field" />
</div>
<div class="row">
<label for="txt_LastName">
Last Name</label>
<input id="txt_LastName" type="text" title="First Name. This is a required field" />
</div>
<div class="row">
<label for="txt_Phone">
Phone</label>
<input id="txt_Phone" type="text" title="First Name. This is a required field" />
</div>
<div class="row requiredRow">
<label for="txt_Email">
Email</label>
<input id="txt_Email" type="text" class="required" title="Email. This is a required field" />
</div>
<div class="row">
<input type="submit" value="" class="button" />
</div>
</form>
In your method attribute inside your form, you need to declare either post or get.
Since your action attribute is "" it will submit to the page itself rather than redirecting to another page, so you can have your code that checks for validation in the same PHP file. First validation that is often checked is if the variable has a value by using isset:
if(isset($_POST['txt_Phone'])) { ... }
This just checks that the Phone number field does not contain empty data. I strongly suggest you perform other validation checks on the POST array so you do not have any users posting malicious code.
You can use functions like htmlspecialchars to prevent user-supplied text depending on what you plan to do with the values
Here are some references to help you along the way in the order they should be viewed.
Form Validation using PHP - PHP and MySQL Tutorial
PHP Advance Form Validation Tutorial
PHP Tutorial Part 2: Form Validation
Your form tag needs a target in the action field and a method in the method field (either GET or POST). So make the action your PHP script.
<form name="input" action="form_submit.php" method="get">
As for field validation, you will either have to parse that inside of the PHP and return a response or use Javascript in the browser to check on the fly.
Here is the shcema of such a script:
if ($_SERVER['REQUEST_METHOD']=='POST') {
//data validation:
$err="";
if (valid_phone($_POST['phone'])) $err="Wrong phone no";
if (!$err) {
//record data:
$sql="...";
query($sql);
Header("Location: ".$_SERVER['REQUEST_URI']); //redirect and exit
exit;
}
}
?>
<html>
<head></head>
<body>
<? if ($err) ?> <font color=red><b><?=$err?></b></font>
<form method="POST" id="get-protected">
here goes your form
Okay, firstly, I like to set the form action to <?=$_SERVER['REQUEST_URI']?> to submit it back to the current page, but leaving it as you have it will work fine too.
Secondly, you need to give all your <input>s a name attribute. This is the variable name that PHP will see.
When your users get an error (something doesn't validate correctly) you don't want all the data they entered to disappear. That means you have to set the value attributes of each input to what they had previously entered. Thus, your form starts to look like this:
<form action="<?=$_SERVER['REQUEST_URI']?>" method="" id="get-protected">
<div class="row requiredRow">
<label for="txt_FirstName">
First Name</label>
<input id="txt_FirstName" type="text" class="required" title="First Name. This is a required field" name="first_name" value="<?=htmlspecialchars($_POST['first_name'])?>" />
</div>
...
<div class="row">
<input type="submit" name="submit" value="" class="button" />
</div>
</form>
If you didn't know <?= is a basically a shortcut for <?php echo but it will only work if your server has short tags enabled. Some people prefer to type it out the long way (in case they want to switch servers later, or for future-compatibility, or because they're nutbars), but I say screw them, I'm lazy.
This page that has the form on it, has to saved with a .php extension (well, technically it doesn't have to, but that's another story). Then you need to handle you form validation. You have to code that up yourself. It might look something like this (put it above your form somewhere)
<?php
if($_POST['submit']) {
$errors = array()
if(empty($_POST['first_name'])) $errors[] = 'please enter your first name';
if(empty($errors)) {
// save the data to database or do whatever you want with it
header('redirect:succcess.php');
} else {
foreach($errors as $e) {
echo $e;
}
}
}
?>
It's been a while since I've coded in PHP so forgive me if there are syntax errors. That's the jist of it anyway, I'm sure you can find validation libraries out there if you Google. Might take some of the grunt work out of trying to validate email addresses and such.
Using Javascript you can do the validation for this form.For each condition you can use return true and return false,based on the condition.Then you can submit the value.
Using action attribute in form tag the values will be submitted to that file.

Categories