In: Computer Science
9.12) How do you write a PHP script that collects the data from the form provided below and write it to a file?
.html
<!doctype html public "-//w3c//dtd html 4.0
transitional//en">
<html>
<head>
<title>Song Survey</title>
</head>
<body>
<form method="post" action="http://localhost/lab/lab
14.php"><br />
<h3>Enter The Date</h3>
<table>
<tr>
<td>Name Of The Song:</td>
<td><input type="text" name="SongName"
size="30"></td>
</tr>
<tr>
<td>Name Of The Composer:</td>
<td><input type="text" name="CName"></td>
</tr>
<tr>
<td>Name Of The Singer:</td>
<td><input type="text" name="Singer"></td>
</tr>
<tr>
<td><input type="submit"
value="Submit"></td>
</tr>
</table>
</form>
</body>
</html>
.php
<html>
<head>
<title>Song Information</title>
</head>
<body>
<?php
$SName=$_POST["SongName"];
$CName=$_POST["CName"];
$Singer=$_POST["Singer"];
$song_file= 'song.txt';
$file_handle = fopen($song_file, 'a') or die('Cannot open file:
'.$song_file);
fwrite($file_handle,$SName);
fwrite($file_handle,"#");
fwrite($file_handle,$Singer);
fwrite($file_handle,"##");
fwrite($file_handle,$CName);
fwrite($file_handle,"\r\n");
fclose($file_handle);
?>
<p>The Contents Are Written to the file
<u>song.txt</u></p>
<?php
$file_handle = fopen("song.txt", "rb");
print "<h4>The Most Popular songs are.....</h4>";
while (!feof($file_handle))
{
$line_of_text = fgets($file_handle);
$parts = explode(' # ',$line_of_text);
print ("$parts[0]<br/>");
}
fclose($file_handle);
?>
</body>
</html>
HTML FORM
Make sure that you enter the correct filename in action attribute of form in html page
Don't put space in between the name of the file and see that php file is in same directory where HTML file stored
Incase if your php file is stored at other directory make sure to add the proper directory path in action attribute along with filename
I placed the php file in same location where html file is saved and runned this script and it worked for me
Save the below file as filename.html
<!doctype html public "-//w3c//dtd html 4.0
transitional//en">
<html>
<head>
<title>Song Survey</title>
</head>
<body>
<form method="post" action="filename.php"><br
/>
<h3>Enter The
Date</h3>
<table>
<tr>
<td>Name Of The Song:</td>
<td><input type="text" name="SongName"
size="30"></td>
</tr>
<tr>
<td>Name Of The Composer:</td>
<td><input type="text"
name="CName"></td>
</tr>
<tr>
<td>Name Of The Singer:</td>
<td><input type="text"
name="Singer"></td>
</tr>
<tr>
<td><input type="submit"
value="Submit"></td>
</tr>
</table>
</form>
</body>
</html>
PHP script
Save the below file as filename.php
<?php
$SName=$_POST["SongName"];
$CName=$_POST["CName"];
$Singer=$_POST["Singer"];
$song_file= 'song.txt';
$file_handle = fopen($song_file, 'a') or die('Cannot open file:
'.$song_file);
fwrite($file_handle,$SName);
fwrite($file_handle,"#");
fwrite($file_handle,$Singer);
fwrite($file_handle,"##");
fwrite($file_handle,$CName);
fwrite($file_handle,"\r\n");
fclose($file_handle);
?>
<p>The Contents Are Written to the file
<u>song.txt</u></p>
<?php
$file_handle = fopen("song.txt", "rb");
print "<h4>The Most Popular songs are.....</h4>";
while (!feof($file_handle))
{
$line_of_text = fgets($file_handle);
$parts = explode(' # ',$line_of_text);
print ("$parts[0]<br/>");
}
fclose($file_handle);
?>
WORKING
when form submitted
song.txt file content