I am just beginning to play around with PHP and HTML and need help with writing form input to a text file.
<!DOCTYPE html>
<html>
<body>
Create Address Book Record
</br></br>
<form id="CreateAddress" action="CreateAddr.php" method="post">
User ID: <input type="text" name="User ID" /></br></br>
Name: <input type="text" name="Name" /></br></br>
Phone No.: <input type="text" name="Phone No." /></br></br>
Address: <input type="text" name="Address" /></br></br>
<input name="Create" type="submit" value="Create"/></br></br>
</form>
<a href='addrbook.txt'>Address Book</a></br>
</body>
</html>
CreateAddr.php:
<?php
include 'CreateAddrForm.html';
$file = fopen("addrbook.txt", "a+");
$status = false;
$data = '';
if (isset($_POST['User ID']) && isset($_POST['Name'])
&& isset($_POST['Phone No.']) && isset($_POST['Address']))
{
echo "hi";
$data = $_POST['User ID'] . "
" . $_POST['Name'] .
"
" . $_POST['Phone No.'] . "
" . $_POST['Address'] . "
";
echo $data;
$status = fwrite($file, $data);
if($status === false)
die('There was an error writing this file');
else
echo $status . "bytes written to file";
}
else
die('no post data to process');
?>
The outer if statement is never entered, the output is always "no data to process." Any help is much appreciated.
Please choose the name very carefully here in your code your Phone No. is the problem because the key of Phone No. in $_Post
array is Phone_No_
Avoid white spaces in your name it makes it a lot easier. I recommend to use Camel Case instead.
HTML :
**<!DOCTYPE html>
<html>
<body>
Create Address Book Record
</br></br>
<form id="CreateAddress" action="CreateAddr.php" method="post">
User ID: <input type="text" name="User ID" /></br></br>
Name: <input type="text" name="Name" /></br></br>
Phone No.: <input type="text" name="Phone No." /></br></br>
Address: <input type="text" name="Address" /></br></br>
<input name="Create" type="submit" value="Create"/></br></br>
</form>
<a href='addrbook.txt'>Address Book</a></br>
</body>
</html>**
PHP :
<?php
include 'CreateAddrForm.html';
$file = fopen("addrbook.txt", "a+");
$status = false;
$data = '';
if (isset($_POST['User_ID']) && isset($_POST['Name'])&& isset($_POST['Phone_No_']) && isset($_POST['Address'])) {
echo "hi";
$data = $_POST['User_ID'] . "
" . $_POST['Name'] .
"
" . $_POST['Phone_No'] . "
" . $_POST['Address'] . "
";
echo $data;
$status = fwrite($file, $data);
if($status === false)
die('There was an error writing this file');
else
echo $status . "bytes written to file";
}
else
die('no post data to process');
?>