November 11th, 2007, 10:20 AM
How to add an 'add comment' form to my site?
<div style="display: none;" id="addcomment">
<form class="comment" method="post">
<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>
November 11th, 2007, 10:46 AM
Do you have PHP on the server?
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...
November 11th, 2007, 10:53 AM
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.
November 11th, 2007, 11:23 AM
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...
November 11th, 2007, 11:44 AM
i appreciate the help. so basically the PHP i write in my HTML will never be seen when a client views my source code?
November 11th, 2007, 12:03 PM
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
November 11th, 2007, 12:19 PM
thanks for the help.
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?
November 11th, 2007, 12:41 PM
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...
what you might want is something like this:
$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 );
just make your form's name "comments"
$hFile = fopen( "comments.txt", "a+" );
fwrite( $hFile, "$comments" );
fclose( $hFile );
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.
November 11th, 2007, 01:39 PM
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.
November 11th, 2007, 02:06 PM
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...
November 11th, 2007, 02:33 PM
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.
November 11th, 2007, 03:18 PM
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!
December 1st, 2007, 02:49 PM
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:
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:
echo "<h4>Please enter this information:</h4>";
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>
<td><input type='text' name='email' value=' ' maxlength='65' size='40'></td>
<tr><td align='center'><p><input type='submit' value='Submit'></p></td>
$ourFileName = "input.txt";
$ourFileHandle = fopen($ourFileName, 'a+');
$stringData = "test\n";
fwrite($ourFileHandle, '$lastName' );
fwrite($ourFileHandle, '$email' );
December 1st, 2007, 03:03 PM
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?
December 1st, 2007, 03:07 PM
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
Last edited by sir_drinxalot; December 1st, 2007 at 03:10 PM.