Form Submission

He has: 1,380 posts

Joined: Feb 2002

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.

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 Laughing out loud).

Good luck.

He has: 1,380 posts

Joined: Feb 2002

ok so i would do something like:

$fp = fopen ("/home/restricted/reportbug.txt", "w");
' and then what?
and also, how would i parse the contents of the form (written in HTML) to translate into variables or whatnot?

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.

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's picture

They have: 840 posts

Joined: Sep 2000

Post the validation code and the submission code...

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's picture

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

Laughing out loud

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's picture

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...

Laughing out loud

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);
'

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. Mad

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's picture

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).

He has: 1,016 posts

Joined: May 2002

Quote: Originally posted by Eskater05
sorry! geez...i was trying to write it myself...i'm a BEGINNER...some of the sites dont make sense to me yet!

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).

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.

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.

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.