Jump to content

New to PHP- need simple code for form to be saved into .txt file in server


loveranger

Recommended Posts

Ok, I'm new to PHP and I wanted a code on how to save something from a form into another file called "logs.txt"

 

Form.html:

Name: [_______________]

Email: [________________]

Number of Orders: [_______________]

Priorty: [Low/Medium/High]

 

After the person has entered it, it will be saved in a file called logs.txt

 

logs.txt:

Name: Roger

Email: roger@roger.com

Number of Orders: 5

Priorty: Medium

 

So what I need is code for both the form.html and process.php

 

Link to comment
Share on other sites

Assuming you have made your form that has action="process.php", process.php could look like this:

 

<?php
// you might want to implement a more user friendly way of validating the values...

if (!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
throw new InvalidArgumentException('Invalid email address.');
}
if (strlen($_POST['name'] > 50) {
throw new LengthException('Name is too long. Max length is 50 characters.');
}
if (in_array($_POST['priority'], array('small', 'medium', 'high')) {
throw new InvalidArgumentException('Invalid priority.');
}

$numOrders = (int) $_POST['numOrders'];
$priority = ucwords(strtolower($_POST['priority']));

$info = <<<EOF
Name: {$_POST['name']}
Email: {$_POST['email']}
Number of Orders: {$numOrders}
Priority: {$priority}

EOF;

file_put_contents('logs.txt', $info);

 

It isn't tested, and you probably wouldn't want to use it verbatim, but it should give you an idea of how to do it.

Link to comment
Share on other sites

Assuming you have made your form that has action="process.php", process.php could look like this:

 

<?php
// you might want to implement a more user friendly way of validating the values...

if (!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
throw new InvalidArgumentException('Invalid email address.');
}
if (strlen($_POST['name'] > 50) {
throw new LengthException('Name is too long. Max length is 50 characters.');
}
if (in_array($_POST['priority'], array('small', 'medium', 'high')) {
throw new InvalidArgumentException('Invalid priority.');
}

$numOrders = (int) $_POST['numOrders'];
$priority = ucwords(strtolower($_POST['priority']));

$info = <<<EOF
Name: {$_POST['name']}
Email: {$_POST['email']}
Number of Orders: {$numOrders}
Priority: {$priority}

EOF;

file_put_contents('logs.txt', $info);

 

It isn't tested, and you probably wouldn't want to use it verbatim, but it should give you an idea of how to do it.

 

Wouldn't that just generate an uncaught exception error since you don't catch any exceptions.

Link to comment
Share on other sites

Wouldn't that just generate an uncaught exception error since you don't catch any exceptions.

 

Like I said, he shouldn't copy it verbatim and he should implement a more user friendly way of reporting form errors...

 

I also need the html code for the form please. I don't know how to do that.

 

Mind showing an effort yourself? If you want people to program the entire thing then post in the freelancing forum. We're here to help you, not to do your work for you.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.