Jump to content

Php File That Generates Xml For Ajax Form Handling


robertlob
 Share

Recommended Posts

Disregarding error checking and file type filtering for the moment, consider that I have an image file input selector in a "multipart/form-data" form on an HTML page. It is not a required element for validation. There are other types of inputs, some required, including checkboxes and text fields.I have been unable to find or recognize an example of how I structure the $_FILES['photo1'] element in the php file that generates XML for AJAX handling of the form.I need to extract the contents and post it to a mysql database table. The best I've been able to do is get it to run, but it posts null or empty data.relevant code I have tried:

if ($_FILES['photo1']) {		$img1 = $_FILES['photo1']['tmp_name'];	$img1_name = $_FILES['photo1']['name'];	$img1_size = $_FILES['photo1']['size'];	$img1_type = $_FILES['photo1']['type'];	$img1_error = $_FILES['photo1']['error'];	}//other inputs and processing code$binary_data1 = addslashes(fread(fopen($img1,"rb"), filesize($img1)));$sql = "INSERT INTO image_table VALUES ('','$binary_data1','$img1_name','$img1_size','$img1_type','$property_record')";	$r = mysql_query($sql, $dbc);	if (mysql_affected_rows($dbc) == 1) {	//do other stuff	}

...where '' is the auto incremented table record, and $property_record is a key field the value of which is determined elsewhere. My code posts only the auto inc. table record and the value of $property_record. The actual image data, name, size, and type are blank or 0. So I'm obviously not extracting the data properly from the $_FILES[] element.The non-AJAX php handler file works fine, using essentially the same code as above.Max_File_Size in php.ini is 4MMax_File_Size in form is 600000Average image size is 44-50 KBphp file that generates the xml header is "Content-Type: text/xml" -- is this right?xml version = "1.0" encoding = "utf-8" standalone = "yes"Database and tables all set to utf-8 unicode ciAssistance much appreciated. I've been fussing with this way too long.Robert

Link to comment
Share on other sites

Your INSERT INTO query failing.INSERT INTO table_name (coloumns, here) VALUES (value1, value2)
Thanks, but I don't think so. The Insert Into is working, it's just putting empty values in the fields related directly to the image data. ie:The auto incremented record number is there as it should be.The binary_data1 is in the correct column, but shows [bLOB- 0B].The filename column is empty.The filesize column shows 0.The filetype column is empty.The propertyrecord column shows the correct value.Either the $_FILES[] is failing to upload the selected image; or I'm not extracting it properly.
Link to comment
Share on other sites

Use print_r to check what's in the $_FILES array:print_r($_FILES);
Tacked on to the <result> tag, print_r($_FILES); gives me a 1which I think means the file was larger than the form allowed. but the form restriction is 600000 and the php.ini is 4M. The image is only 37 KiB. I don't understand what's going on. Edited by robertlob
Link to comment
Share on other sites

I'm not sure what you mean by result tag. If you're exporting this to XML or something, just copy the form and form processing code to a new page and get that working first before changing the output format. The print_r function just prints everything out immediately, if you want to have it return the output instead of printing it you would use true for the second parameter. e.g.:$str = print_r($_FILES, true);

Link to comment
Share on other sites

I'm not sure what you mean by result tag. If you're exporting this to XML or something, just copy the form and form processing code to a new page and get that working first before changing the output format. The print_r function just prints everything out immediately, if you want to have it return the output instead of printing it you would use true for the second parameter. e.g.:$str = print_r($_FILES, true);
The form is on a HTML page.it is intercepted by a javascript file that creates an XMLHttpRequestObject, then to a page specific javascript file which checks for AJAX support and either sends the form data ...to a AJAX php file that creates an XML tree containing any errors to be highlighted on the form page (validation stuff) and the results to be reported on the form page in a <results> div if successful, or......to a regular old non AJAX php file that displays any errors on a separate page from the form page (validation stuff) or reports success if no errors.The NON AJAX page works fine. The $_FILES input selector contains the data it needs and posts it to the db.The AJAX page works fine, except that the $_FILES input selector contains no data for some reason. So it posts null data.I don't understand why the NON AJAX file gets the $_FILES data and the AJAX file doesn't. I have essentially the same code on both files.
Link to comment
Share on other sites

