Question

In: Computer Science

9.12) How do you write a PHP script that collects the data from the form provided...

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>

Solutions

Expert Solution

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


Related Solutions

Write a simple PHP script. In your script use all of the following: HTML, Javascript, and...
Write a simple PHP script. In your script use all of the following: HTML, Javascript, and PHP.
<?php # Script 10.5 - #5 // This script retrieves all the records from the users...
<?php # Script 10.5 - #5 // This script retrieves all the records from the users table. // This new version allows the results to be sorted in different ways. $page_title = 'View the Current Users'; line 6 include('includes/header.html'); echo '<h1>Registered Users</h1>'; require('mysqli_connect.php'); // Number of records to show per page: $display = 10; // Determine how many pages there are... if (isset($_GET['p']) && is_numeric($_GET['p'])) { // Already been determined.    $pages = $_GET['p']; } else { // Need to...
In this assignment you will write a PHP program that reads from a data file, creates...
In this assignment you will write a PHP program that reads from a data file, creates an associative array, sorts the data by key and displays the information in an HTML table. Create a PHP file called hw4.php that will do the following: - Display your name. - Read text data from a file. The data file is hw3.txt. The file hw3.txt will hold the following text: PQRParrot, Quagga, Raccoon DEFDo statements, Else statements, For statements GHIGeese, Hippos, If statements...
The MySQL script provided in chapter 4 (murachs php & mysql) creates and populates the DB...
The MySQL script provided in chapter 4 (murachs php & mysql) creates and populates the DB used in this application(s). A look at the tables shows no connection between parent and child tables. Provide the necessary SQL for enforcing data integrity between tables: categories and products. JUST NEED SQL FOR ENFORCING ??
Write a PHP script that checks a word or a phrase (stored in a string variable)...
Write a PHP script that checks a word or a phrase (stored in a string variable) to determine if it is a standard palindrome, a perfect palindrome, or not a palindrome at all. Also, for each letter that the word/phrase contains, count and print the number of times that each consonant and vowel is encountered. Your output will look as follows: Word/phrase: racecar Perfect palindrome Contains consonants: r - 2 c - 2 Contains vowels: a - 2 e -...
Q 3 b) In this question you must write a php script to take Name[input =...
Q 3 b) In this question you must write a php script to take Name[input = text], Country[input = select, card_number[input = number], and type of card[input = radio button]to get user's input. The name of this file will be payment.php. Once the user submits the form; you must direct all the information to cust_info.html. The task is to keep a record in the cust_info.html of all the users who fill the forms and hit submit. the output must populate...
Write a PHP script to display in frequency all the words in the submitted paragraph. Convert...
Write a PHP script to display in frequency all the words in the submitted paragraph. Convert the string to an array of words. Count the number of times the words are used. Hint: array_count_values($arrayname) Sort the array in frequency order. Print the array to the screen. Hint: print_r($arrayname)
In a Servlet, how do you read HTML Form Data (data from the previous Web page)...
In a Servlet, how do you read HTML Form Data (data from the previous Web page) into the Servlet?
9.) Write a MATLAB script that will read the Pressure and Temperature columns from the provided...
9.) Write a MATLAB script that will read the Pressure and Temperature columns from the provided Excel file. Use a loop to calculate the linear best fit line for the data. You may only use the built-in functions sum, size, and length. Plot both the individual data points (red) and the fit line (blue) on the same graph. Include a title and axes labels.
Write a PHP script that: 1) Has an associative array with 10 student names and test...
Write a PHP script that: 1) Has an associative array with 10 student names and test scores (0 to 100). ie. $test_scores('John' => 95, ... ); 2)Write a function to find the Average of an array. Input is an array, output is the average. Test to make sure an array is inputted. Use ARRAY_SUM and COUNT. 3)Output the test scores highest to lowest, print scores above the average in Green. Find a PHP Sort function for associative arrays, high to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT