I am having trouble with a file upload portion of a project. I have created the form with the inputs but when i try to "upload" the file it does not get posted into the $_FILES array. Code posted below. I have declared an enctype within the from tag but the file still does not get posted into the array it only shows Array()
here is my form code:
<?php
require("includes/application_top.php");
$pageTitle = "Add A Product";
require("includes/header.php");
print_r($_FILES);
?>
<div class="container-fluid py-4">
<div class="row ">
<div class="col">
<form method="POST" action="s.php" enctype="multipart/form-data">
<!-- Product Name input -->
<div class="form-outline">
<input type="text" id="ProductName" class="form-control" />
<label class="form-label" for="ProductName"> Product Name</label>
</div>
</div>
<div class="col">
<!-- Product Description input -->
<div class="form-outline">
<input type="text" id="ProductDesc" class="form-control" />
<label class="form-label" for="ProductDesc">Product Description</label>
</div>
</div>
</div>
<hr />
<div class="row">
<div class="col">
<!-- Product Price input -->
<div class="form-outline">
<input type="number" min="0.00" max="10000.00" step="0.01" id="ProductPrice" class="form-control" />
<label class="form-label" for="ProductPrice">Product Price </label>
</div>
</div>
<div class="col">
<!-- Img Upload input -->
<div class="form-outline input-group mb-3 ">
<input type="file" id="ImageUpload" class="form-control" />
<input type="submit" value="Upload" />
</div>
</div>
</div>
</form>
</div>
<?php
require("includes/footer.php")
?>
Any form field must have a name attribute in order to populate PHP's $_GET , $_POST or $_FILES superglobals after submitting the form.
$_GET and $_POST depend on the form method, and $_FILES is specific to file uploads (HTTP POST method only).
Your file input should then look like this:
<input type="file" id="ImageUpload" class="form-control" name="image_upload" />
Given the image_upload name I wrote, you should be able to get the file informations using $_FILES['image_upload].
Read about file uploads in the documentation: Handling file uploads.
You have to provide a name attribute to your fields, both $_POST and $_FILES work on the name attribute, not the ID. id is mostly used when you do something with the field using javascript, like when submitting the form with an ajax request.
In your case, you would need names:
<input type="file" id="ImageUpload" class="form-control" />
would become
<input type="file" name="ImageUpload" class="form-control" />
The same goes for your other input fields, you need a name attribute before you will see then in $_POST.
Related
I'm trying to duplicate 4 input text. For example I want to duplicate 'Produk Simpanan Saham', then only 4 input on 'Produk Simpanan Saham' div will be duplicated. I already tried a few methods using jQuery but it seem doesn't work.
<form action="" method="post" class="form-horizontal">
<br><br>
<div class="form-group">
Upload File :
<input type="file" name="file">
</div>
<div class="saham">
<div class="w3-container w3-black">
<h3>
<center>Produk Simpanan Saham</center>
</h3>
</div>
<label for="kp_produk">Kode Produk :</label>
<input type="text" name="kp_produk" class="form-control">
<br>
<label for="produk_saham">Produk :</label>
<input type="text" name="produk_saham" class="form-control">
<br>
<label for="bunga_saham">Bunga :</label>
<input type="text" name="bunga_saham" class="form-control">
<br>
<label for="ket_saham">Keterangan :</label>
<input type="text" name="ket_saham" class="form-control">
</div>
<div class="harian">
<div class="w3-container w3-black">
<h3>
<center>Produk Simpanan Harian</center>
</h3>
</div>
<label for="kp_harian">Kode Produk :</label>
<input type="text" name="kp_harian" class="form-control">
<br>
<label for="produk_harian">Produk :</label>
<input type="text" name="produk_harian" class="form-control">
<br>
<label for="bunga_harian">Bunga :</label>
<input type="text" name="bunga_harian" class="form-control">
<br>
<label for="ket_harian">Keterangan :</label>
<input type="text" name="ket_harian" class="form-control">
</div>
you mush have blueprint of field first like
var blueprint= '<div class="saham">'+
'<div class="w3-container w3-black">'+
'<h3>'+
'<center>Produk Simpanan Saham</center>'+
'</h3>'+
'</div>'+
'<label for="kp_produk">Kode Produk :</label>'+
'<input type="text" name="kp_produk[]" class="form-control">'+
'<br>'+
......
used [] for all name field to get array request when you send form to server
example :
'<input type="text" name="kp_produk[]" class="form-control">'+
and make button add like (jquery) example
<button onclick="$('form').append(blueprint)">add</button>
just use
var dom=$($('.saham')[0]).clone();
//copy dom
$('.form-horizontal').html(dom):
//paste dom
dont forget to add [] on name tag dom input to set data send to array, because if you not set. data with key kp_produk value is last tag name with name kp_produk
if you use [] (kp_product[]) data will set to array
kp_product=['anu1',anu2];
Recently I need to use multiple input type file in one form. But when I post the form its returning null. Like I have nothing on $_POST. I have also add the enctype="multipart/form-data". I am stacked for last 2 days. Googled for many times. But still didn't get any solution. Please help me to out of this.
<form enctype="multipart/form-data" action="/admin/process_post/savepost" method="POST">
<div class="uk-grid uk-grid-small" data-uk-grid-margin="">
<div class="uk-width-medium-7-10">
<div class="md-card">
<div class="md-card-content">
<h3 class="heading_a uk-margin-medium-bottom">Add New Canvas</h3>
<div class="uk-grid" data-uk-grid-margin>
<div class="uk-width-medium-1-1">
<div class="uk-form-row">
<label>Post Title</label>
<input type="text" name="post_title" id="post_title" class="md-input" />
</div>
<div class="uk-form-row">
<label>Post Description</label>
<textarea id="post_desc" name="post_desc" cols="30" rows="20"></textarea>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="uk-width-medium-3-10">
<div class="md-card">
<div class="md-card-content">
<div class="uk-form-row">
<p>
<input type="checkbox" name="post_status" id="post_status" checked data-md-icheck />
<label for="post_status" class="inline-label">Post Status</label>
</p>
</div>
</div>
</div>
<div class="md-card">
<div class="md-card-content">
<div class="uk-form-row">
<label for="post_attachment" class="inline-label">Post Featured Image</label>
<input type="file" id="post_attachment" name="post_attachment">
</div>
</div>
</div>
<div class="md-card">
<div class="md-card-content">
<div class="uk-form-row">
<label for="result_img" class="inline-label">Post result Image</label>
<input type="file" id="result_img" name="result_img" multiple="multiple">
</div>
</div>
</div>
<div class="md-card">
<div class="md-card-content">
<div class="uk-form-row">
<button type="submit" class="uk-form-file md-btn md-btn-primary">Save Post</button>
</div>
</div>
</div>
</div>
</div>
</form>
I know this is a silly situation for a developer when they face problem like this.
Files can be gotten through $_FILES
When doing multiple uploads at same name, name it as array appending [] at end of name example: ( name="images[]" )
If upload continues to appear as empty, and you said in comment that with one file it's doing right, try to change at php.ini post_max_size and upload_max_filesize to a larger amount. Also check max_file_uploads - the number of files allowed for uploads per single request.
Your problem should be your form action, try to put an "/" on the end of it and see if it works:
<form enctype="multipart/form-data" action="/admin/process_post/savepost/" method="POST">
You need to change your file input name to an array
<input type="file" id="result_img" name="result_img[]">
Also try adding multiple
<input type="file" id="result_img" name="result_img[]" multiple>
To check for files you must use $_FILES instead of $_POST
I'm currently encountering a problem which is baffling me. There will be a lack of PHP mixed with HTML due to using a MVC Framework.
What i've got:
Form A on Page 1 with a submit button which directs user input from Form A to page 2 with a chunk of text for the user to read before continuing, a hidden text field which will contain a serialized array from Form A and a submit button to continue to the validation of user input.
My forms direct to the correct pages as required, the post array can be seralized providing I do not put it into a text field. The HTML from form A follows:
<form action="/RegisterInformation" method="POST">
<div class="row">
<div class="large-12 columns">
<input type="text" name="Username" placeholder="Username, ex: JohnDoe ">
</div>
</div>
<div class="row">
<div class="large-4 columns">
<input type="password" name="Password" placeholder="Password">
</div>
<div class="large-4 columns">
<input type="password" name="cpassword" placeholder="Confirm Password">
</div>
<div class="large-4 columns">
<input type="text" name="email" placeholder="Email, ex: JohnDoe#provider.com">
</div>
</div>
<div class="row">
<div class="large-12 columns">
<textarea name="Referral" placeholder="Where Did You Hear About Us?"></textarea>
</div>
</div>
<div class="row">
<div class="large-4 columns">
<input type="submit" name="submit" value="Continue" class="button">
</div>
</div>
</form>
The redirect to page 2:
<div class="row">
<div class=" large-12 columns">
<p>
<?php echo $data['PreregisterInformation']; ?>
</p>
</div>
</div>
<div class="row">
<div class="large-12 columns">
<form action="/Continue" method="POST">
<input type="text" name="SubmitInfo" value="<?php echo serialize($_POST); ?>">
<input type="submit" name="submit" value="Continue" class="button">
</form>
</div>
</div>
With the current example, i've got the text field set to being visible, so I can see what's going on.. But this is where it gets strange.
What i'm getting is two different types of results.
The first, is that when I echo the serialized array outside a input field. I get the desired results:
a:6:{s:8:"Username";s:1:"s";s:8:"Password";s:1:"4";s:9:"cpassword";s:1:"4";s:5:"email";s:3:"asd";s:8:"Referral";s:3:"asd";s:6:"submit";s:8:"Continue";}
The second is when I use the following:
<input type="text" name="SubmitInfo" value="<?php echo serialize($_POST); ?>">
Which passes an invalid serialized array to page 3 breaking the whole process.
I assume it's not a HTML related error, but the new line break in the serialized array definition a:6. I have made an attempt to rectify this problem by removing the new line break with str_replace:
$Ser = serialize($_POST);
$Ser = nl2br($Ser);
$Ser = str_replace("<br>","00",$Ser);
echo $Ser;
which has proven not to work.
This is not a direct solution of your problem, but have you tried json_encode or avoiding the hidden field by storing the date into $_SESSION?
How can I unset label from a exposed filter in a custom module? (form_alter)
HTML:
...
<form action="/location-list" method="get" id="views-exposed-form-zoekresultaten-geolocation-zoekresultaten" accept-charset="UTF-8">
<div>
<div class="views-exposed-form">
<div class="views-exposed-widgets clearfix">
<div id="edit-distance-wrapper" class="views-exposed-widget views-widget-filter-distance">
<div class="views-widget">
<div class="form-item form-type-textfield form-item-distance-postal-code">
<label for="edit-distance-postal-code">Postcode </label>
<input type="text" id="edit-distance-postal-code" name="distance[postal_code]" value="" size="60" maxlength="16" class="form-text" />
</div>
...
Have you tried to change field using form_alter and set '#title_display' => 'invisible' inside your field?
Form api reference
I'm making a tool to pull eve API data and I use a GET form to take the data and get access to the key ID and code to access the data. This then pulls the first character ID on the key and grabs all that character data.
for example the url is:
whatever.com/APIChecker/?keyID=123456&code=ABCDEFGHIJKL
I pull these variables out via the session variables that they create.
What I'm now trying to do is now have a for loop which puts up buttons for each character on the key so you can flick between characters, I'm trying and have tried get and post methods hidden in the button but can't seem to get my desired result. It seems to not be taking out the variable that i'm trying to pass and adding it into the $_SESSION array or the $_POST or the $_GET - i've tried print_r on all these variables but I can't get any output.
Here is my primary form thats on my main page:
<form method="get" action="APIChecker/" >
<legend> Submit an API Key</legend>
<div class="control-group">
<label class="control-label">Key ID:</label>
<div class="controls">
<input type="text"
class="input-small"
name="keyID""/>
</div>
</div>
<div class="control-group">
<label class="control-label">Verification Code:</label>
<div class="controls">
<input type="text"
class="input-xxlarge"
name="vCode"
/>
<p class="help-block"></p>
</div>
</div>
<div class="control-group">
<div class="controls">
<button type="submit" class="btn">Submit API</button>
</div>
</div>
What I'm trying to do now is get another session variable from another page - which updates a variable and refreshes the page with the keyID and code unaltered.
As in:
whatever.com/APIChecker/?keyID=123456&code=ABCDEFGHIJKL **&charID=654321**
so $_SESSION['keyID'] = 123456
& $_SESSION['code'] = ABCDEFGHIJKL
& $_SESSION['charID'] = 654321
what I've tried is:
<form method="get" action="" >
<div class="control-group">
<div class="controls">
<button type="submit" class="btn">
<input type="hidden"
name="charID"
value="$charID"
/>
</button>
</div>
</div>
<div class="control-group">
<div class="controls">
<button type="submit" class="btn">Submit API</button>
</div>
</div>
</form>
You can't output PHP values directly in HTML like this: value="$charID"
Try this instead:
value="<?php echo $charID; ?>"