Welcome! Log In Create A New Profile

Advanced

Topic 5A

Posted by mauritz 
Announcements Last Post
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
Topic 5A
July 29, 2009 11:13PM
Done!

YAY! Learnt something new! I always wondered how you pass variables to a 3rd or 4th ... plus form page before processing it, and with the hidden form fields, magic really exist smiling smiley
Re: Topic 5A
August 11, 2009 09:43PM
a small prob , must be doing somrthin stupid , here is my code cant seem to get the name to display on the last page everything else works fine , any pionters to point my mistake

hers my code

BELOW IN RED IS THE PROB DOES NOT WANT TO DISPLAY
$fname=$_POST['firstname'];
$lname=$_Post['lastname'];
if (isset($fname,$lname))

{
echo "Thanks for responding to the survey $fname $lname <br><br>\n";
}




echo "<b>These are your favorite book categories:</b>";
echo "<br><br>";


THIS WORKS FINE BELOW
$choice= $_POST['choice'];
for ($i=0; $i<count($choice); $i++) {
echo "$choice[$i]<br>";
}

smoking smiley When all else fails , read instructions grinning smiley
smiling smiley Reading is a course on it's own ! smiling smiley
avatar
Mac
Re: Topic 5A
August 12, 2009 10:00AM
Do a GET method and see what is sent in the URL...
Re: Topic 5A
August 12, 2009 09:14PM
eish, i did the $_GET , the ISSET ,in the URL nothing is displayed , with isset the whole echo is gone , that means only one thing somewhere on the first page somethin wrong , eish well hopefully ill figure it out before Prac time

smoking smiley When all else fails , read instructions grinning smiley
smiling smiley Reading is a course on it's own ! smiling smiley
Re: Topic 5A
August 13, 2009 04:58PM
5A done!

Chilli, shouldn't your code

$fname=$_POST['firstname'];
$lname=$_Post['lastname'];

be

$fname=$_POST['fname'];
$lname=$_Post['lname'];
Re: Topic 5A
August 13, 2009 08:26PM
I dont know why these two variables dont display $fname $lname

echo "Thanks for responding to the survey $fname $lname <br><br>\n";

smoking smiley When all else fails , read instructions grinning smiley
smiling smiley Reading is a course on it's own ! smiling smiley
Re: Topic 5A
August 13, 2009 08:40PM
This one got me , i need help seriously

smoking smiley When all else fails , read instructions grinning smiley
smiling smiley Reading is a course on it's own ! smiling smiley
avatar
Mac
Re: Topic 5A
August 14, 2009 02:21PM
$fname=$_POST['firstname'];
$lname=$_Post['lastname'];

echo "$fname $sname"; // do some custom error checking - does this show? If not, you are not receiving it so the error lies on the first page

if (isset($fname) && isset($sname)) //change to this : && is for AND and || is for OR

{
echo "Thanks for responding to the survey $fname $lname <br><br>\n";
}

You can name it $fname=$_POST['firstname']; no prblem. You can also name it $my_cat_is_fat=$_POST['firstname'] if you really want to. You are declaring a variable and is giving it a name....

oh, and $_post must be $_POST.... in $lname
Re: Topic 5A
August 14, 2009 06:36PM
ok i will redo the first page and work my way through ....

smoking smiley When all else fails , read instructions grinning smiley
smiling smiley Reading is a course on it's own ! smiling smiley
Re: Topic 5A
August 15, 2009 06:42AM
Displaying a variable from the either the first to the second or second to third page is no problem.

Getting the the variable from the first page to the third is the problem , i don't see what my prob is , i did do the error checking and it points to the first page , the first page (form.php), did i not add something on the first page , well here is the source maybe another eye would see my stupid mistake:

