Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Writing/Downloading Element Content

    Hi,

    I have no clue on how to program the following using JavaScript: I want the user to be able to click a button or link in order to download the content of a <div id="query"> as a .sql file. I assume the variable for the writing to be document.getElementById('query').textContent (or perhaps .innerHTML) but I have absolutely no idea what the command for writing and downloading should be, nor have I been able to find any good tutorial/explenation on that using Google (yes I've tried myself first)

    Thanks in advance,

    Venidrad

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,200
    Thanks
    75
    Thanked 4,342 Times in 4,308 Posts
    Hmmm... JavaScript isn't really the right answer for this.

    Much better to do this with server-side coding (PHP/ASP/JSP/etc.).

    That way you can have the server-side script *tell* the browser to download the file, rather than just display it.

    I think the best you could do with JS would be to copy the text to the clipboard (and now we are talking browser dependencies) so that you can then just paste it into a text file.

    This *COULD* be done with MSIE, assuming that the browser user is willing to give special (dangerous!) permissions to the web site in question. Normally, you would only do this in an inTRAnet situation, not inTERnet.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Right, well I'd like to do it in php, I prefer php over javascript (in terms of language structure) anyway, but the Javascript is responsable for writing in the <div> so I assume it's all on client side and there isn't an easy way to get it as a php variable in order to write it using php. Or do you know of one? A way to make php write content from a <div>?

  • #4
    Regular Coder
    Join Date
    Apr 2005
    Location
    Texas
    Posts
    448
    Thanks
    24
    Thanked 63 Times in 63 Posts
    Could you use AJAX to send the contents of the div to the php script maybe? (this would be in key/pair format, or XML of course)

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,200
    Thanks
    75
    Thanked 4,342 Times in 4,308 Posts
    Don't think you need or want AJAX, actually.

    You just want to send the <div> contents to PHP and then have PHP respond with a "download this file", where there's not really any file, just the <div> contents returned.

    I'm not enough of a PHP person to know the right stuff to respond with. I could show you how it would work with ASP, but then you'd have to translate it.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •