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.
Page 1 of 3 123 LastLast
Results 1 to 15 of 36
  1. #1
    New Coder
    Join Date
    Dec 2010
    Posts
    36
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question get DIV content to textarea !

    I'm try to get DIV content <textarea>
    i think we need to use some JavaScript here donno how to please help to do it
    when i click EDIT button it should send text to <textarea>

    Code:
    if (isset($_GET['edit']) && $_GET['edit'] == 'textupdate') {
     <textarea>  here i put that contain text content of div </textarea>
    }
    this fetch from mysql db and echo results
    Code:
    echo  " <div id=\"edit{$row['id']}\"> {$row['title']} </div> <a href= \"{$_SERVER['PHP_SELF']}?edit=textupdate\" id=\"edit{$row['id']}\" >Edit</a><br><br>\n" ;

  • #2
    New Coder
    Join Date
    Oct 2010
    Posts
    29
    Thanks
    0
    Thanked 2 Times in 2 Posts
    PHP isn't needed as far as I can tell. Try something like this:

    Code:
        <html>
    <head>
        <script>
        function movecontent(){
            document.getElementById("mydiv").innerHTML = document.getElementById("mytextarea").innerHTML;
        }
        </script>
    </head>
    <body>
        <div id="mydiv"></div>
        <textarea id="mytextarea"></textarea>
        <button onclick="movecontent()">Move Content</button>
    </body>
        </html>
    Haven't tested it, but it should work. When you click the button what ever is in the textarea will move to the div.

    For kicks you can even get rid of the button and change the texarea to this:
    Code:
    <texarea id="mytextarea" onkeyup="movecontent()"></textarea>
    Which will update the div every time a user presses a key in the textarea.
    Last edited by jmace; 12-23-2010 at 11:49 PM.
    PHP - MySQL - JavaScript - AJAX - HTML/XHTML/XML - CSS - Java - C++

  • #3
    New Coder
    Join Date
    Oct 2010
    Posts
    29
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Upon re-reading your post, I think I may have misunderstood. Are you trying to get the div contents to display in the textarea? If so, the javascript would be reversed:
    Code:
    document.getElementById("mytextarea").innerHTML = document.getElementById("mydiv").innerHTML
    Or something else? Please clarify a little more if this doesn't help. Maybe showing more of your code?
    Last edited by jmace; 12-23-2010 at 11:58 PM.
    PHP - MySQL - JavaScript - AJAX - HTML/XHTML/XML - CSS - Java - C++

  • #4
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts
    If you have ?edit=textupdate in the url why not fetch the content from the database instead of trying to call the info from the div, it also wouldn't work if you click the edit button as the page would reload and the div content will be lost.

    what you would need to do it something like this.

    The Javascript Code.
    Code:
    (function($){
      $.AJAX = function() {
        var xmlhttp;
        if(window.XMLHttpRequest) {
          xmlhttp = new XMLHttpRequest();
        } else {
          xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        return xmlhttp;
      };
      $.editContent = function(id1,id2) {
        var content  = document.getElementById(id1),
            textarea = document.getElementById(id2),
            AJAX     = new $.AJAX();
        if(content&&textarea) {
          if(content.style.display == 'none' | content.style.display === '') {
            AJAX.onreadystatechange = (function() {
              if(AJAX.readyState == 4 && AJAX.status == 200) {
                textarea.style.display = 'none';
                content.style.display  = 'block';
                content.innerHTML      = textarea.innerHTML;
                textarea.innerHTML     = '';
              }
            });
            AJAX.open("POST","http://www.example.com/AJAX.php",true);
            AJAX.setRequestHeader("Content-type","application/x-www-form-urlencoded");
            AJAX.send("ajax=true&type=editText&content="+textarea.innerHTML);
          } else {
            textarea.style.display = 'block';
            content.style.display  = 'none';
            textarea.innerHTML     = content.innerHTML;
            content.innerHTML      = '';
          }
        }
        return false;
      };
    })(window);
    Your Echo String (with edits).
    PHP Code:
    echo  " <div id=\"c_{$row['id']}\"> {$row['title']} </div>\n".
          
    " <a href= \"{$_SERVER['PHP_SELF']}\" onclick=\"editContent('c_{$row['id']}','e_{$row['id']}');\">Edit</a>\n".
          
    " <textarea cols=\"1\" rows=\"1\" style=\"width:100px;height:100px;display:none;\" id=\"e_{$row['id']}\"></textarea>"
    AJAX.php
    PHP Code:
    <?php
    if(isset($_POST['ajax'])) {
      if(
    $_POST['type'] == "editText") {
        
    $text $_POST['content'];
        
    // Do your mysql update here.
      
    }
    } else {
      die(
    'ERROR: None AJAX Request');
    }
    ?>
    Official BinPress hand picked coder.
    For anyone worried about SQL injection go have a look at my small yet powerful script here.
    Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
    I am 1 of 65,608 people to get a Pebble Watch :P

  • #5
    New Coder
    Join Date
    Dec 2010
    Posts
    36
    Thanks
    1
    Thanked 0 Times in 0 Posts
    thank you so much works gr8 !
    how can i remove button and update on function call is it possible ?

    Code:
     <html>
    <head>
        <script>
        function movecontent(){
            document.getElementById("mydiv").innerHTML = document.getElementById("mytextarea").innerHTML;
        }
        </script>
    </head>
    <body>
    <script>
      movecontent();
     divcontent();
        </script>
    function divcontent(){
        <div id="mydiv"></div>
        <textarea id="mytextarea"></textarea>
    }
    </body>
        </html>

  • #6
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts
    Quote Originally Posted by simzam View Post
    how can i remove button and update on function call is it possible
    If you look at the code I wrote you will see that it uses AJAX to communicate with the server scripts and update on finish.
    Official BinPress hand picked coder.
    For anyone worried about SQL injection go have a look at my small yet powerful script here.
    Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
    I am 1 of 65,608 people to get a Pebble Watch :P

  • #7
    New Coder
    Join Date
    Dec 2010
    Posts
    36
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I'm not familiar with Ajax how to use with php any thing need to install or it works like java script ?

  • #8
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts
    To run an AJAX request the only thing you need is the AJAX function in my code and then you use the AJAX send codes in the other function and that is all you need, nothing needs to be installed and AJAX is a Javascript library so it's already there for you to use.
    Official BinPress hand picked coder.
    For anyone worried about SQL injection go have a look at my small yet powerful script here.
    Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
    I am 1 of 65,608 people to get a Pebble Watch :P

  • #9
    New Coder
    Join Date
    Dec 2010
    Posts
    36
    Thanks
    1
    Thanked 0 Times in 0 Posts
    when i HIT Edit it pass through this .i wanted to know how to call movecontent() i have question why i need onclick when i already have IF statement and that work pretty good i want call movecontent() immediate pass of if statement i try ed this but it wouldn't work it never called


    Code:
    echo "<script> \n" ;
    echo "movecontent(); \n" ;
    echo " } \n" ;
    echo " </script>\n" ;
    Code:
    if (isset($_GET['edit']) && $_GET['edit'] == 'textupdate'){
    echo "<script> \n" ;
    echo " function movecontent(){  \n" ;
    echo " document.getElementById(\"textarea12\").innerHTML = document.getElementById(\"$divid\").innerHTML; \n" ;
    echo " } \n" ;
    echo " </script>\n" ;
    echo  "Insert Row: <br> <textarea rows=\"1\"  cols=\"60\" name=\"quote1\"  id= \"textarea12\" wrap=\"physical\" > </textarea> \n" ;
    }
    Last edited by simzam; 12-24-2010 at 04:38 PM.

  • #10
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts
    Do this.

    PHP Code:
    if (isset($_GET['edit']) && $_GET['edit'] == 'textupdate'){
    echo 
    "<script type=\"text/javascript\"> \n".
         
    "window.onload=(function(){document.getElementById(\"textarea12\").innerHTML = document.getElementById(\"{$divid}\").innerHTML;}); \n".
         
    "</script>\n".
         
    "Insert Row: <br> <textarea rows=\"1\"  cols=\"60\" name=\"quote1\"  id= \"textarea12\" wrap=\"physical\" ></textarea> \n";

    Official BinPress hand picked coder.
    For anyone worried about SQL injection go have a look at my small yet powerful script here.
    Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
    I am 1 of 65,608 people to get a Pebble Watch :P

  • Users who have thanked DJCMBear for this post:

    simzam (12-24-2010)

  • #11
    New Coder
    Join Date
    Dec 2010
    Posts
    36
    Thanks
    1
    Thanked 0 Times in 0 Posts
    thanks A lot ! it works Like Char M!

  • #12
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts
    No problem if you have any more problems just post here and I'll help.
    Official BinPress hand picked coder.
    For anyone worried about SQL injection go have a look at my small yet powerful script here.
    Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
    I am 1 of 65,608 people to get a Pebble Watch :P

  • #13
    New Coder
    Join Date
    Dec 2010
    Posts
    36
    Thanks
    1
    Thanked 0 Times in 0 Posts
    hay when page reload or i click on link <textarea> set to default value
    Is there any way if page reload or link clicked text remembers previous value
    How to tackle this

    need to create session for that ?
    or java scripts helps ?

  • #14
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts
    Quote Originally Posted by simzam View Post
    hay when page reload or i click on link <textarea> set to default value
    Is there any way if page reload or link clicked text remembers previous value
    How to tackle this

    need to create session for that ?
    or java scripts helps ?
    What do you mean?

    Can you elaborate abit.
    Official BinPress hand picked coder.
    For anyone worried about SQL injection go have a look at my small yet powerful script here.
    Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
    I am 1 of 65,608 people to get a Pebble Watch :P

  • #15
    New Coder
    Join Date
    Dec 2010
    Posts
    36
    Thanks
    1
    Thanked 0 Times in 0 Posts
    i mean when i click on EDIT it refresh page and textarea set to default value and its none it didn't remember previous value


  •  
    Page 1 of 3 123 LastLast

    Posting Permissions

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