Form Submission
Hi, everybody! I am just starting with PHP, and I know very little. The book that I have goes over how to submit info from a form via EMail, but not into a flatfile database. Thats all I want right now...it goes over how to submit into a MySQL database, but I dont have one. I cant seem to find it on the internet either. Could you guys help me? Thanks.
zollet posted this at 00:06 — 24th May 2002.
He has: 1,016 posts
Joined: May 2002
Hi Kyle,
Here are some links that might help you:
- http://www.php.net/manual/en/function.fopen.php (to open/create a file)
- http://www.php.net/manual/en/function.fwrite.php (to write to a file)
You will find all the information you need on those two pages. Just read through them and use common sence (open a file, then write to it ).
Good luck.
kb posted this at 00:11 — 24th May 2002.
He has: 1,380 posts
Joined: Feb 2002
ok so i would do something like:
$fp = fopen ("/home/restricted/reportbug.txt", "w");
and also, how would i parse the contents of the form (written in HTML) to translate into variables or whatnot?
zollet posted this at 00:20 — 24th May 2002.
He has: 1,016 posts
Joined: May 2002
It's very simple. In your HTML form, if you name a text box for example "name", then after you submit, the value of the text box "name" will be in the variable $name.
Here's a sample script..
<?php
$fp = fopen (\"/home/restricted/reportbug.txt\", \"w\"); //Open the file
fwrite($fp, \"Name...: $name\n\"); //Write a line to the file. \n = new line
fclose ($fp); //close the file
?>
Add as many fwrite() you want and don't forget \n means new line. You could write two or more lines in one fwrite(). For example:
fwrite($fp, "Name...: $name\nAddress....: $address\nCity...: $city\n");
I hope this helps.
kb posted this at 20:06 — 24th May 2002.
He has: 1,380 posts
Joined: Feb 2002
one more question...say i have a function that will validate the form that is being submitted...can i include this in the same PHP script?
nike_guy_man posted this at 20:23 — 24th May 2002.
They have: 840 posts
Joined: Sep 2000
Post the validation code and the submission code...
kb posted this at 20:37 — 24th May 2002.
He has: 1,380 posts
Joined: Feb 2002
heres the code
<?php
$fp = fopen ("/home/restricted/reportbug.txt", "w");
fwrite($fp, "First Name: $fname\n");
fwrite($fp, "Last Name: $lname\n");
fwrite($fp, "Email: $email\n");
fwrite($fp, "Month: $month\nDate: $date\nYear: $year\n");
fwrite($fp, "Description: $description\n");
fclose ($fp);
include "/php-bin/phpinclude.inc";
function validate_form()
{
global $fsname, $lname, $email, $month, $date, $year;
$errors=0;
if (!trim($fsname))
{
echo "<br><font color="#FF0000"><b>First Name</b> is required.";
$errors++;
}
if (!trim($lname))
{
echo "<br><font color="#FF0000"><b>Last Name</b> is required.";
$errors++
}
if (!trim($email))
{
echo "<br><font color="FF0000"><b>Email Address</b> is required.";
}
if (!trim($month))
{
echo "<br><font color="FF0000"><b>Month</b> is required.";
}
if (!trim($date))
{
echo "<br><font color="FF0000"><b>Date</b> is required.";
}
if (!trim($year))
{
echo "<br><font color="FF0000"><b>Year</b> is required.";
}
switch ($errors)
{
case 0:
return TRUE;
case 1:
echo "<br><br>Please go back and fill in the required fields.";
return FALSE;
default:
echo "<br><br>Please go back and fill in the required fields.";
return FALSE;
}
}
function update_database()
{
echo "<br>Thank your for reporting the bug to <i>1Eye Films</i>.";
}
$ok = validate_form()
if ($ok)
update_database();
?>
</body>
</html>
nike_guy_man posted this at 20:46 — 24th May 2002.
They have: 840 posts
Joined: Sep 2000
What is included in php-bin/phpinclude.inc??
Also, this will write to the DB even if it isn't valid... move the last bracket around the form addition part...
i'll work in fixing this, I hope
kb posted this at 20:54 — 24th May 2002.
He has: 1,380 posts
Joined: Feb 2002
well....the phpinclude.inc is a header(my nav bar)...which i am told will go at the top of the page...so...
nike_guy_man posted this at 22:04 — 24th May 2002.
They have: 840 posts
Joined: Sep 2000
Here ya go
<?php
function validate_form()
{
global $fsname, $lname, $email, $month, $date, $year;
$errors=0;
if (!trim($fsname))
{
echo \"<br><font color=\"#FF0000\"><b>First Name</b> is required.\";
$errors++;
}
if (!trim($lname))
{
echo \"<br><font color=\"#FF0000\"><b>Last Name</b> is required.\";
$errors++
}
if (!trim($email))
{
echo \"<br><font color=\"FF0000\"><b>Email Address</b> is required.\";
}
if (!trim($month))
{
echo \"<br><font color=\"FF0000\"><b>Month</b> is required.\";
}
if (!trim($date))
{
echo \"<br><font color=\"FF0000\"><b>Date</b> is required.\";
}
if (!trim($year))
{
echo \"<br><font color=\"FF0000\"><b>Year</b> is required.\";
}
switch ($errors)
{
case 0:
return TRUE;
case 1:
echo \"<br><br>Please go back and fill in the required fields.\";
return FALSE;
default:
echo \"<br><br>Please go back and fill in the required fields.\";
return FALSE;
}
}
function update_database()
{
echo \"<br>Thank your for reporting the bug to <i>1Eye Films</i>.\";
$ok = validate_form()
$fp = fopen (\"/home/restricted/reportbug.txt\", \"w\");
fwrite($fp, \"First Name: $fname\n\");
fwrite($fp, \"Last Name: $lname\n\");
fwrite($fp, \"Email: $email\n\");
fwrite($fp, \"Month: $month\nDate: $date\nYear: $year\n\");
fwrite($fp, \"Description: $description\n\");
fclose ($fp);
}
include \"/php-bin/phpinclude.inc\";
if ($ok)
update_database();
</body>
</html>
?>
See if that works...
kb posted this at 22:09 — 24th May 2002.
He has: 1,380 posts
Joined: Feb 2002
ok...thanks
now i have one more question...how could i use php to gather certain env variables and store it in a database...would i use
fp = fopen ("/home/restricted/memberdata.txt", "w");
fwrite($fp, "IP Address: $REMOTE_ADDR\n");
fclose ($fp);
zollet posted this at 00:10 — 25th May 2002.
He has: 1,016 posts
Joined: May 2002
Kyle,
Why don't you try things out first or search php.net's documentation and then if you still have problems, we will help you out. I really hate people that expect you to do the work for them.
kb posted this at 17:36 — 26th May 2002.
He has: 1,380 posts
Joined: Feb 2002
sorry! geez...i was trying to write it myself...i'm a BEGINNER...some of the sites dont make sense to me yet!
Mark Hensler posted this at 18:44 — 26th May 2002.
He has: 4,048 posts
Joined: Aug 2000
Kyle, the code you have will work for writting the info into a flat-file database (a simple ASCII file).
zollet posted this at 22:47 — 26th May 2002.
He has: 1,016 posts
Joined: May 2002
The thing is, if you had tried it out yourself first, you would have known that $REMOTE_ADDR contains the visitors IP address. You could have also tried searching on PHP.net for "env" which would have resulted in you finding this page (which explains everything you need to know about env). I don't mind helping people out, but I hate it when people want you to do the job for them (which to me it seemed as what you were doing with your last question).
kb posted this at 01:22 — 27th May 2002.
He has: 1,380 posts
Joined: Feb 2002
zollet...i know what environment variables are. i knew that $REMOTE_ADDR gets the IP Address, i was asking if that was the correct format...
Thanks, Mark.
zollet posted this at 08:14 — 27th May 2002.
He has: 1,016 posts
Joined: May 2002
Still, my point is that you did not try it out to see if it works or not before asking for our help.
Anyways, I didn't/don't want to start an argument.
kb posted this at 20:54 — 28th May 2002.
He has: 1,380 posts
Joined: Feb 2002
i didnt try it out because i dont have PHP support on my server yet...i will very soon, and i want to have things running almost immediately!
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.