php upload file multiple - php

I have a multiple upload textbox that I generate based on questions in my database. Right now my problem is that at the input, I have written <input type="file" name="file[<?php echo $id;?>]"/>, and when i want to save it, I write the program as $_FILES['file']['name'][$start]; where $start is a number I declared to match the question number. For example, if the file[123] is in the textbox, the post field when saving the information is also $_FILES['file']['name'][123].
But when I try to find if the textbox file[123] sent something like $_FILES['file']['name'][$start]<>"", it posts nothing.
This is the code when saving the information:
<input multiple name="file[<?php echo id;?>]" type="file" size="50" maxlength="100">
if ($_FILES['file']['name'][$start]<>""){
$fname=$_FILES['file']['name'][$start];
$tandatangan=$fname;
mysql_query("UPDATE answer SET document='$tandatangan' WHERE user_id='$id_user' and id_question='$id_soalan' ");
move_uploaded_file($_FILES["file"]["tmp_name"],"../files/$tandatangan");
}

Related

PHP form submitting all data but when i get its not complete data

it might seem weird but i have a strange problem. when i submit a form and check the request from network console of chrome developer tool it shows all the data is submitted but when i get that data in my update file it won't show all the data. the data is submitted as array. like i have below input field in form. total of 330 field we have with the same name and when we add new dynamic field and put data into it and then submit it won't save.
<input type="text" name="txtOptions[]" id="txtOption<?= $i ?>" value="<?= formValue($sOption) ?>" maxlength="100" size="25" class="textbox title" />
when is get on server side and print the data it won't show all the data.
$sOptions = IO::getArray("txtOptions");
print_r("<pre>");
print_r($sOptions);
print_r("</pre>");
exit();
I've tried hard to find out what's going on there is hidden field in my form of MAX_FILE_SIZE. I also try to change the value of this and make it more but nothing happen
<input type="hidden" name="MAX_FILE_SIZE" value="10485760" />
This only happening on live server not on localhost. any idea. sorry for not to be clear of my question but this is what i have first time in my life.
The IO::getArray are the custom classes functions.
i've found the solution. Actually i have above 1000 input variables in my form which is been generated dynamically and in my php.ini the max_input_vars has been set to 1000 only,that's why i was getting only 1000 input variable values not more than that.
i had no idea that the issue will be the number of input variables. I thought it might be an issue of file size.

How to Post both Files and Other Types of Input in PHP?

I am learning PHP and wish to deal with database.
Basically, I want the users to be able to put in some string in input1 and upload the file as well. Then, after the form has been submitted, I want to store the value of input1 and the path of the file in a database. In w3school, I have encountered a tutorial on how to upload a file. However, the tutorial does not deal when I am trying to upload a file and an input at the same time. Below is the code that I have so far.
So how do I go about doing it? Thanks!
<form id="form" action="uploaded.php" method="POST" enctype="multipart/form-data">
<pre>
input1: <input type="text" name="in1" maxlength="20">
File: <input type="file" name="uploadThis">
</pre>
<input type="submit" value="Submit">
</form>
I'm not going to type out exactly how to, because I'm on my iPad, but you'll need to make an if(isset($_POST['sumbit'])) statement for the submit, watch some tutorials on if statements, preferably by PHPacademy. You'll also need something to insert data with, INSERT INTO your db info goes here SELECT your db info goes here FROM your db info goes here. You can find information on doing that with YouTube or Google. You should also add a form action to your <form> , ex. <form action='#'>.

How to get the right forms value from a button click

