1

Am stuck here. I've been trying to upload some photos and at the same time pass the id of the album (value is in a hidden form in the form) to the same php script that processes the upload. but i dont know how to pass the album id seprately this is the code.

Js

input.addEventListener("change", function (evt) {
        document.getElementById("response").innerHTML = "<img src='../assets/admin/images/loading.gif' />"
        var i = 0, len = this.files.length, img, reader, file;

    for ( ; i < len; i++ ) {
        file = this.files[i];

        if (!!file.type.match(/image.*/)) {
            if ( window.FileReader ) {
                reader = new FileReader();
                reader.onloadend = function (e) { 
                    showUploadedItem(e.target.result, file.fileName);
                };
                reader.readAsDataURL(file);
            }
            if (formdata) {
                formdata.append("images[]", file);
            }
        }   
    }

    if (formdata) {
        $.ajax({
            url: "../assets/admin/ajaxupload/upload.php",
            type: "POST",
            data: formdata,
            processData: false,
            contentType: false,
            success: function (res) {
                document.getElementById("response").innerHTML = res; 
            }
        });
    }

PHP

//how do i retrieve the given album id value that was passed.

foreach ($_FILES["images"]["error"] as $key => $error) {
    if ($error == UPLOAD_ERR_OK) {
        $name = $_FILES["images"]["name"][$key];
        move_uploaded_file( $_FILES["images"]["tmp_name"][$key], "../../uploads/pics/" .$_FILES['images']['name'][$key]);


    }
}
echo "<p>Successfully Uploaded Images</p>";

Please i need a reply asap thanks.

2
  • 1
    Please i need a reply asap thanks. That's not how it works buddy. Commented May 10, 2012 at 9:44
  • and does this other "album ID" sit in a variable? Commented May 10, 2012 at 9:50

1 Answer 1

1

(sorry i have not big time to explain but hope help)

in your ajax php file

$errors = array();  // initialize empty error array
if (sizeof($errors) == 0) {
       ...
       if ($securimage->check($captcha) == false) {
            $errors['captcha_error'] = 'wrong code';
        }
 }
  if (sizeof($errors) == 0) {
        // no errors, send the form
        $return = array('error' => 0, 'message' => 'OK');
        die(json_encode($return));
    } else {
       $errmsg = '';
        foreach ($errors as $key => $error) {
            // set up error messages to display with each field
            $errmsg .= " - {$error}\n";
        }
        $return = array('error' => 1, 'message' => $errmsg);
        die(json_encode($return));
   }

your js

url: 'ajax.php',
        type: "POST",
        data: $('#formID').serialize(),
        success: function(msg) {
            try {
                //                    alert( "Data Saved: " + msg );
                json = jQuery.parseJSON(msg);
                //                      alert(json.error)
                if (json.error == 0) {
                } catch(e) {
                alert("Sorry, there was an error parsing the json");
            }
        },
        error: function(msg) {
            alert("Ajax request failed");
        }
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.