I have this form for multiple images here:
http://www.comehike.com/account/upload_hike_photos.php?hike_id=58
And when I try to get the images in the POST processing of the request, I get these messages:
Uninitialized string offset
This array does not get populated:
$_FILES['uploadedfile']['name']
Any idea why? I have a very similar form like this working here:
http://www.hikingsanfrancisco.com/account/upload_hike_photos.php?hike_id=58
But I can't quite figure out what the problem might be that makes the array of images be empty.
I think you need to end the name="uploadedfile" with a bracket so: "uploadedfile[]". Then you can go through the $_FILES['uploadedfile'] with foreach and put them in your server just like the other form.
EDIT: I've checked the source of your second link too, you have brackets in the second page you've provided. :)
I find dumping the contents of the array often helps see the structure. Try:
<?php echo '<pre>'.print_r($_FILES, true).'</pre>; ?>
However, that said, I think Tekin was on the right path. Try using a foreach or accessing the files directly like:
$_FILES['uploadedfile'][0]['name']
$_FILES['uploadedfile'][1]['name']
$_FILES['uploadedfile'][2]['name']
Related
I'm stuck with a php/mySQL thing..
I have a dynamically created form and I want to parse the $_POST variables it generates. To be specific,I have a query in SQL which generates the fields in my form. Then, I need to process these variables in the php file, where the action of the form goes.
However, I cannot parse the dynamically created $_POST variables. Below is my code:
$sql="just-a-query";
$result = mysql_query($sql);
while ($data = mysql_fetch_array($result)) {
${''.$data['parameterName']}=$_POST[$data['parameterName']];
}
For example, if I have 3 variables that got through the form the values:
house=1
tree=3
car=2
I would like to save them via php like this:
$house=$_POST['house'];
$tree=$_POST['tree'];
$car=$_POST['car'];
However I can't get through it. It returns Undefined index error. Any thoughts?
If you want to find if a variable is defined before using it, it's as simple as using isset():
if( isset($_POST[$data['parameterName']]) ) {
${''.$data['parameterName']}=$_POST[$data['parameterName']];
}
If on the other hand, it's supposed to be defined (you see the form element), but then it's not getting defined in the postback. First check to make sure that your form submission type is post, then check to make sure you are using the name attribute in the form elements.
thank you for your time. My problem was that I was parsing wrong parameters from the HTML.
Yes, I'm an idiot and yes, var_dump() helped me to figure my error.
Thanks again!
btw, my code was working perfectly. Ha!
I have a slightly frustrating problem...
I'm sending a form value to PHP via AJAX and that seem to work fine.
When I do var_dump in PHP I see my values I can also set a variable and echo it correctly.
However, the line
$prod_id=$_POST['product'];
causes an uncaught type error in the browser.
If I just set the variable with text in PHP everything works fine.
To conclude, this piece of code works fine:
$prod_id=("Slab Skate");
$selected_customers = mysqli_query($link,"SELECT * FROM customers
INNER JOIN cust_products on cust_products.cust_id=id
INNER JOIN products on products.prod_id=cust_products.product_id
WHERE products.prod_name='$prod_id'");
This code causes uncaught typerror:
$prod_id=$_POST['product']
Same SQL statement as above.
If I do
var_dump ($prod_id);
after setting it with $_POST I get:
string(10) "Slab Skate"
My form data in network headers tab of Chrome developer tools say:
product:Slab Skate
I don't get it...
Thanks in advance for any tips.
Update and some clarifications.
The error I get is this: "Uncaught TypeError: Cannot read property 'documentElement' of null" which is a Javascript error coming from a function later in the code. However, since the whole thing works if I "hardcode" the variable instead of setting it from $_POST my assumption was that the error must reside in PHP.
But maybe that's not the case...
What I'm doing is the following: posting a form value to PHP -> use value to select from my_sql and prepare an XML output. So far so good, (I can see the xml output in Chrome dev tools) but then I go back to a javascript to fetch the xml output from my PHP file and then it fails.
When thinking about it, it's rather obvious why it works with a "hardoded" variable and not with the $_POST set one.
So, I see two solutions either set the PHP variable in my_sql or using javascript more intelligently.
Do anyone have a smart solution? I could post all the code, but it's quite long.
Second update:
I solved the issue by writing an xml file to the server instead of trying to download it from the php file. Then my java function can process the xml correctly.
It does work, but I'm not sure how well it scales? It must be better to process the xml output from PHP directly rather then saving it to file first and then process. But, I have no insight on how big the difference is...
/Tim
I wouldn't use SELECT * FROM when using Inner Join if i were you. that would cause all sort of problems , simply give the names of columns.
Probably another basic question but its been annoying me for a while now...
I have a php file which is included on a php web page to bring in dynamic content from my mysql database.
Everything works fine with this except when i try get pictures to work and here is the problem.
I am using the code:
echo "<img src=fishery_images/$region/$url/$url1.jpg'/>";
All of which has been selected from the correct table and so on. an example of what i want this to resolve to is below:
fishery_images/fife/goldenloch/goldenloch1.jpg
However because i have the code:
$url1.jpg
and $url1 is not defined as anything then it resolves as the following:
fishery_images/fife/goldenloch/.jpg
i can have anything from goldenloch1.jpg all the way to goldenloch10.jpg so need to be able to say which image should be used.
how is it i say $url1.jpg without meaning $url1?
Really hope this makes sense... and thanks in advance
echo "<img src=fishery_images/{$region}/{$url}/{$url}1.jpg'/>";
http://en.wikipedia.org/wiki/String_interpolation#PHP
You'll want to use curly braces to wrap your variable:
echo "<img src=fishery_images/$region/$url/{$url}1.jpg'/>";
I wish to write the response of hitting a given url into the href attribute of an anchor tag using PHP. How can I do this?
Here's an example of what I excpect to happen
mylink.com/getdoc?name=documentA
returns a string as a response:
mylink.com/document2012-03-15.pdf
I need to write this response (using PHP into the href attribute as shown below:
Open Document A
(so the above will be the final source of my page.
I think there are a few ways to do what you want. Not all of them will work exactly as you ask for, but the end result should be the same.
Solution one
My first possible solution was already posted by #shanethehat. You could use file_get_contents to call your PHP script via HTTP and get the response.
Solution two
Another possible solution was suggested in the comments of the post by #YourCommonSense. You could simply include the getdoc script in the PHP script that is generating your HTML file, like this:
$_GET["name"] = "documentA";
echo " Open Document A ";
Solution three
Or you could change the way the getdoc script works. You could use a script more like this:
header("Content-type:application/pdf");
header("Content-Disposition:attachment; filename=\"{$_GET["name"]}\"");
readfile($_GET["name"]);
And you keep your link like this: Open Document A . When getdoc.php is called, it will get the specified file and start a file download.
NOTE: you should probably do some input sanitization with this method (removing slashes, making sure the file ends in .pdf, etc) to make sure someone doesn't try to get a file they're not allowed to get.
That's all I'm coming up with at the moment. There might be a more clever way to do it, but hopefully one of these solutions will do it for you. I would try solution 2 or 3 first, and if they don't work out for you, then go with solution 1.
<?php
//get output from URL
$myfile = file_get_contents('http://mylink.com/getdoc?name=documentA');
?>
Open Document A
How to write response to file using php
Noway.
PHP do not process HTTP requests.
You have to set up your web server to do the rewrite.
There are 100500 questions under mod_rewrite tag, you will find the solution easily.
Note that you may wish to rewrite your url to /getdoc.php?name=document2012-03-15.pdf, not one you mentioned in your question
I would like to know how can I access or read the array from another page. I am working on a PHP page which contains the array, and I want to display the content of that array on another PHP page.
For example, I used the following method in a PHP file and I want to get the content of the array in another PHP file. what is the method that is going to receive the array's content in the second page.
<?php
$r = new HttpRequest('http://localhost/sameh.php', HttpRequest::METH_POST);
$r->addPostFields(array("n" => 'heba')) ;
$r->send();
?>
This code is in the first page but I don't know what to write to receive it on the second one.
Maybe my question was not that clear and sorry about that ,, I want to find a way to access the array that is defined inside the HttpRequest() class on another page. So that the array "n" that include value "heba" will be displayed on another.php page. this is what make me thinking that the problem is on how to access the content of the array on the second page.
I tried the session and It sends the array to another page ,, but when I tested with the
httpRequest() method it doesn't send the content of the array "heba" to the second page.
Thanks for your help.
Sounds like this is a job for Sessions.
You can read the complete session guide here
In the script that has the array you can do something like:
session_start();
$_SESSION['array'] = $array;
In the next script you access it similarly:
session_start();
print_r($_SESSION['array']);
Include that file in your php file where you want to use that array. This should solve your issue PHP - How to send an array to another page?
I'm not sure what the HttpRequest class is, but at a guess, it's POSTing variables to the sameh.php file. You should be able to access the variable on the next page by doing this:
echo $_POST['n'];
Which should print "heba".
Note that to work with serialized arrays, you need to use POST as the form's transmission method, as GET has a size limit somewhere around 1024 characters.
I'd use SESSIONs wherever possible.
You can use serialize() and unserialize() on your array to represent it as a string and pass it via POST.