PHP Make an HTML page that will take customer information. The fields should be
ID: 3756589 • Letter: P
Question
PHP
Make an HTML page that will take customer information. The fields should be “First Name”, “Last Name”, “phone number”, and “email address”. Name the html file information.html. the form should have a reset button and a submit button. The input fields for the form should be inside of a table. The submit button should send the information to the php file info.php. This file should check the data submitted by the html file for correct formatting. The first name and last name should be made up of only letters and at largest length of 20. The phone number should be all numbers, and an exact length of 10, (the area code plus number). Dashes should not be allowed. The email address can be of length at most 20, and should have an “@”, and made of only letters and numbers. Show an error message if any field has incorrect data, and show a link back to the information.html page. If all the fields are valid, the PHP should save the data into a file named shopper.txt which should have the following format, and be sorted by “Last Name”. (The existing file’s data has to be read, and new data should be added to the data structure, sort it before a new file is written in the proper order). Each record should be on its own line. Assume that last names are unique – use last name as a key for the associative array.
LastName:FirstName:123-456-7890:emailaddress
Format the phone number input to add dashes to the number.
Once the new data has been received, display all data that that was entered into the text file from the PHP page into an HTML table. The table to be sorted by last name just like the text file.
Explanation / Answer
//information.html
<form method="get" action="info.php">
<table border='1'>
<tr><td>First Name :</td><td> <input type="text" name="fname"></td></tr>
<tr><td>Last Name :</td><td> <input type="text" name="lname"></td></tr>
<tr><td>Phone Number:</td><td><input type="text" name="phone"></td></tr>
<tr><td>Email Add :</td><td><input type="text" name="email"></td></tr>
</table><br>
<input type="submit"><input type="reset">
</form>
//info.php
<?php
$fname = $_GET['fname'];
$lname = $_GET['lname'];
$phone = $_GET['phone'];
$email = $_GET['email'];
$f = 0;
echo "$fname $lname $phone $email";
if(!ctype_alpha($fname))
{
echo "Error : first name not contains letters";
$f=1;
}
if(!ctype_alpha($lname))
{
echo "Error : last name not contains letters";
$f=1;
}
if(strlen($fname)>20)
{
echo "Error : first name length larger than 20";
$f=1;
}
if(strlen($lname)>20)
{
echo "Error : last name length larger than 20";
$f=1;
}
if(!ereg("^[0-9]{10}$",$phone))
{
echo "Invalid phone";
$f=1;
}
if(strlen($email)>20)
{
echo "Error : Email Id length greater than 20";
$f=1;
}
if(!strpos($email,"@"))
{
echo "Error: @ not contains in Email";
$f=1;
}
if(!filter_var($email,FILTER_VALIDATE_EMAIL))
{
echo "Error Email: Sepcial symbol not allowed";
$f=1;
}
if($f==1)
{
echo "<a href="information.html">Go Back</a>";
}
else
{
$ph = substr($phone,0,3)."-".substr($phone,3,3)."-".substr($phone,6);
//echo $ph;
//$str = $lname.":".$fname.":".$ph.":".$email;
//echo $str;
$file = fopen("shopper.txt","r");
$fn = array();
$ln = array();
$p = array();
$em = array();
if(!$file)
{
echo "File Not Found";
}
else
{
//echo "File Found<br>";
$fh = file("shopper.txt");
while(list($no,$line) = each($fh))
{
//echo "$line<br>";
$temp = explode(":",$line);
$ln[] = $temp[0];
$fn[] = $temp[1];
$p[] = $temp[2];
$em[] = $temp[3];
}
$ln[] = $lname;
$fn[] = $fname;
$p[] = $ph;
$em[] = $email;
array_multisort($ln,$fn,$p,$em);
/*print_r($ln);
print_r($fn);
print_r($p);
print_r($em);*/
$ff = fopen('shopper.txt','w');
for($i=0;$i<count($ln);$i++)
{
fwrite($ff,$ln[$i].":".$fn[$i].":".$p[$i].":".$em[$i]);
}
fclose($ff);
}
}
?>
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.