Overview of what my question is:
I have an array that is populated via XML inputs, and from this I am the using it to populate a web form with form controls. From here I want to be able to select the exact form that is clicked, but to do that I need to give the controls some form of unique identifier, which is an issue...
As the site is of a betting nature and I am currently working with horse racing, each horse is given a unique identifier by default, I have tried to add this identifier to the forms.
e.g:
<?php
//Values from feed examples: 123, 234, 345
$valuesFromFeed = array(123, 234, 345); //These are not in my code, they are values from the XML feed
while ($uniqueIdentifier = $valueFromFeed) {
<form name="horse_<?php echo $uniqueIdentifier; ?>_frm" action="#">
<input type="hidden" name="horse_<?php echo $uniqueIdentifier; ?>" />
<input type="button" value="Place bet" />
</form>
}
?>
But then the problem comes when I try to reference this name of "horse_123", I need to know exactly what the value of that name is, which is impossible as there are millions of horses, tracks and races.
Example of trying to get post:
<?php
if (isset($_POST['horse_' . $uniqueIdetifier])) {
echo "You got the right thing here.";
} else {
echo "Still no joy.";
}
?>
The issue with the code above, is that once the $uniqueIdentifier has been used in the while above, it is removed and is no longer usable in this scope.
So to conclude, my point and question:
How do I get the correct name from the form in a submit for the specific horse that I wish to reference and get information on?
How do I use this information as I need to?
Better Description:
I have been given an XML feed and site as part of a handover, this feed contains many hundreds of races and horses.
When this information is loaded into the page, it is also stored in a database on the server, as well as sending it through some different loops (which are messy, but someone else's code I'm trying to clean up!) which split it down and then make up a dynamic menu containing all the races, horses, odds and information. (All information on a single horse within a race is kept in one form)
Next to the information stated in the prior paragraph, is 2 buttons, one that allows the user to take odds and another that allows users to take starting price.
On either of these button clicks, I need the information attached to said horse, and then populate a betting slip. In the form (mentioned above) the name is "horse_<?php echo $uniqueIdentifier; ?>_frm".
The problem that occurs to me is, yes data is stored on the server when it is loaded, that I cannot seem to get the right form via the unique identifier that is put into the form name
Edits
Added form surrounding my input as this is there, I just missed it in original question
Added the button that transmits data to where I need it
Added a better description of my problem
You can use multiple forms, one for each horse. Each form has a different action, where the URL includes the id of the horse. For example:
<form action="/horses/my_unique_horse_name">
...
</form>
<form action="/horses/another_horse_name">
...
</form>
Or you could have multiple forms all with the same action, with a hidden field for the name of the horse:
<form action="/horses/">
<input type="hidden" value="my_unique_horse_name">
</form>
<form action="/horses/">
<input type="hidden" value="another_horse_name">
</form>
Alternatively, you could have a button for each horse:
<form method="/horses/">
<button type="submit" value="my_unique_horse_name">My Horse</button>
<button type="submit" value="another_horse_name">Another Horse</button>
</form>
Beyond that, I don't entirely understand the problem. What kind of data are you submitting and retrieving?

Fill file input after form submit / on form submit error

I´ve a multipart form with a mixture of default inputs (text, select etc.) and a file upload (<input type="file">).
I´m using a combination of form validation class and upload library of Codeigniter for form submission. That works great.
I´ve only one problem for what I haven´t found a solution yet: If the user selects an image but misses to fill another required field (like "name"), then the form validation class blocks the request and shows an error message to the customer.
But now I´ve the problem, that the image was already submitted successfully and I don´t want to let the user add the file again. So I want to pre-fill the file input with this data.
I´ve tried different things like:
<input type="file" name="image" value="<?php echo set_value('image','');?>" />
and also spent time on finding a solution on the web but without success.
On the server side, you do not get any information about where the file is located on the client's computer, so in the scenario of a user uploading an image successfully but the user hasn't filled out the rest of the fields properly, you have to simply omit the input type="file" field entirely but keep a store of where the file is located on your server. There's a few ways to go about this, but it all involves taking the absolute location of the uploaded file and:
Inserting it back as a hidden value using <input type="hidden" name="uploadedFile" value="<?php echo $absPath; ?>" /> then checking for the existence of $_POST['uploadedFile'] and utilizing it appropriately. But this isn't a solid idea as you're now exposing server paths to the end-user (opens yourself up to malicious attack.)
Starting a session and saving the absolute path in the $_SESSION variable while presenting the user with a simple token in their re-attempt form.
I'd stick with method 2, so assuming you've done all the work to validate the form and upload the file and your file is located in $absFilePath, you could do the following:
session_start(); // This needs to be at the very top of you PHP file
// ...
$formToken = md5(time());
$_SESSION['uploadedFile'][$formToken] = $absFilePath;
Then render the token as a hidden variable using:
if (!empty($_SESSION['uploadedFile'][$formToken]))
echo '<input type="hidden" name="formToken" value="'.$formToken.'" />';
and hide the file upload portion using
if (empty($_SESSION['uploadedFile'][$formToken]))
echo // <input type="file" output here...
finally inside of your form submission code check for the existence of a formToken value before attempting to load $_FILES['image'] using isset($_POST['formToken']), and handle it using:
$absFilePath = $_SESSION['uploadedFile'][$_POST['formToken']];
Bam! Now you have your absolute file path as if the file had been uploaded just like before.
Since you haven't given enough code, I can only given you enough instruction to get you started, but this should be more than enough.

Disable form element after the data is saved

I have a website on which the user has to enter quite a number of data using text boxes. I want to lock the textbox after the first use. I mean for the example the user has to enter his name and save it but once saved, the user should not be able to change it again.
So can anybody suggest me what changes has to be made in this code
<td align="left" valign="top">
<input class="tooltip v_empty" title="first name" type="text"
name="<?php echo "LP".$lp_id."_"; ?>firstname[self]"
id="firstname[self]"
value="<?php echo $PLAN->lp[$lp->lp_id]->info['self']->firstname; ?>" />
</td>
You should use a better title. the disabled attribute in html may help you.
http://www.javascriptkit.com/javatutors/deform3.shtml
The <input> HTML elements take a disabled="disabled" argument which renders the user unable to normally modify the contents of the field (it will be greyed out). (Take note, that local HTML source modification can result in changing the data!)
Apart from that, you should take a look at how sessions work. You should store the already entered value in a session, or the database backend, and when the form is getting printed, you should check against what value is already present in the session or database.

Categories