<?php
$title = "WebBooks.com Reader Survey";
include("header.php" ) ;
?>
<form method=post action='hiddenfields.php'>
<b>WebBooks.com Reader Survey</b>
<br>
<br>
What is your first name?
<br>
<input name='firstname' type='text'>
<br>
<br>
What is your last name?
<br>
<input name='lastname' type='text'>
<br>
<br>
What is your age?
<br>
<input name='age' type='text'>
<br>
<br>
<input type='submit'>
</form>
<?php
include("footer.php" ) ;
?>

I cannot move to the next topic if i don't resolve this issue

smoking smiley When all else fails , read instructions grinning smiley
smiling smiley Reading is a course on it's own ! smiling smiley
Re: Topic 5A
August 16, 2009 07:51AM
To pass a variable from the first page to the third , you need to echo statement on the second page :

<?php

echo "<input name= 'fname' type= 'hidden' value= '$firstname'>";
echo "<input name= 'lname' type= 'hidden' value= '$lastname'>";
?>

to call the value on the third page :

$fname = $_POST['firstname'];
$lname = $_POST['lastname'];
echo "Thanks for responding to the survey $fname $lname <br><br>\n";


doing tests with
if (isset($fname) && isset($sname)) //change to this : && is for AND and || is for OR

{
echo "Thanks for responding to the survey $fname $lname <br><br>\n";
}

does not display anything.

echo a variable from the first to the second page works fine,second to third works , but from the first to the third does not .

i understand the concept of hidden fields , but now have to move on i am falling behind schedule and need to move on and complete topic 6 before we receive the prac sad smiley

i have search the previous forums , there was the same problem , how they resolved it does'nt say much drinking smiley

smoking smiley When all else fails , read instructions grinning smiley
smiling smiley Reading is a course on it's own ! smiling smiley
avatar
Mac
Re: Topic 5A
August 17, 2009 08:03AM
Rather post all your code here.
Anonymous User
Re: Topic 5A
August 17, 2009 12:44PM
Hi Chilli

Had the same issue. Try the following:

In hiddenfields.php, add this code:

Language: PHP
<?php $firstname = $_POST[';firstname';]; $lastname = $_POST[';lastname';]; // Then carry on as per the task echo "<input name=';fname'; type=';hidden'; value=';$firstname';>"; echo "<input name=';lname'; type=';hidden'; value=';$lastname';>"; ?>

In hiddenfields2.php:
Language: PHP
$fname = $_POST[';fname';]; $lname = $_POST[';lname';]; $choice = $_POST[';choice';]; // Then carry on as per the task echo "Thanks for responding to the survey $fname $lname <br><br>\n";

I'm not sure this is 100% correct, but it worked thumbs up smiley
Anonymous User
Re: Topic 5A
August 17, 2009 12:59PM
I didn't make any changes to form.php. I think that because the two input boxes in the form.php script are named 'firstname' and 'lastname' respectively, you need to declare them in hiddenfields.php. They are thereafter assigned as the values for 'fname' and 'lname', and passed on to hiddenfields2.php. Is this correct?
Re: Topic 5A
August 17, 2009 03:01PM
ok ill give it a go ,again grinning smiley

smoking smiley When all else fails , read instructions grinning smiley
smiling smiley Reading is a course on it's own ! smiling smiley
Re: Topic 5A
August 17, 2009 06:58PM
yes , i found my mistake in hiddenfields.php:

i did not do :

$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];

that explains why nothing displays in my browser and url when u was doing some custom error checking.


It is working just fine ....

Now that is Task 5A COMPLETE !!!!!

smoking smiley When all else fails , read instructions grinning smiley
smiling smiley Reading is a course on it's own ! smiling smiley
avatar
Mac
Re: Topic 5A
August 18, 2009 07:37AM
This is an html error you did Chilli. The hidden field's NAME was fname and the value (in this case $firstname) is passed to the next page. It is NOT $firstname that is passed - just its valu, within fname - so to speak. SO you have to call it with $fname_POST.
Point is, in a form, the name is used
Anonymous User
Re: Topic 5A
August 18, 2009 01:53PM
Mac, I'm a bit confused now...

