...

View Full Version : Simple form processing script



jsolomon
04-04-2007, 12:19 AM
Hello everyone,

I apologize in advance for my first post being a request, and for my general programming ignorance. I think what I want to do is simple...though it may be more complicated than I suspect. I need a form processing script to take a text box entry and automaticaly print it as html on my webpage. The next time a user submits an entry into the text box, it will print below the first entry. Essentially I want a website that looks like the comments section of a blog. There will be one long list of entries, and one text box at the bottom to add to that list. So...how do I do this? Thanks in advance.

PS. To get a better idea of what I want: www.jssolomon.com This is what I'm trying to make. Right now, the submissions simply get emailed to me and I have to post them manually.

adamjthompson
04-04-2007, 03:20 AM
You'll need a mySQL database to do this the correct way. You could have the data written directly to the HTML page if you wanted, but that would be more prone to troubles.

This little tutorial should give you the basics of creating a simple mysql database and displaying the data: http://www.php-mysql-tutorial.com/

Len Whistler
04-04-2007, 04:12 AM
You would be best to set up a MySQL database as mentioned and the present mail form you have would be changed to enter the data into the database. The web page would then query the database.

This would be a very easy protect once you understand the basics of setting up the MySQL database and how to use PHP to query the database.

iLLin
04-04-2007, 05:50 AM
Yea its easy when you get it and fun :) Good Luck!!!

jsolomon
04-04-2007, 08:29 AM
Thank you all so much. Glad I get to learn something :). I'll be back if I can't get it done.

mabuc
04-04-2007, 11:09 AM
Hey buddy- if you want the data send in your email you can create it using the email function in PHP... without creating a database/

I'll give you a hint how to play the codes.
if you are using form.. you can use the $_POST function in the submit type of the form and if you are using the <a href></a> you can use the $_GET function with the help of this you can easily display and submit datas.

I have a questions? what would you like to do in your form?
1. Do you want the input data being submitted to your email or
2. you want it to save first in the database before submitting it in your email?

Ill give you some sample codes:
if($_POST['btsubmit']=='submit') {
}
I think you know this already...

YOur problem is very easy., I just want to know what do you want to do with it..

Thnks and Happy COding guyz

Nightfire
04-04-2007, 11:21 AM
He doesn't want it emailing. He wants it going into a db automatically. Did you read his original post?

jsolomon
04-06-2007, 09:24 PM
Follow up: I've setup a mysql database/ created a PHP script that seems to be semi-functional...some of the time. It works for the first few entries and then the query stops working.I can't quite figure out what's wrong. Here is my code:



<body>

<form name="form" method="post">
<input name="secret" type="text" />
<input name="send" type="submit" value="Submit" />
</form>

<?php
$dbhost = 'localhost';
$dbuser = 'XXXXXXX';
$dbpass = 'XXXXXXX';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
$dbname = 'XXXXXXX';
mysql_select_db($dbname);
if(isset($_REQUEST['send']))
{
$secret = $_REQUEST['secret'];
echo "$secret";
}
mysql_query("INSERT INTO secrets (secret) VALUES ('$secret')") or die ('Error, insert query failed');
$query = "SELECT secret FROM secrets";
$result = mysql_query($query);
while($row=mysql_fetch_array($result,MYSQL_ASSOC))
{
echo "Message :{$row['secret']} <br>";
}
mysql_close($conn);
?>
</body>



I have a feeling it is a problem with how I created the database...because I more or less guessed. Here is what I did.
1. Created a new database titled 'secret' from my hosting company's custom interface.
2. Entered the databsae through phpMyAdmin and created a new table labeled 'secrets' with number of fields set to 1.
3. I set varchar to 200.
4. Create and index on "1" columns.
-->I selected my field and set the size to 2.

And that is all. Is there anything else I should have done? Thanks in advance everyone.

iLLin
04-06-2007, 10:23 PM
Well this should be changed like so


if(isset($_REQUEST['send']))
{
$secret = $_REQUEST['secret'];
echo "$secret";
}
mysql_query("INSERT INTO secrets (secret) VALUES ('$secret')") or die ('Error, insert query failed');

//to this
if(isset($_REQUEST['send']))
{
$secret = $_REQUEST['secret'];
echo "$secret";
mysql_query("INSERT INTO secrets (secret) VALUES ('$secret')") or die ('Error, insert query failed');
}


