Jump to content

[SOLVED] Yet again, more homework help... :/


Joe Sai

Recommended Posts

Ok, so I want to thank everyone for helping me on my assignment last week. I was so stumped, and after receiving help, everything was perfect. Thanks so very much for that!

 

However, now I have another assignment that is throwing me for a loop. What sucks about this class is that they automatically expect you to know this stuff, instead of really teaching it. I've read the material, but without the teaching, it's all greek to me.

 

My earlier assignments were laid out in my book as assignments, they pretty much told me how to start it and I had to fill in the blanks. This one just tells you what to make, without giving you anything, so I am trying my best and hoping I'm doing the right thing. This is the description:

 

You will create a Telephone Directory Application to save entries into a single text file.  Your HTML form will collect the following data items:

 

1.  Last Name

2.  First Name

3.  Street Address

4.  City

5.  State

6.  Zip

7.  Area code

8.  Phone Number

 

Your PHP program will take the data submitted by the form, using string functions assemble one record to be written to the file for each Telephone Directory entry that is submitted.

 

The data will be written in a comma delimited line of information terminated by a new line character and will be appended to the telephone data file. When multiple entries have been made to the form, the file should contain all the stored items on separate lines.

 

Be sure to ‘close’ the file when the data has been written.

 

In a new program you will open the file to ‘read the data’ and list all the items currently in the file. Your PHP program will present the data in a format that is easily readable, not in the manner it was ‘stored’. Provide a link or button to ‘run’ this program on the HTML form page.

 

I'm kind of confused at how to even begin this, but I started with the html file, which seems pretty good so far:

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Telephone Directory</title>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" href="php_styles.css" type="text/css" />
</head>
<body>
<h2>Enter your information into our telephone directory!</h2>
<form method="get" action="SignGuestBook.php">
<p>Last Name <input type="text" name="last_name" /></p>
<p>First Name <input type="text" name="first_name" /></p>
<p>Street Address <input type="text" name="street_address" /></p>
<p>City <input type="text" name="city" /></p>
<p>State <input type="text" name="state" /></p>
<p>Zip <input type="text" name="zip" /></p>
<p>Area Code <input type="text" name="area_code" /></p>
<p>Phone Number <input type="text" name="phone_number" /></p>
<p><input type="submit" value=" Submit" /></p>
</form>
<p><a href="ShowData.php">Show Data</a></p>
</body>
</html>

 

I have no problems with that, it's just the php file that I'm not sure about, and more importantly, getting the two to function together. This is all I've come up with as far as the php goes, and it really isn't doing anything for me at all, so I'm not even sure if I need to redo it completely different or what:

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Telephone Directory</title>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1" />
</head>
<body>
<?php
if (is_writable("telephonedirectory.txt")) {
echo "<p>The following visitors have enetered our telephone directory:</p><pre>";
readfile("telephonedirectory.txt");
echo "</pre>";
}
else
echo "<p>Cannot write to the file.</p>";
?>

<?php
if (empty($_GET['first_name']) || empty($_GET['last_name'])) || empty($_GET['street_address'])) || empty($_GET['city'])) || empty($_GET['state'])) || empty($_GET['zip'])) || empty($_GET['area_code'])) 
|| empty($_GET['phone_number']))
echo "<p>You must enter all information! Click your browser's Back button to return to the Telephone Directory form.</p>";
else {
$FirstName = addslashes($_GET['first_name']);
$LastName = addslashes($_GET['last_name']);
    $StreetAddress = addslashes($_GET['street_address']);
    $City = addslashes($_GET['city']);
    $State = addslashes($_GET['state']);
    $Zip = addslashes($_GET['zip']);
    $AreaCode = addslashes($_GET['area_code']);
$TelephoneDirectory = fopen("telephonedirectory.txt", "a");
if (is_writable("guestbook.txt")) {
	if (fwrite($TelephoneDirectory, $LastName . ", " . $FirstName . "\n"))
		echo "<p>Thank you for signing our Telephone Directory!</p>";
	else
		echo "<p>Cannot add your entry to the diresctory.</p>";
}
else
	echo "<p>Cannot write to the file.</p>";
fclose($TelephoneDirectory);
}
?>

