Reputation: 63
How I can make PHP create a file for each order named whatever the person enters there name into the field
<?php
Name: php echo $_POST["Firstname"];
php echo $_POST["Lastname"]; <br /> <br />
Dorm Name: php echo $_POST["DormName"]; <br />
Room Number: php echo $_POST["RoomNumber"]; <br />
Pizza Type php echo $_POST["PizzaType"]; <br />
?>
This is what i have so far but I need a function that will make a file for each submission and name it the persons name.
Upvotes: 1
Views: 82
Reputation: 10061
your PHP should look something like this...
<?php
if ($_POST["submit"]) {
$firstName = $_POST["firstName"];
$lastName = $_POST["lastName"];
$dormName = $_POST["dormName"];
$roomNumber = $_POST["roomNumber"];
$pizzaType = $_POST["pizzaName"];
$fp = fopen($firstName . "_" . $lastName . ".txt", "a"); // the 'a' will append to the end of the file.
fwrite($fp, "\n\n");
fwrite($fp, "First Name: $firstName\n");
fwrite($fp, "Last Name: $lastName\n");
fwrite($fp, "Dorm Name: $dormName\n");
fwrite($fp, "Room Number: $roomNumber\n");
fwrite($fp, "Pizza Type: $pizzaType\n");
fclose($fp);
}
?>
<html>
<head>
<title>test</title>
</head>
<body>
<form method="POST" id="" action="">
<input type="text" name="firstName" value="" />
<input type="text" name="lastName" value="" />
<input type="text" name="dormName" value="" />
<input type="text" name="roomNumber" value="" />
<input type="text" name="pizzaName" value="" />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
Note: If another person with the same name orders again, it will all just append to the bottom of the same file.
Cheers
Upvotes: 1
Reputation: 2519
Try and have a read about fopen and fwrite.
Here's an example from the php codex:
$fp = fopen('data.txt', 'w');
fwrite($fp, '1');
fwrite($fp, '23');
fclose($fp);
// the content of 'data.txt' is now 123 and not 23!
So where that calls the file data.txt, you could call it a name based on $_POST['Firstname'] etc...
Upvotes: 0