Welcome! Log In Create A New Profile

Advanced

Task 3B-1

Posted by Loop 
Announcements Last Post
Announcement myUnisa availability 21 to 24 March 2019 03/17/2019 02:24PM
Announcement SoC Curricula 09/30/2017 01:08PM
Announcement Demarcation or scoping of examinations and assessment 02/13/2017 07:59AM
Announcement School of Computing Short Learning Programmes 11/24/2014 08:37AM
Announcement Unisa contact information 07/28/2011 01:28PM
Task 3B-1
February 16, 2009 05:12PM
Hi,

I'm having a bit of a problem with the guestbook exercise in the masterskill manual (3-1 pg36)

I've typed out the code (just made a few of my own changes to the table layout) and all seems to be working fine, except for writing to the guest.txt file.
The code used to write to the guestfile is:
fwrite($fp, "<br><b>$firstname $lastname:</b> $comments<br>"winking smiley; (soz cant find where to turn off smileys)
however only the $firstname, $lastname and $comments variables don't get writen, so essentially all you see for the new entry is the ":"


Here is my code:

guestbookform.php
Language: PHP
<?php $title = "Webooks.com Guestbook"; include("header.php"); ?>   <h3>Webooks.com Guestbook:</h3> <form method='post' action='guestbook.php'> <table width="600" cellpadding="0" cellspacing="0" border="0"> <tr> <td>What is your first name:</td> <td><input name='firstname' type='text'></td> </tr> <tr> <td>What is your last name?</td> <td><input name='lastname' type='text'></td> </tr> </table> <br> What are your comments about this site?:<br> <textarea name='comments' rows='6' cols='45'></textarea> <br><br> <input type='submit'> </form>   <h3>Guestbook Entries</h3>     <?php include("guest.txt");   include("footer.php"); ?>

guestbook.php

Language: PHP
<?php $title = "Webooks.com Guestbook"; include("header.php");   $fp = fopen("./guest.txt", "a"); fwrite($fp, "<br><b>$firstname $lastname:</b> $comments<br>"); fclose($fp); echo "Your entry has been added to the guestbook!<br><br>"; echo "<a href='guestbookform.php'>Return to the guestbook</a>"; ?>
avatar
Mac
Re: Problem with apply your knowledge exercise
February 17, 2009 11:06AM
Have you read the handbook - have you got the handbook?? You need to "catch" the variables in guestbook.php first. PHP4 did that automatically for you (the manual is version 4), but as of version 5 (the handbook covers that) it does not. XAMPP runs version 5 - you can set it to run as 4 if you want, in which case your problem will go away. But work for version 5. So always read the manual together with the relevant chapters from the handbook - as shown in the tut letter.

Do look at this task topic in the previous years' forums. There is a lot of information there. Also remember to post your question under the topic number, which you need to create if ti does not exist yet.
Re: Task 3B-1
February 17, 2009 11:42AM
I do have the manual, and I thought it had to do with the fact that PHP5 doesn't allow global variables on the same level as PHP4, I actually tried switching XAMPP over to PHP4 using php-switch, but it was coming up with the same problem. Just checked XAMPP though and for some reason it says it is still using version 5 though.

I've managed to complete the hit counter successfully.

edit: Figured out: The PHP switch was changing it to PHP4.4 however the register_globals has been turned off since PHP4.2. I'm just gonna changed how the variables are passed on so that it works with PHP5
Re: Task 3B-1
February 17, 2009 01:10PM
ok finally got it working smiling smiley

I changed the variables as follows:

Language: PHP
<?php $title = "Webooks.com Guestbook"; include("header.php");   $first = $_POST['firstname']; $last = $_POST['lastname']; $text = $_POST['comments'];   $fp = fopen("./guest.txt", "a"); fwrite($fp, "<br><b>$first $last:</b> $text<br>"); fclose($fp); echo "Your entry has been added to the guestbook!<br><br>"; echo "<a href='guestbookform.php'>Return to the guestbook</a>"; ?>

and it now works fine in PHP5

Just out of curiosity, when I tried to use the imported variables directly in the fwrite function without first assigning them to new variables($first, etc) it wouldn't work. How come that happens?

And apologies for jumping a bit ahead of the schedule, I'm just really eager to learn how to do all these cool things smiling smiley
avatar
Mac
Re: Task 3B-1
February 17, 2009 01:30PM
If you don't have a name, then I can't call you smiling smiley

However, in the place of
$first = $_POST['firstname'];
$last = $_POST['lastname'];
$text = $_POST['comments'];
you could use
@extract($_POST)
in which case the $first, $last, $text variables are created automatically, and can be used without defining them. Useful when many variables BUT INSECURE IF NOT USED PROPERLY http://blog.php-security.org/archives/5-Tips-That-Every-PHP-Developer-Should-NOT-Know.html and here http://www.php.net/extract
No further explanation smiling smiley it's above this course.

You should however be able to use $_POST('first') in the place of $first?
Re: Task 3B-1
March 02, 2009 09:04PM
Yeah, global variables again. Also got this one working, with considerably less hassle than 3B-2.
Sorry, only registered users may post in this forum.

Click here to login