Why is my PHP upload script not working? - php

I am doing some simple work with uploading a file. I am ignoring error checking and exceptions at this point just to get my uploads working. I have this HTML form:
<form action='addResult.php' method='post' enctype='multipart/form-data' target='results_iFrame' onsubmit='startUpload();'>
Entry: <input type='text' id='entry' />
Stop: <input type='text' id='stop' />
Final: <input type='text' id='final' />
Chart: <input type='file' id='chart' />
<input type='submit' value='Add' /></form>
As you can see, it calls 'addResult.php' within the iFrame 'results_iFrame'. The Javascript is just for animation purposes and to tell me when things are finished.
addResult.php has this code in it (along with processing the other inputs):
$upload_dir = "../img/";
$chart_loc = $upload_dir.basename($_FILES['chart']['name']);
move_uploaded_file($_FILES['chart']['tmp_name'], $chart_loc);
print_r($_FILES);
It uses the 'chart' input from the form and tries to upload it. I have the print_r() function to display some information on $_FILES, but the array is empty, thus making this fail. What could I be doing wrong?

When dealing with forms, the name= is used to reference items serverside, not the id=.
Just change all those id attributes to namess, and you're good to go (hopefully).

You have to give your input elements name attributes or they don't get passed to your script. The id is used for DOM/browser/client-side use.
There might be another problem after you resolve this, but this is a good first step.

Try adding the name attribute to your input values like so:
<input type='file' id='chart' name="myfile"/>

Your inputs have no "name" attribute, only IDs.

You need to give your form fields name attributes

Related

How to take input value from outside form action without javascript?

I still in training,
But i stuck in this problem.
// we need this password in 2 form POST
<input style='text' name='password' />
<form action='delete.php' method='POST'>
... // i need password to be here
<input type='submit' name='delete' value='Del' />
</form>
<form action='edit.php' method='POST'>
... // and i need password to be here too
<input type='submit' name='edit' value='Edit' />
</form>
My menthor want to do this using 2 form but I need the input password outside this 2 form.
Are there any way to do this without using script?
I can do this with 1 form and action.php that include 2 php file(edit.php and delete.php)
But he said that it is waste.
Can someone help? I already checking using search engine and found nothing.
Maybe your mentor just testing you on how you solve the situation.. As far as i know... you cant pass value of a textbox upon submit without putting the textbox inside a form.. I just wonder why javascript is forbidden.. Well anyway I suggest you to use 1 form and 1 page for the action and in the action make an if statement to separate you delete and edit method.. maybe in that case it is not waste.. It's just only a suggestion..

stored the several value of the textbox into an array php

Im making my project, and my project is make a program that will enter name,age and address.
There was a 3 textbox and a submit button. when the user answer the 3 textbox and click submit button, the data or the input of the user will save into an array, but when the user try to input again the previous input will not be replace or it will be there together with the user new inputs, and later those input where use to save into mysql db, do you think this project is possible using php array? i have no sample code because i dont know how to start.thank you so much.
index.php
<form action='index.php' method='get'>
<input type='text' name='name[]'>
<input type='text' name='age[]'>
<input type='address' name='address[]'>
<input type='submit' value='Save'>
</form>
Simple solution is to use 'sticking form' - form elements that stick (keep) the values previously entered.
You can make a textbox sticky like this:
<input type='text' name='name' value='<?php isset($_POST['name'])? $_POST[\'name\']: ""; ?>' />
You need to sanitize the inputs, however, if you try to use it for some real application.

Can I include a forms value into the action redirect in php?