Notice I put the query in your if isset... You do this as your only submitting if someone submits something, versus it would insert into db everytime the page was loaded.

Len Whistler
04-07-2007, 01:17 AM
I set varchar to 200.

I would consider maybe TEXT instead, good for 65,535 characters. VARCHAR holds only 255 characters and I notice one of your entries is about 170 characters. Someone might end up wanting to enter more than 255 characters.

jsolomon
04-07-2007, 04:44 AM
Ok, I have gotten that issue worked out. One final set of questions and I should be complete. Here is my code now:


<?php
$dbhost = 'localhost';
$dbuser = 'XXXXXXX';
$dbpass = 'XXXXXXX';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
$dbname = 'XXXXXXX';
mysql_select_db($dbname);
$ip = $REMOTE_ADDR;
if(isset($_POST['send']))
{
$secret = $_POST['secret'];
mysql_query("INSERT INTO secret (secret) VALUES ('$secret')") or die ('Error, insert query failed');
mysql_query("INSERT INTO secret (ip) VALUES ('$ip')") or die ('IP log failed');
}

$query = "SELECT secret FROM secret";
$result = mysql_query($query);
while($row=mysql_fetch_array($result, MYSQL_ASSOC))
{
print "<p>{$row['secret']}<hr /></p>";
}
mysql_close($conn);
?>I have two problems.
1. For some reason, the <hr> tag that I want to seperate each entry appears twice... You can see here: www.jssolomon.com/test.php (http://www.jssolomon.com/test.php) I cannot for the life of me figure out why that's happening.
2. I want the list to appear in reverse chronological order (the most recent entries on the top). Do I do that by rearranging the database or through PHP?

Gracias!

iLLin
04-07-2007, 05:28 AM
Well your inserting blank information



mysql_query("INSERT INTO secret (secret) VALUES ('$secret')") or die ('Error, insert query failed');
mysql_query("INSERT INTO secret (ip) VALUES ('$ip')") or die ('IP log failed');


You have two inserts there making two different rows. One row containts the secret text or w/e. The next row contains ONLY the ip so the field "secret" shows blank so thats where your 2nd hr is coming from. Combine the two inserts like this.


mysql_query("INSERT INTO secret (secret,ip) VALUES ('$secret','$ip')") or die ('Error, insert query failed');

Len Whistler
04-07-2007, 07:16 AM
2. I want the list to appear in reverse chronological order (the most recent entries on the top). Do I do that by rearranging the database or through PHP?

Gracias!


You would use MySQL and PHP. Create a TIMESTAMP column and ORDER BY time, either DESC or ASC. TIMESTAMP will automatically be filled whenever a new record is added.

jsolomon
04-09-2007, 01:48 AM
Edit: I fixed this issue, so don't bother responding to this post. The problem, for anyone who cares, was that magic_quotes_runtime was set to on, so when I ran addslashes() to my database insert it added a second backslash. All fixy now, thanks.

Well, with your help I have made this: www.jssolomon.com

It pretty much does everything I wanted. There is one last issue that I can't seem to figure out. Whenever someone submits an entry, single quotation marks become /' and double quotes become /". I want to get rid of that forward slash... Here is the relevant code:
$insert = "INSERT INTO secret (secret,ip,time) VALUES ('$secret','$ip','$time')";
$select = "SELECT secret FROM secret ORDER BY timestamp DESC";
mysql_query($insert);
$result = mysql_query($select);
while($row=mysql_fetch_array($result, MYSQL_ASSOC))
{
print "<p>";
print htmlspecialchars($row['secret'], ENT_QUOTES);
print "<hr></p>";
}
}
}
mysql_close($conn);
?>

I know it has to do with the htmlspecialcharas line, though I don't know what to change. I tried changing
print htmlspecialchars($row['secret'])
to this

print htmlspecialchars($row['secret'], ENT_QUOTES);


But that didn't work. Any suggestions? By the way, feel free to contribute if you feel inspired :). Thanks in advance.

sirborder
04-09-2007, 04:54 AM
Just thought I'd tell you to never post your MySQL info on the web as you did in one of your posts.

Len Whistler
04-09-2007, 06:52 PM
I just tested it and it works very fast, good job. You might need to add pagnation otherwise you will end up with a very long page and maybe have the time displayed of each entry.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum