Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2007
    Posts
    17
    Rep Power
    0

    How to add an 'add comment' form to my site?


    I am trying to design an 'add comment' form to my site that will allow users to post comments on my site. I have written the form, and the 'add comment' is a javascript link, the form is all HTML. My question is how does this form create a dynamic html page with the comment added? I have the code below:

    Code:
    <a href="javascript:{ document.getElementById('addcomment').style.display = 'block'; void(0); }">add a comment</a>
    
    <div style="display: none;" id="addcomment">
    <form class="comment" method="post">
    <label for="comment_name">name:</label>
    <input name="comment_name" size="25" value="" type="text"/>
    <label for="comment_comment">comment:<br /></label>
    </label><textarea name="comment_comment" rows="7" cols="30"></textarea><br/>
    <input value="post comment" type="submit"/><br/></form></div>
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Feb 2004
    Posts
    2,102
    Rep Power
    1767
    Do you have PHP on the server?

    to write a file/page requires a server side package...php is probably the easiest for a novice to write an application as you are wanting to do...(javascript is a client side language)

    if You do have PHP running all you need is a simple "flat file" php script... about 3-4 short lines of easy to understand php code...
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2007
    Posts
    17
    Rep Power
    0
    I havn't created the server yet, I was going to use a LAMP setup. I guess the thing I am confused about it where does the PHP go, directly on my HTML?

    The reason I ask this is because when I viewed the source code of a page i liked, and wanted to add the same comment form, he didn't seem to have any PHP in it.

    the link to the site i am referring to doesn't have php on the
    html, he has exactly what i posted.

    he has an 'add comment' to his blog which is exactly what i want to do. what would i need to add for a webserver to 'read' my comments and then create a 'page/file' to display them forever? tks.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Feb 2004
    Posts
    2,102
    Rep Power
    1767
    LAMP

    L-inux
    A-pache
    M-Ysql
    P-HP


    PHP is a module/Library package that installs along side of Apache (or other http server)... all PHP is executed on the server and an end user will never see any PHP code...The browser doesn't read it. it only sees and displays the "output" of the php..the output can be plain or formatted including markup tags like HTML or XML

    just be sure to install the PHP packages... if you are configuring the server yourself you can use much more sophisticated application by installing MYsql or other relational database package.


    FWIW, I use a lot of simple "flat files scripts" for counters and access logging so I see what users are searching for on my sites...

    sorry if this does not apply to you...be aware that running a public web server from a residential Internet service connection is not advised for several worthy reasons...
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2007
    Posts
    17
    Rep Power
    0
    i appreciate the help. so basically the PHP i write in my HTML will never be seen when a client views my source code?

    also, does my code for the form look clean? I wasn't sure about the DOM (document.getElementID) that he used.. i assume all the javascript link does it open the form to be submitted, is this true?

    thanks.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Feb 2004
    Posts
    2,102
    Rep Power
    1767
    Personally I only use JavaScript where it is the best or only solution and I'm no expert...

    The only reason I am familiar with where forms use JavaScript is for validation etc...

    either part of your same HTML file or a sperate php file will be the "script" that takes the submitted data and writes it to a new file...

    To answer your question NO...no php code can be seen

    to use a separate script file just set your form's "action" as your script.php
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2007
    Posts
    17
    Rep Power
    0
    thanks for the help.

    I think the javascript line just creates a link, when u click that link it displays the form (i think that's all that line does).

    i was wondering if you could help me just do a simple php script that will send the data to the server (doesnt have to have working variables just a simple template).

    it appears that i need the (action=" ") command inside the form to do this, or create the script around the HTML form.

    what would u do?
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Feb 2004
    Posts
    2,102
    Rep Power
    1767
    The code below is one of the scripts I use as a log/counter:
    it really is self explanatory if you look at it...the first line is the path to the output file (the directory "/logs" must be writable to)

    the "fwrite()" lines tell it what information I want logged i.e., my form variables then a time/date stamp followed by the users ISP and their IP address then it close the file...

    PHP Code:
    <?php  
     $hFile 
    fopen"./logs/search.txt""a+" ); 
    fwrite$hFile"\n$city;$query;$category-"); 
    fwrite($hFile,date("l, F jS Y - H:i:s-").($ip $_SERVER['REMOTE_ADDR']));
    fclose$hFile ); 
    ?>
    what you might want is something like this:
    PHP Code:
    <?php  
    $hFile 
    fopen"comments.txt""a+" ); 
    fwrite$hFile"$comments); 
    fclose$hFile ); 
    ?>
    just make your form's name "comments"

    save the above code (just the way it is ) and call it comments.php

    set your form's actions as action="comments.php"

    if the directory where the script resides is writable the script will create a text file with whatever "comments" are entered in your forms input field when executed

    there are several way to display your "output"...

    If I was going to display user comments I would probably create an HTML file and just include (SSI or php) "comments.txt" a Style sheet can be used to format the included text...
    Last edited by tj_nt; November 11th, 2007 at 12:53 PM.
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2007
    Posts
    17
    Rep Power
    0
    thanks for all your help

    just a couple small questions.

    1) when u say to make the forms name "comments", how do i do this? name=" "

    2) once i do get this to work and i want to write a new .html file, will this comments.php file continously be updated everytime a new post is made changing the text?

    this is the first time i have used php\mysql, i am going to try to make a simple web server, using ubuntus.
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Feb 2004
    Posts
    2,102
    Rep Power
    1767
    sorry, I meant to say the form "input" should be "comments" although it won't hurt to name the form the same thing

    <input name="comments"> (in the form)

    will pass whatever is entered into the form field to the script as $comments

    The script I posted as an example should write each new submission on a new line (of the same file)

    This is not using MYsql...the examples I have been discussing are "flat files" (text) big difference

    Get a dev server set up with all the LAMP packages to where you can execute some code...you will get a much better understanding of what is going on when you can run some scripts...We wil be here when you are ready...
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2007
    Posts
    17
    Rep Power
    0
    i appreciate all the help. i am going to read a little more on HTML DOM. after that i am going to setup the LAMP, using ubuntus on a dell laptop to practice. i also bought a book entitled 'php & sql'. hopefully this should allow me to post comments dynamically to my website when all is said and done.

    im wondering if i should just do a paid 'php/sql' web hosting company to host the site until i gain a better understanding of php, what do think? i'm leaning towards the LAMP, since i have the laptop im not using for anything else.
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Feb 2004
    Posts
    2,102
    Rep Power
    1767
    I think getting a hosted account is the best idea...If you are not dead set on using Ubuntu...consider setting up your dev server with the same O/S and configuration as the hosted server.(same package version/editions etc...

    for administering LAMP servers one of the first apps you will want to become familiar with is phpMyAdmin this is a web based interface for the MYsql server...it will also give you php code for sql queries you run through it... it is a must have IMO!
  24. #13
  25. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2007
    Posts
    17
    Rep Power
    0

    form data not showing in my text file..


    I am using a form to display 2 fields, one for name and one for email. I am trying to write these values to a .txt file called input.txt. When I open the input.txt file it shows:

    'test$lastName$email'

    I want it to show 'test' followed by what the user enters for the 2 form fields. Here is my code for the 2 php files:

    Code:
    <?php
    			echo "<h4>Please enter this information:</h4>";
    			echo "<table>";
    			echo "<form action='input.php' method='POST'>
    			      <tr><td align='right'>Last Name:</td>
    			       <td><input type='text' name='lastName' value=' ' maxlength='65' size='40'></td>
    			      </tr>
    			      <tr><td align='right'>Email:</td>
    			       <td><input type='text' name='email' value=' ' maxlength='65' size='40'></td>
    			      <tr><td align='center'><p><input type='submit' value='Submit'></p></td>
    			      </tr>
    			      </form>";
    			echo "</table>";
    			?>
    and

    Code:
    <?php
    					      $ourFileName = "input.txt";  
    $ourFileHandle = fopen($ourFileName, 'a+');  
    $stringData = "test\n";
    fwrite($ourFileHandle, $stringData);
    fwrite($ourFileHandle, '$lastName' ); 
    fwrite($ourFileHandle, '$email' ); 
    fclose($ourFileHandle);    
    ?>
    any ideas?
  26. #14
  27. CSS & JS/DOM Adept
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jul 2004
    Location
    USA (verifiably)
    Posts
    20,127
    Rep Power
    4304
    That's a PHP question.

    In PHP, variables names embedded in strings delimited with single-quotes aren't evaluated like they are in strings delimited with double-quotes.

    Why are you making 3 calls to fwrite() anyway? Why not just concatenate the variables?
    Spreading knowledge, one newbie at a time.

    Check out my blog. | Learn CSS. | PHP includes | X/HTML Validator | CSS validator | Common CSS Mistakes | Common JS Mistakes

    Remember people spend most of their time on other people's sites (so don't violate web design conventions).
  28. #15
  29. Known to taste like chicken
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    In front of my computer
    Posts
    399
    Rep Power
    312
    also, on side note, it's not always wise to accept user input, and then display it without any form of sanitizing etc. It leaves you open to XSS exploits and the like. This becomes more important when you start dealing with SQL injections / Mail injections.

    Comments on this post

    • Kravvitz agrees
    Last edited by sir_drinxalot; December 1st, 2007 at 03:10 PM.
    "Take thy beak from out my heart, and take thy form from off my door" - Homer J Simpson / Edgar Allan Poe

    Looking for a project Idea?
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo