Var to input field

Discussion in 'JavaScript' started by Freske, Feb 18, 2018.

  1. #2
    Hello,

    This ia a code from Kcfinder
    
    function openKCFinder(div) {
        window.KCFinder = {
            callBack: function(url) {
                window.KCFinder = null;
                div.innerHTML = '<div style="margin:5px">Loading...</div>';
                var img = new Image();
                img.src = url;
                img.onload = function() {
                    // afbeelding en pad afbeelding
                    div.innerHTML = '<img id="img" src="' + url + '" />';
                    var img = document.getElementById('img');
                    var o_w = img.offsetWidth;
                    var o_h = img.offsetHeight;
                    var f_w = div.offsetWidth;
                    var f_h = div.offsetHeight;
                    if ((o_w > f_w) || (o_h > f_h)) {
                        if ((f_w / f_h) > (o_w / o_h))
                            f_w = parseInt((o_w * f_h) / o_h);
                        else if ((f_w / f_h) < (o_w / o_h))
                            f_h = parseInt((o_h * f_w) / o_w);
                        img.style.width = f_w + "px";
                        img.style.height = f_h + "px";
                    } else {
                        f_w = o_w;
                        f_h = o_h;
                    }
                    img.style.marginLeft = parseInt((div.offsetWidth - f_w) / 2) + 'px';
                    img.style.marginTop = parseInt((div.offsetHeight - f_h) / 2) + 'px';
                    img.style.visibility = "visible";
                }
            }
        };
     
        window.open('kcfinder/browse.php??lang=nl&type=files=files/public',
      'kcfinder_image', 'status=0, toolbar=0, location=0, menubar=0, ' +
          'directories=0, resizable=1, scrollbars=0, width=800, height=600'
       );
    }
    
    Code (markup):
    The rule
    div.innerHTML = '<img id="img" src="' + url + '" />';
    is the url from a chosen image if i am wright, is it posible this URL to put in a input field like,

    <input type="hidden" name="image_url" value="THe image URL" />
     
    Last edited by a moderator: Feb 18, 2018
    Freske, Feb 18, 2018 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,821
    Likes Received:
    4,539
    Best Answers:
    123
    Trophy Points:
    665
    #3
    I use it with ckeditor and it's gives you the url.

    <script>
     function openKCFinder(field) {
    
                window.KCFinder = {
                    callBack: function(url) {
                        var id = field.target.id;
    
                        $('#'+id).val(url);
    
                        var preview = '#preview' + id.substr(12);
                        console.log(id);
                        console.log(preview);
                        $(preview).attr('src', url);
    
                        window.KCFinder = null;
                    }
                };
                window.open('https://www.mysite.org.nz/js/kcfinder/browse.php?type=images&langCode=en', 'kcfinder_textbox',
                    'status=0, toolbar=0, location=0, menubar=0, directories=0, ' +
                    'resizable=1, scrollbars=0, width=800, height=600'
                    );
            }
    </script>
    PHP:
     
    sarahk, Feb 18, 2018 IP
  3. Freske

    Freske Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #4
    Thanks but i must store the value off the image url in a input field so i can store it in the database table

    This is not with CKeditor
    https://kcfinder.sunhater.com/demos/image


    By the way: how to make the code in a code field on this forum (your code in black background)
     
    Last edited: Feb 18, 2018
    Freske, Feb 18, 2018 IP
  4. sarahk

    sarahk iTamer Staff

    Messages:
    28,821
    Likes Received:
    4,539
    Best Answers:
    123
    Trophy Points:
    665
    #5
    • the code I pasted was from a standalone use of kcfinder - you'll see in the callback it puts the url into a input and then sets the source on a preview image. I was in a hurry so dumped and ran without explaining, sorry.
    • to wrap your code in bbcode use [ php ] and [/ php] but without the spaces.
     
    sarahk, Feb 18, 2018 IP
  5. Freske

    Freske Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #6
    I mean the IMAGE version
    https://kcfinder.sunhater.com/demos/image

    But there is no inputfield
    Is it posible the url to put in a value from a (hidden) inputfield

    after choose an image in the sourse off my webpage

    <div id="image" onclick="openKCFinder(this)">
    <img id="img" src="/upload/files/myimage.jpg" style="width: 200px; height: 134px; margin-left: 0px; margin-top: 33px; visibility: visible;"/>
    </div>

    So how to copy /upload/files/myimage.jpg to a value off a input field
     
    Last edited: Feb 19, 2018
    Freske, Feb 19, 2018 IP
  6. sarahk

    sarahk iTamer Staff

    Messages:
    28,821
    Likes Received:
    4,539
    Best Answers:
    123
    Trophy Points:
    665
    #7
    That's what I'm doing.
     
    sarahk, Feb 19, 2018 IP
  7. Freske

    Freske Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #8
    Thank, but where is the input field?
     
    Freske, Feb 19, 2018 IP
  8. sarahk

    sarahk iTamer Staff

    Messages:
    28,821
    Likes Received:
    4,539
    Best Answers:
    123
    Trophy Points:
    665
    #9
    In my case it's identified here
    
    var id = field.target.id;
    $('#'+id).val(url);
    
    Code (markup):
    change $('#'+id) to whatever you've called your input field.
     
    sarahk, Feb 19, 2018 IP
  9. Freske

    Freske Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #10
    Thanks for help , file stuped as a beginner

    Can you give a better sample for the input how to do.
    <input type="hidden"
    value="" />
     
    Freske, Feb 20, 2018 IP
  10. Blizzardofozz

    Blizzardofozz Well-Known Member

    Messages:
    132
    Likes Received:
    9
    Best Answers:
    1
    Trophy Points:
    118
    #11
    <input type="hidden" name="image_url" value="THe image URL" /> has nothing to do with the function you had posted.
    You have to make a form where to include this field and you get the url variable in the function as a function parameter so you should see where are you getting the url variable from and then include it in <input type="hidden" name="image_url" value="url" />

    or you could make a global variable:
    var urlStringForHiddenFIeld;
    function openKCFinder(div) {
    window.KCFinder = {
    callBack: function(url) {
    urlStringForHiddenFIeld = url;
    }
    }
    }
    <input type="hidden" name="image_url" value="urlStringForHiddenFIeld" />

    But in this case, you have to be sure that openKCFinder is called before the hidden field.
     
    Blizzardofozz, Feb 20, 2018 IP
  11. Freske

    Freske Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #12
    Thanks for help/
    Can you give a more full sample?
     
    Freske, Feb 21, 2018 IP