Is the code in my previous posting in this thread correct? It works fine and displays the results as required.
avatar
Mac
Re: Topic 5A
August 18, 2009 05:52PM
No, it's not.

You have
echo "<input name='fname' type='hidden' value='$firstname'>"; and you call it with $fname=$_POST['fname']; which is what I am saying.
The part I'm referring to is $_POST['fname']. That is correct since on the form it is <input name='fname'. You could have called it $grandmother= as long as it is $grandmother=$_POST['fname']
Chillie, it seems, tried to use the $_POST['firstname']
Anonymous User
Re: Topic 5A
August 18, 2009 07:09PM
confused smiley

In form.php the textbox for First Name is named 'firstname' and the textbox for Last Name is named 'lastname'. Because of this, I thought that we needed to call the information entered into these textboxes using $firstname = $_POST['firstname']; and $lastname = $_POST['lastname']; in the hiddenfields.php script.

I used $firstname and $lastname to name my variables because in hiddenfields.php, the values of 'fname' and 'lname' are '$firstname' and '$lastname' respectively.

Sorry if I'm going on about this a bit, but I'm trying to understand what's going on...

Here's my code for form.php:

Language: PHP
<?php $title = "WebBooks.com Reader Survey"; include("header.php"); ?> <form method=post action=';hiddenfields.php';> <b>WebBooks.com Reader Survey</b> <br> <br> What is your first name? <br> <input name=';firstname'; type=';text';> <br> <br> What is your last name? <br> <input name=';lastname'; type=';text';> <br> <br> What is your age? <br> <input name=';age'; type=';text';> <br> <br>   <input type=';submit';>   </form>   <?php include("footer.php"); ?>

hiddenfields.php:
Language: PHP
<?php $title = "WebBooks.com Reader Survey"; include("header.php"); ?> <form method=post action="hiddenfields2.php"> <b>WebBooks.com Reader Survey</b> <br> <br>   Select all of the subjects that reflect your reading tastes: <br> <br>   <input name=';choice[]'; type=';checkbox'; value=';Fiction';>Fiction <br>   <input name=';choice[]'; type=';checkbox'; value=';Non-fiction';>Non-fiction <br>   <input name=';choice[]'; type=';checkbox'; value=';Theater';>Theater <br>   <input name=';choice[]'; type=';checkbox'; value=';Computing';>Computing <br>   <input name=';choice[]'; type=';checkbox'; value=';Science';>Science <br> <br>   <!--INSERT CODE HERE-->   <?php $firstname = $_POST[';firstname';]; $lastname = $_POST[';lastname';]; echo "<input name=';fname'; type=';hidden'; value=';$firstname';>"; echo "<input name=';lname'; type=';hidden'; value=';$lastname';>"; ?>     <input type=';submit';>   </form>   <?php include("footer.php"); ?>

and hiddenfields2.php:
Language: PHP
<?php $title = "WebBooks.com Reader Survey"; include("header.php"); ?>   <b>WebBooks.com Reader Survey</b> <br> <br>   <?php   //INSERT CODE HERE $fname = $_POST[';fname';]; $lname = $_POST[';lname';]; $choice = $_POST[';choice';]; echo "Thanks for responding to the survey $fname $lname <br><br>\n";   echo "<b>These are your favorite book categories:</b>"; echo "<br><br>";     for ($i=0; $i<count($choice); $i++) { echo "$choice[$i]<br>"; }   include("footer.php");   ?>
avatar
Mac
Re: Topic 5A
August 18, 2009 09:03PM
Look at your code. Print your code out, the mark or cross out what I say here below.

form.php: Let us work with firstname. The user enters a value by way of <input name='firstname' type='text'>. Forget about the value entered by the user.

