How to Send an Image from the DOM, as Part of a Form

SHORT VERSION:

How do I attach an image object from the Document Object Model, using JavaScript, to a form so it can be sent to the server, without the user having to manually attach it using the input type=file tag?

Read More

Description:

I need a user to be able to look at a series of pics on a web page that were pulled in as the preview of a link he pasted, choose one, and have it automatically attach to a form, to be sent with text he wrote and processed by existing PHP as part of a new post, exactly as if he’d used an input type=”file” interface to attach it.

The problem is that the pic exists in the browser as part of the Document Object Model, and it needs to somehow become an attachment into his form, to submit with his text as a new post. I’ve tried making a hidden input and making its value equal to the image, but that seems not to work.

The solution can be in jQuery, or hand-coded, I’ve been using JavaScript for 18 years, so I can understand either one…I just don’t know how to attach a DOM object as a file to post to the server and process as part of a form.

Example Code:

This is not the actual code, which is complex and involves using JSON to pull a preview of a URL in PHP and send it back to the user, but it summarizes the problem:

<img id="image[0]" src="images/image0.jpg" onclick="attachimage(0)"/>
<img id="image[1]" src="images/image1.jpg" onclick="attachimage(1)"/>
<img id="image[2]" src="images/image2.jpg" onclick="attachimage(2)"/>
<form method="post">
    <input type="text" name="title"/>
    <textarea name="description"></textarea>
    <input type="hidden" name="theimage" id="theimage">
    <input type="submit" name="post" value="save">
</form>
<script>
    var attachimage = function(item) {
        // So far, nothing like this next line has worked for me,
        // the image never shows up in the saved post
        $("#theimage").val(document.getElementById("image[" + item + "]"));
    }
</script>

CONTEXT:
I am working on a WordPress website, using BuddyPress, to allow users to post their own links (a-la Digg and Reddit) without having Editor permission and using the Dashboard. I’m using a plugin called BuddyBlog (which uses bp-simple-front-end-post) to let users do this, which works fine…

But the owner also wants a preview to come up when they paste in a URL, just like it does on Facebook. I found nothing that already integrates the two features (user posts AND preview), so I pulled some open source code from the web that takes the URL, sends it via JSON to the server, which grabs the title, description, and images via PHP and sends the results back as a formatted HTML block. I then grab the values of those results and insert them into the BuddyBlog form fields…but BuddyBlog’s form anticipates the image coming to it via:

<input type="file" name="bp_simple_post_upload_0">

…and I don’t think I can simply set the value of bp_simple_post_upload_0 to be equal to the source of image[0]

Related posts

Leave a Reply

1 comment

  1. If you’ve already processed the images on the server and created the previews, it means they’re already there. So just pass in some variable representing which picture was selected and get the corresponding image. It’s already on the server.

    If the images are generated dynamically, with canvases or whatnot, you could send a base64 hash of them.