Is it possible in php to include a forms value into the action redirection?
For example:
<form method='POST' name='Select' action='customer.php?CID=xxxxx'>
<input type=text width='5' name='searchVal' />
where xxxxx is the value entered into the form.
I've tried a number of different ways and I'm just not figuring it out! (Still sort of new to php) Any help would be appreciated.
It was looking like I would have to use $_POST and $_GET. A little more information might be in order... customer.php displays a list of customers in order by ID, name, etc. The user currently clicks on the customer ID that they want to display the details for. I'm trying to add a box where they can just enter the customer number to get to the details quickly, but I still want to have the listing displayed. From what it is sounding like, I will have to do this as two separate programs...is that true?
How about this:
<form method='POST' name='Select' action='customer.php'>
<input type='hidden' value='xxxxx' name='CID' />
<input type=text width='5' name='searchVal' />
...
</form>
You are free to add as much hidden values as needed.
Note, that you can even use PHP-like array notation_
<input type='hidden' value='xxxxx' name='CID[1]' />
<input type='hidden' value='yyyyy' name='CID[2]' />
At the PHP-side, access those values using this syntax:
$_POST[ 'CID' ][ 1 ]
$_POST[ 'CID' ][ 2 ]
UPDATE-1
Ah, you want to use a user-entered values to the Action URL just before the form gets submitted?
In this case you need to use JavaScript. Access the DOM to change the Action URL.
But let me ask, why you need to post a form value additionally as a parameter of the Action URL?
UPDATE-2
You wrote: 'From what it is sounding like, I will have to do this as two separate programs...is that true?'
No, actually not. You can still use one customer.php which checks at its beginning, if it was called using a linked customer in the table element or a searched customer in the search field.
In other words: You don't need to prepare two scripts, but two forms for two purposes which call the same script customer.php.
You can include the required value in a hidden field in your form:
<input type="hidden" name="CID" value="xxxxx" />
The reason this is required is that you are submitting the form to your server via POST, but appending parameters to the URL requires submission via the GET method.
Not without a post to the server. The value in the form is filled in client-side, so it has to return to the server before you can add it to the action. (at least, if you want to use php).
You can either
add it after posting (might not be usefull)
use javascript
just not use the GET CID, but get it out of the POST in your customer.php script.
I got it finally! It's actually very simple!
In the body of the code I put this:
<form action='#_SELF' method='GET' name='Projected'>
<input type=text size=5 name='CID' value='' title='Enter Customer number to display' />
<a href='#' onclick='document.Projected.submit();' title='Enter Customer number to display'>Go</a>
And at the top of the code I just do a:
if (!isset($_GET['CID'])) { ...
It works exactly the way I wanted it to!
Thanks everyone for the help! I appreciate it! (And I'm learning more and more about PHP everyday!)
Im pretty sure you cant do that unfortunately

i got a undefined index error when file upload in php

i searched again and again but could not find the right answer. here is the situation. i got more than one forms in the same php file and below shows the code.
when i echo as below
echo count($_FILES["fileUploadPath"] );
it shows 0 as the count and
Notice: Undefined index:
addProjectFileUploadPath in C:\wamp...
updated: probelm solved..... error came due to 3rd party jquery plugin called "fileinput"
add enctype="multipart/form-data" to the form
Try looking at the entire array with this:
echo "<pre>".print_r($_FILES,true)."</pre>";
Then use this manual page to let you know what the error numbers mean. That will probably give you a good idea of what is going on.
PHP File Upload Error Codes
Okay, there are a couple of things you need to be aware of.
1) You can have as many forms on a page as you want, but you can only submit one of them. You need to make sure the form you expect is being submitted. I'm assuming you're using the submit button names for doing this. However this can result in problems if someone submits the form by hitting enter in a text entry region, the button won't be submitted. A hidden field would be better as it would always be submitted.
2) There doesn't seem to be a MAX_FELE_SIZE form input anywhere in your file upload form. File uploading will not work without it. You need to put something like <input type="hidden" name="MAX_FILE_SIZE" value="30000" /> before the file inputs on your form.
I had the same problem before and I noticed that It happens when I don't close the tags, so try closing all input tags like this:
<form action='upload.php' method="post" enctype="multipart/form-data">
<!-- at the end of the input add / -->
<input type='file' name='file' />
<input type='submit' name='upload' />
</form>

Refering to arrays of form elements (name="blah[]")

I have a form with series of 3 file upload fields, each of which has a related hidden "todo" fields.
The file upload fields start greyed out and a user can either upload a new file, remove a file if one has previously been uploaded in that position or leave it unchanged (i.e. use the previously uploaded file or leave it blank).
The todo fields should store what is to be done with each file (i.e. 0=upload new, 1=delete existing, 2=leave unchanged).
I have a series of buttons next to the upload field. One for "upload new" (which enables the file upload field and (should) set the related todo field to 0; one for remove (which disables the file upload box); and one for "leave unchanged" (which also disables the file upload field).
I've found the name="blah[]" technique for creating arrays when the form is posted to a PHP document which makes looping through the files nice and easy. The trouble is that I need to edit the value in the related "todo" fields and if they're all named "todo[]" then I can't refer to one specifically...
The code is something like this:
<input type="file" name="file[]" />
<input type="hidden" name="todo[]" />
<input type="button" onclick="enableFileField('file[]', 0)" value="Upload New" />
<input type="button" onclick="enableFileField('file[]', 1)" value="Remove Current" />
<input type="button" onclick="enableFileField('file[]', 2)" value="No Change" />
I'm pretty sure I'm missing something and that this is actually quite simple...
You can give the fields ids in addition to names. The name would be used for the post to the server, but the id can be used for referencing the input in JavaScript:
<input type='hidden' id='todo_0' name='todo[]'>
<input type='hidden' id='todo_1' name='todo[]'>
In JavaScript, document.getElementById("todo_0") will give you the first todo field. Be sure to keep the ids sufficiently different that Internet Explorer doesn't get confused (it has namespace bugs around id and name [it tends -- completely incorrectly -- to put them in the same namespace]).
You could increment a counter in javascript as you add more fields, so you create todo[0], todo[1], etc. This wouldn't change how PHP interprets it.
Edit:
Realised you aren't creating fields on the fly in javascript, but the naming still applies
You could give each of the todo inputs a unique ID that you remember, or, I believe you can use
<input type='hidden' name='todo[0]' />
<input type='hidden' name='todo[1]' />
etc. in your HTML.
If I understand what you are asking, you want to be able have to multiple fields that will be used to upload a file. For example, if you have 3 files to modify, you would have three hidden todo fields?
A quick and easy solution would be to keep a hidden field for the number of files such as:
<input type='hidden' name='numFiles' value='1' />
and update that as you add or remove files with javascript. Then as others have suggested, give each todo a unique id as such:
<input type='hidden' name='todo1' />
Now you can easily find a todo because each file will have a unique one and you will be able to update it from there.
Once you post the form, you can pull the number of files there will be from the numFiles field and loop through all the todo's with a number appended to the end.

Categories