In hiddenfield.php, you catch the value entered on the previous page by way of $_POST['firstname']. This is the only way since on the previous page you gave that form element a name of 'firstname'.

hiddenfield.php, however, is just a "pass-through" page to the next page, .i.e. here you need to collect an additional "array" of tastes. So we are collecting more information (tastes) but do not want to lose the firstname we collected on the 1st page, as we need to keep it and pass it on to the 3rd page - where we will use it together with the array collected from the 2nd page.

SO how do we pass it on? We first capture it in hiddenfields.php as sent from the 1st page with a $firstname = $_POST['firstname'];

and then we add it within hiddenfields.php, using a hidden field - as you have done.

echo "<input name='fname' type='hidden' value='$firstname'>";

BUT NOTE WHAT YOU HAVE DONE NOW - you have now named it "fname' (and you can give it any name you want under the sun - as long as you capture it on the next page with $_POST['any name you want under the sun '].

Back to the example - here you have used 'fname' as in

echo "<input name='fname' type='hidden' value='$firstname'>";

So, necessarily, on the next page you need to catch it with $_POST['fname'].

The point is this - when using a form, you need to give elements of the form (whether it is <input type= text...> or <input type=hidden) a "name". It is this "name that you need to catch on the next page and whatever next pages in the &_POST part. So just look wat what <input name.... you have used.

So your script is quite correct.

And just to confuse (but teach you) you - remember - you can use sessions to pass the value on from page to page, as opposed to using it in hidden form elements.....for now - use hidden forms elements, then think about how you can pass it with sessions. Or even cookies if you really want.
Anonymous User
Re: Topic 5A
August 19, 2009 07:59AM
Thanks for the explanation Mac smileys with beer

It seems that the more we learn, the more possibilities become available and the more we need to learn.
Re: Topic 5A
August 21, 2009 07:53AM
I battled a bit but found the problem.

In the first form we send the variable $lastname and we call it in the second as that $lastname.
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];

But then we change that value to $lname so in the third form we need to call $lname
$fname = $_POST['fname'];
$lname = $_POST['lname'];

That solved things for me

Done, this is a pretty cool feature.
Thanx Mac
Thanx Unisa
avatar Re: Topic 5A
August 23, 2009 03:31PM
Ok 5A completed. Pretty cool using hidden form fields.
fcb
Re: Topic 5A
August 26, 2009 02:51PM
No problems.

Probably better to keep the passed variable names the same throughout to avoid confusion.
avatar Re: Topic 5A
August 27, 2009 07:29PM
5A-1 completed.

To pass the variables from the 2nd to the 3rd page I used the format shown on page 323 in the handbook (in the HTML section):
Language: PHP
<input name=';fname'; type=';hidden'; value=';<?php echo $firstname; ?>';> <input name=';lname'; type=';hidden'; value=';<?php echo $lastname; ?>';>
avatar Re: Topic 5A
August 27, 2009 08:56PM
Two questions for Mac:

1. How do you validate arrays being passed between pages?

This works for single parameters (on the 2nd page):
Language: PHP
$firstname = HTMLSpecialChars($_POST[';firstname';]);

But this does not (strings is an array):
Language: PHP
$strings = HTMLSpecialChars($_POST[';strings';]);

2. Passing variables between pages with forms (old topic I know, but ...):
I assume the Masterskill study guide is correct and if I were using PHP 4.x, the exercises would have worked as is (without having to make use of register_globals)? So why did they change PHP 5 so you have to make use of register_globals?
fcb
Re: Topic 5A
August 28, 2009 08:12AM
@ ORDO

The function HTMLSpecialChars requires a STRING
see php.net
you will need to convert each element of the array after being passed to the next page
avatar
Mac
Re: Topic 5A
August 28, 2009 08:50AM
Re: Topic 5A
August 30, 2009 05:40PM
Done. This is pretty cool. on to 5B
Sorry, only registered users may post in this forum.

Click here to login