Ah, I missed that you were using ajax. You can't upload a file through ajax, you need to use a different method. One that I've seen is creating a form inside of a hidden iframe and then submitting that. I use ExtJS for my ajax work, that's the way they do file uploads.

Link to comment
Share on other sites

Thank you. I'm even glad to find out that it can't be done that way, I've been fussing with it for weeks and nobody seemed to know... or I was asking the wrong questions.This is my first forey into the ajax world, and I still have lots to learn. Learning what I can't do makes it a lot easier to learn what I can do. I'll probably be back as soon as I find out more about iframes and whatnot. It wasn't in the book when I took HTML in school years ago.Appreciate the answer,Robert

Link to comment
Share on other sites

You'll get a lot of info if you do a Google search for ajax uploads. I think I misspoke though, it doesn't create the form inside the hidden iframe, it creates the form normally and targets it to submit to the iframe. So, the page still doesn't reload, and when the response comes back and gets loaded in the iframe I think my ajax library takes that response and makes it available for me to use like a regular ajax response.

Link to comment
Share on other sites

Is the ExtJS you mentioned a library like YUI or similar? I understand libraries are much easier to use, but I thought it would be easier if I learned the other way first. YUI gives me a case of information overload because I don't know enough about it to know what I'm looking for yet.

Link to comment
Share on other sites

Yeah, ExtJS actually started as an extension to YUI before they broke off. It's definitely better to learn everything you can before using a library, but for what it's worth I haven't done any ajax uploads without using the library. When I needed to do an upload for an ajax site I was writing myself I ended up using a popup window to hold the upload form as normal and then return the data back to the parent page after the upload finished. It wasn't the most elegant, but it was the best thing for me to do in the time I had.That being said, since I started using ExtJS for one major project I frankly don't really have any plans not to use it, the interface elements look too good for me to try to mess around with the regular HTML way of doing things. It's too easy to set up drag and drop, double-clicking, buttons and menus, grids, trees, movable resizable windows, etc. But, of course I say that now. For the first few months of learning ExtJS it was pretty confusing, but now I'm as fast with it as with anything else, and it actually makes my PHP a lot cleaner.

Link to comment
Share on other sites

Yeah, ExtJS actually started as an extension to YUI before they broke off. It's definitely better to learn everything you can before using a library, but for what it's worth I haven't done any ajax uploads without using the library. When I needed to do an upload for an ajax site I was writing myself I ended up using a popup window to hold the upload form as normal and then return the data back to the parent page after the upload finished. It wasn't the most elegant, but it was the best thing for me to do in the time I had.That being said, since I started using ExtJS for one major project I frankly don't really have any plans not to use it, the interface elements look too good for me to try to mess around with the regular HTML way of doing things. It's too easy to set up drag and drop, double-clicking, buttons and menus, grids, trees, movable resizable windows, etc. But, of course I say that now. For the first few months of learning ExtJS it was pretty confusing, but now I'm as fast with it as with anything else, and it actually makes my PHP a lot cleaner.
Thanks. I'll check it out and see what I can learn. Appreciate your assistance. I'll probably be back to ask more questions as soon as I figure out what to ask.-Robert
Link to comment
Share on other sites

make sure that in image_table the first column is Defined "NOT NULL" Or "Primary Key"This might be reason for the failure of the query.....if it has been defined primary key or not null ....then you just cannot insert "" into it !!!!!!

Edited by PravinBhat
Link to comment
Share on other sites

Thanks. But the query is not failing. The files are not uploading so there is noting to insert. It is something that the AJAX system apparently cannot do, from what I've read. There are plenty of workarounds, but some are limited, and others are more complicated than I'm qualified to do right now. Need much more study. So I'm going to close this topic, hit the books, and get on with some other projects in the meantime.Thanks for all who provided suggestions and assistance.-robert

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...