</body>
</html>

 

I'd appreciate as much help as I can get, this class is stressing me out like crazy.

Link to comment
Share on other sites

the first thing to fix is your syntax errors.  turn on error reporting and make sure your parenthesis match up

 

Heck, I don't even know if the code is even do what the assignment asks of me, which is why I haven't fixed the parenthesis or anything ye. :/

 

It's kind of ridiculous to me that they give you instructions and expect you to do it all from scratch when this is the first PHP class I have ever taken in the history of my life.

Link to comment
Share on other sites

yeah, as lonewolf said... on this line (if (empty($_GET....) there are extra closing parantheses... also, you use telephonedirectory.txt and guestbook.txt. whatever it is make sure that is consistent... also, make sure your file is writable. chmod it to 777 if you can.

Link to comment
Share on other sites

It's kind of ridiculous to me that they give you instructions and expect you to do it all from scratch when this is the first PHP class I have ever taken in the history of my life.

 

Regardless if you dont know exactly what your code does, your syntax needs to be correct so you can take it one step at a time to make sure each part works.  That is how you debug code, not posting it to a site and hoping for a magic response

Link to comment
Share on other sites

i tested your code, and it wrote first and last name (as you have it) to the text file... i did have to remove the extra parantheses, changed guestbook.txt to telephonedirectory.txt, and made the file writable. and it worked just fine for me.

 

you are on the right track!

Link to comment
Share on other sites

i tested your code, and it wrote first and last name (as you have it) to the text file... i did have to remove the extra parantheses, changed guestbook.txt to telephonedirectory.txt, and made the file writable. and it worked just fine for me.

 

you are on the right track!

 

Will you please paste the code so I can work with that? I'm so confused, and I'd really appreciate to have that to work with. Thanks so much!

Link to comment
Share on other sites

Data Entry Form:

Change the METHOD to POST, instead of GET.

-Reason, GET appends the data to the url, the url has a limitation on length.  You might not hit this, but for this type of entry, I recommend using POST.  You will access the variables using $_POST instead of $_GET on the target page

 

Data "processing" page:

-Where ever the form points to, first validate the data (empty, valid characters, etc) rather than checking if the file you will be writing to is available.  If the data isn't good or complete, there's no need to check if the file is ready.

 

-Validate your inputs.  (unless this is not part of the class).  Check that names only contain word characters, phone numbers only #s, and maybe () and -s, streets have #s and letters, etc, zip only #s...  You can use regular expressions if you've learned these.

 

-Sanitize your inputs.  You have addslashes which is "ok".  There are other methods as well that you might look into: htmlentities, filter_var, [m]strlen[/n] also read this (though it's a bit beyond your scope) by Daniel0

 

Storing the data

from what i saw, seems to be ok.. maybe add in all the } { for your if else statements (your preference though) to make it more readable

-Also, make sure the user data doesn't contain something that would mess up the CSV format.  (ie: including a comma in their name or address - validate that out)

 

Read the data

Should probably be on it's own page

-What you have doesn't fulfill the assignment, but we'll get to that part after you're good on storing the data.

Link to comment
Share on other sites

lonewolf217 is partly right imo... but it is a php HELP forum. without posting problem code, it would be very difficult to effectively help. but i do agree that you shouldn't come here to have others write your code...

 

anyway i just changed this line, so you don't get the error... do you have errors turned on btw?

 

if (empty($_GET['first_name']) || empty($_GET['last_name']) || empty($_GET['street_address']) || empty($_GET['city']) || empty($_GET['state']) || empty($_GET['zip']) || empty($_GET['area_code']) || empty($_GET['phone_number']))

 

and changed the guestbook.txt reference to telephonedirectory.txt. and make sure the file is writable, or the directory if the file isn't there.

Link to comment
Share on other sites

I don't feel they are asking too much, if you have covered the absolute basics already, such as syntax rules.  That's actually a pretty easy program to make, compared to how complex things can be.  There are plenty of hints in there for what functions to use to accomplish the task.

 

http://www.php.net/ is your best friend.  All you have to do is

 

http://www.php.net/somekeyword  and it will try to find the closest function relevant for you.  Then all you have to do is read what it does, how to use it (very easy to read explanation and examples).  Just read the assignment carefully.  Break it down into individual tasks. Sum it up into keywords.  use them with php.net.  Also, it might help to tell you that you can do this in 40-60 lines of code.  Maybe less, depending on how pretty you want to make it. 

 

 

Link to comment
Share on other sites

Well I definitely didn't come here to have someone write the code for me, I just wanted help. Hence why I made what I already have. I just wanted to know if I was on the right track, and if not, how to even begin.

 

Sounds like I just have to fix some errors and we can get this going, so that is exciting. I'll try that website and what you said, but I just get frustrated because I feel so illiterate when I'm reading this sort of thing. I've spent hours reading my textbooks and never grasped a single thing from it, and it makes me want to pull all of my hair out. I just wish the class was a bit more basic for being my very first PHP class, but this was the only one they offered. :/

 

Let me fix these errors you recommended and I'll post again here in a few if I have more problems. I just need help fixing the problems, and need validation that what I'm doing fulfills those directions, because it's all Greek to me and I don't even know, to be honest.

Link to comment
Share on other sites

I have to disagree with some of what you posted xtopolis. The assignment does not ask for him to validate or sanitize anything.  Don't make it more complicated than it needs to be.  Yes, in a real world application, it needs to be considered, just like a bunch of other things not listed in the assignment.  But your advice applies even to this: don't take it all in big chunks. 

Link to comment
Share on other sites

Ok, I'm back with more questions, but I want to first thank everyone so very much, because I feel like I am actually getting somewhere now, and I couldn't have done it without your help.

 

This is my code now:

 

HTML file:

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Telephone Directory</title>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" href="php_styles.css" type="text/css" />
</head>
<body>
<h2>Enter your information into our telephone directory!</h2>
<form method="get" action="TelephoneDirectory.php">
<p>Last Name <input type="text" name="last_name" /></p>
<p>First Name <input type="text" name="first_name" /></p>
<p>Street Address <input type="text" name="street_address" /></p>
<p>City <input type="text" name="city" /></p>
<p>State <input type="text" name="state" /></p>
<p>Zip <input type="text" name="zip" /></p>
<p>Area Code <input type="text" name="area_code" /></p>
<p>Phone Number <input type="text" name="phone_number" /></p>
<p><input type="submit" value=" Submit" /></p>
</form>
<p><a href="ShowData.php">Show Data</a></p>
</body>
</html>

 

Form PHP:

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Telephone Directory</title>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1" />
</head>
<body>
<?php
if (is_writable("telephonedirectory.txt")) {
echo "<p>The following visitors have enetered our telephone directory:</p><pre>";
readfile("telephonedirectory.html");
echo "</pre>";
}
else
echo "<p>Cannot write to the file.</p>";
?>

<?php
if (empty($_GET['first_name']) || empty($_GET['last_name']) || empty($_GET['street_address']) || empty($_GET['city']) || empty($_GET['state']) || empty($_GET['zip']) || empty($_GET['area_code']) || empty($_GET['phone_number']))
echo "<p>You must enter all information! Click your browser's Back button to return to the Telephone Directory form.</p>";
else {
$FirstName = addslashes($_GET['first_name']);
$LastName = addslashes($_GET['last_name']);
    $StreetAddress = addslashes($_GET['street_address']);
    $City = addslashes($_GET['city']);
    $State = addslashes($_GET['state']);
    $Zip = addslashes($_GET['zip']);
    $AreaCode = addslashes($_GET['area_code']);
$TelephoneDirectory = fopen("telephonedirectory.html", "a");
if (is_writable("telephonedirectory.html")) {
	if (fwrite($TelephoneDirectory, $LastName . ", " . $FirstName . "\n"))
		echo "<p>Thank you for signing our Telephone Directory!</p>";
	else
		echo "<p>Cannot add your entry to the diresctory.</p>";
}
else
	echo "<p>Cannot write to the file.</p>";
fclose($TelephoneDirectory);
}
?>

</body>
</html>

 

ShowData PHP:

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Telephone Directory</title>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1" />
</head>
<body>
<?php
if (is_writable("telephonedirectory.html")) {
echo "<p>The following visitors have entered our Telephone Directory:</p><pre>";
readfile("telephonedirectory.html");
echo "</pre>";
}
else
echo "<p>Cannot write to the file.</p>";
?>
</body>
</html>

 

Ok, now here's my problems. When I fill out the form, everything goes through fine, except it always says, "cannot write to the file" on the top, even when everything worked fine. How do I go about fixing this?

 

My other problem is when I push the "Show Data" button on the php page, it's supposed to list everyone that has filled out the form. But instead it just displays the entire HTML form instead of just the submitees.

 

I'd greatly appreciate some help with this, thanks!

Link to comment
Share on other sites

so the file in not writable.... does the file exist? if not, create it... change permissions, if you can, so anyone can write to it....

 

also, you changed the filename to telephonedirectory.html, which technically will not hold any html data... though it really doesn't matter, it helps to have a proper file extension, hence .txt if it is just going to house textual data.

 

Link to comment
Share on other sites

so the file in not writable.... does the file exist? if not, create it... change permissions, if you can, so anyone can write to it....

 

also, you changed the filename to telephonedirectory.html, which technically will not hold any html data... though it really doesn't matter, it helps to have a proper file extension, hence .txt if it is just going to house textual data.

 

Ok, I'm a little bit confused.

 

I changed the names to telephonedirectory.html, because that's the name of my html file and I don't have any files named telephonedirectory.txt anywhere. Should I make it telephonedirectory.txt anyway?

 

And I'm not sure what you mean by not writable. I opened everything in my local server, and it came up fine, allowed me to fill out the form, and even saved the names inputted underneath the "show data" button.

 

All of the code I posted above are three separate html and php files I have saved in my localhost directory.

Link to comment
Share on other sites

yeah, the telephonedirectory.txt file will hold all your data and only the data you are collecting... should look something like this... with each line holding the data someone enters.

 

first,last,street,city,state,zip,area,phone

first,last,street,city,state,zip,area,phone

first,last,street,city,state,zip,area,phone

first,last,street,city,state,zip,area,phone

first,last,street,city,state,zip,area,phone

 

Link to comment
Share on other sites

yeah, the telephonedirectory.txt file will hold all your data and only the data you are collecting... should look something like this... with each line holding the data someone enters.

 

first,last,street,city,state,zip,area,phone

first,last,street,city,state,zip,area,phone

first,last,street,city,state,zip,area,phone

first,last,street,city,state,zip,area,phone

first,last,street,city,state,zip,area,phone

 

Ok, thanks. You're awesome. :D Lemme input this real quick and I'll post again in a second.

Link to comment
Share on other sites

Works like a charm, and I can't even begin to thank you enough. I was so stressed out, but I feel muuuuch more at ease now. THANK YOU! :D

 

I'm sorry for pestering you guys two weeks in a row. I have already been accused of posting my stuff just to get people to do my homework for me, but that is truly not the case. I never ask that the work is done for me, I just need some direction and help, as I am very new to this. I wasn't even sure how to start this assignment, but am very grateful for the help I received.

 

Thanks again, I can't even tell you how much I appreciate it. I'm going to try my best not to bother you guys with more of my problems, I still have 6 more weeks of this class, but if I do run into some trouble and need a little help, I will post and hope that you don't mind. Thanks so much!

Link to comment
Share on other sites

So, how many lines are you up to with this now? I counted about 100 in the 3-code post you posted earlier.  And you still have a bit more work to do.  At the very least I see that you need to make your output some level of pretty.  Judging by the rest of your code, I put you at about 125 lines.  That's not too bad for someone who claims to be a complete noob.  Once you finish and are not stressing anymore is the best time to go back and see if you can't streamline it a bit.  Look for patterns.  Do you see pieces of code that appear multiple times throughout your script? Good possibility it can be condensed. 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.