0

I have a drupal-7 website and I created a module, where you insert an image.
This is the code for this form at the .module file

function testform($form, &$form_state) {
$form = array();
$form['file'] = array(
    '#type' => 'file',
    '#id' => 'files',
    '#title' => t('Select image'),
    '#description' => t('Upload a file, allowed extensions: jpg, jpeg, png, gif'),
    '#upload_validators' => array('file_validate_is_image' => array())
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
return $form;
}

Also, in my module-template.tpl.php file, I have created a form, that gives a preview of the image you submitted in it. This is the code I use:

<form id="form1" runat="server">
<input type='file' id="imgInp" />
<img id="blah" src="#" alt="your image" />
</form>



<script>
function readURL(input) {

if (input.files && input.files[0]) {
    var reader = new FileReader();

    reader.onload = function (e) {
        $('#blah').attr('src', e.target.result);
    }

    reader.readAsDataURL(input.files[0]);
}
}

$("#imgInp").change(function(){
readURL(this);
});
</script>

I want, when you select an image at the testform, then the form1 to trigger and preview the image that you selected at the testform.
Any ideas?

Argiris A
  • 156
  • 2
  • 19
  • Where is the testform exactly? The HTML code has somewhere to be. – Charlotte Dunois Mar 28 '16 at 14:43
  • The testform is at the module file. Both forms are shown at my page. The testform is called from the .module file, via drupal_render and the other one is created at the template.tpl.php file. I want to achieve the connection between those two forms. – Argiris A Mar 28 '16 at 14:46
  • Why do you have two forms, why do you try to select in one a file and then want a different form to trigger? What problem are you trying to solve with this? Looks like a [xy problem](http://xyproblem.info) to me. – Charlotte Dunois Mar 28 '16 at 14:52
  • My primary goal is to preview the image that I upload at the testform before I submit it. I tried many things but I couldn't achieve that. Then I found a theoretical solution. You manage it via another form, you connect them, and you hide the second one. My jquery-javascript knowledge is not that great though, so I can't make it work. – Argiris A Mar 28 '16 at 14:58
  • So this is a xy problem. I see you already have taken a look at this question http://stackoverflow.com/questions/4459379/preview-an-image-before-it-is-uploaded but I don't know what exactly happened that you think you need a different solution. The accepted answer clearly works. – Charlotte Dunois Mar 28 '16 at 15:02
  • Yes, this is the code I used. But the problem is that I want the form to be from the drupal module and use drupal function, not with plain html. – Argiris A Mar 28 '16 at 15:05

0 Answers0