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 3 of 3
  1. #1
    New Coder
    Join Date
    Apr 2011
    Posts
    27
    Thanks
    1
    Thanked 0 Times in 0 Posts

    comment posting feature in my site

    I am trying to create a comments posting feature in my site using javascript
    However everytime i click on submit it shows the comment posted for a second and then the comment dissapears. It does not retain the comment posted. Please help.

    code is given below:

    Code:
     
    
    <html>
        <head>
            <script type="text/javascript">
                function post(){
                    document.getElementById("label").innerHTML=document.hello.posted.value;
                                  
                }
                
            </script>
        </head>
        <body>
            <form name="hello">
                <div id="label"> </div>
            <textarea name="posted">  </textarea>
            <input type="submit" value="Submit" onclick="post()">
            </form>
        </body>
    </html>

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,316
    Thanks
    203
    Thanked 2,565 Times in 2,543 Posts
    When you click on a submit button the page is refreshed, and reloaded once more. Change the type to "button". But I expect that you realise that only the writer of the comment can see it! If you want all you users to see the comments then you will require server-side scripting and a database.

    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    Banned
    Join Date
    Apr 2011
    Posts
    656
    Thanks
    14
    Thanked 69 Times in 69 Posts
    Code:
    function post(){                 
            document.getElementById("label").innerHTML=document.hello.posted.value;
    
    return false;
    
     }
    Code:
    <form name="hello" action="" method="post" onsubmit="return post();">
    Add the red lines.

    and take out the onclick

    Code:
    <input type="submit" value="Submit" >
    The above should submit the <form> and abort the default action to send the form data to the action's url after your label has been filled.


  •  

    Posting Permissions

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