After looking around for weeks and being totally confounded by the tutorials that were already on the books, I have decided to write my own jQuery upload script. I thought I could get it to work but I am having issues. I will enumerate them:
On the page that I am writing this for, I have placed a jQuery script that looks a little like this:
$(document).ready(function() {
var thumb = $('#thumb');
$('#profilepicinput').live('change', function(){
$("#preview").html('');
$("#preview").html('<img src="loader.gif" alt="Uploading...."/>');
$("#registerpt3").ajaxForm({
target: $('#registerpt3').attr('action')
}).submit();
});
$('#profilepicbutton').change(function(){
alert("Boy I hate PHP");
$.ajax({
type: "POST",
url: "register3.php",
success: function(msg) {
alert( "Data Saved: " + msg );
$.ajax({
type: "POST",
url: "retrievePic.php",
success: function(msg) {
var image = new Image();
$(image).load(function(){
console.log("we have uploaded this image");
}).attr('src', 'images/');
alert("AJAX Success!");
},
error: function(msg) {
alert(" didn't work");
}
});
},
error:function(msq){
alert("didn't work!: " + msg);
}
});
});
});
This calls the following script which will load a picture into a database:
<?php
session_start();
$email = '';
if (isset($_SESSION['user_email'])) {
$email = $_SESSION['user_email'];
} else {
$email = 'dww2@pitt.edu';
}
$link = mysql_connect('censcoredFool.com', 'greetmeet', 'Maverick$41');
mysql_select_db(first_1) or die("Opps, You are pretty Got-Damned Stupid! Did you realize that?!?!?");
$target = './Uploads/';
$target = $target . basename( $_FILES['uploaded']['name']);
$ok = 1;
$path = "uploads/";
$Email = $_SESSION['user_email'];
$valid_formats = array("jpg", "png", "gif", "bmp","jpeg");
if (isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST") {
$name = $_FILES['photoimg']['name'];
$size = $_FILES['photoimg']['size'];
if (strlen($name)) {
list($txt, $ext) = explode(".", $name);
if (in_array($ext,$valid_formats)) {
if ($size<(1024*1024)) { // Image size max 1 MB
$actual_image_name = time().$session_id.".".$ext;
$tmp = $_FILES['photoimg']['tmp_name'];
if (move_uploaded_file($tmp, $path.$actual_image_name)) {
mysql_query("insert into personal_photos (Email, Pics) values('$email', '$tmp')");
echo "<img src='uploads/".$actual_image_name."' class='preview'>";
} else {
echo "failed";
}
} else {
echo "Image file size max 1 MB";
}
} else {
echo "Invalid file format..";
}
} else {
echo "Please select image..!";
}
exit;
}
What I have doesn’t even seem to work. I get the success alert, but the picture doesn’t even upload. I am guessing the PHP script ran the whole way through but didn’t throw an exception or anything. How do I get this thing to work?
UPDATE: After running Firebug My PHP script isn’t getting the Picture
Not sure why that is. Need to figure out how the $_File datastructure works.
Most browsers won’t send files via ajax for security purposes. To have a pretend-ajax effect on uploads, you might try the excellent jQuery form plugin, which uses an hidden iframe to do the uploading.
Since this is the only answer so far – and it’s also right – allow me to give you some OT advice:
Most of the people using PHP like PHP (more things in Heaven and Earth, Horatio). Most of them will take your “PHP Sucks” thing as a pretty pretentious thing to say, since your problem has more to do with a lack of basic knowledge of Ajax and http implementations than it has to do with the language’s faults. Thus the lack of answers and the unwelcoming comments.
Your witty is my distracting: please make the debugging output in the code you post as descriptive as possible – actually, it would be a useful thing to do to simply write descriptive output anyway – it’s easier to find problems that way, trust me.
If a form is made of one hundred fields ond only one is relevant, please post the relevant field only.