loveranger Posted April 6, 2009 Share Posted April 6, 2009 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: [email protected] Number of Orders: 5 Priorty: Medium So what I need is code for both the form.html and process.php Quote Link to comment https://forums.phpfreaks.com/topic/152838-new-to-php-need-simple-code-for-form-to-be-saved-into-txt-file-in-server/ Share on other sites More sharing options...
Daniel0 Posted April 6, 2009 Share Posted April 6, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/152838-new-to-php-need-simple-code-for-form-to-be-saved-into-txt-file-in-server/#findComment-802643 Share on other sites More sharing options...
loveranger Posted April 6, 2009 Author Share Posted April 6, 2009 I also need the html code for the form please. I don't know how to do that. Quote Link to comment https://forums.phpfreaks.com/topic/152838-new-to-php-need-simple-code-for-form-to-be-saved-into-txt-file-in-server/#findComment-802758 Share on other sites More sharing options...
Prismatic Posted April 6, 2009 Share Posted April 6, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/152838-new-to-php-need-simple-code-for-form-to-be-saved-into-txt-file-in-server/#findComment-802762 Share on other sites More sharing options...
Daniel0 Posted April 6, 2009 Share Posted April 6, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/152838-new-to-php-need-simple-code-for-form-to-be-saved-into-txt-file-in-server/#findComment-802766 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.