Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5
This is the forum to discuss the Wrox book Beginning PHP4 by Wankyu Choi, Allan Kent, Chris Lea, Ganesh Prasad, Chris Ullman; ISBN: 9780764543647
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old October 27th, 2003, 04:02 PM
Authorized User
 
Join Date: Oct 2003
Location: , Ok, .
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
Default CH3, pg 96 Hidden Form Fields

Im having a little trouble with the second page. Can I do on page two what I did? It doesnt work so obviously its wrong but why?


<html><head><title> Hidden Form Fields</title></head>
<body>
<?php $message1 = "Bugs";
    $message2 = "Homer";
    $message3 = "Ren & Stimpy";

    echo"<form method = POST action = 'hidden2.php' >";
    echo"Which of the following would win in a shootout?";
    echo"<select name = 'listbox'>";
    echo"<option> $message1 </option>";
    echo"<option> $message2 </option>";
    echo"<option> $message3 </option>";
    echo"</select>";

    echo"<input type = hidden name = hidden value ='$message1'>";
    echo"<input type = hidden name = hidden value ='$message2'>";
    echo"<input type = hidden name = hidden value ='$message3'>";

    echo"<input type = submit>";
    echo"</form>";
?>
</body>
</html>

---

<html><head><title> Hidden Form Fields</title></head>
<body>
    <?php
        echo "The three options were: <br />";
        if(isset($_POST['hidden']))
        {
            echo .join(', ', $_POST['hidden'] .".\n";
        }

        echo "You selected: <br />";
        echo $_POST['listbox'] ;
    ?>
</body>
</html>

  #2 (permalink)  
Old October 27th, 2003, 04:36 PM
richard.york's Avatar
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

Each hidden field needs to have a unique name.

echo"<input type='hidden' name='hidden1' value ='$message1' />";
echo"<input type='hidden' name='hidden2' value ='$message2' />";
echo"<input type='hidden' name='hidden3' value ='$message3' />";

$_POST["hidden"] would contain the value of the last field, overwritting the two previous values. Secondly you should delimit all HTML attribute values with quotations.

See my HTML standards FAQ:
http://p2p.wrox.com/topic.asp?TOPIC_ID=4028

echo "The three options were: <br />";

if (isset($_POST['hidden1']))

    echo $_POST['hidden1']."<br />\n";

if (isset($_POST['hidden2']))

    echo $_POST['hidden2']."<br />\n";

if (isset($_POST['hidden3']))

    echo $_POST['hidden3']."<br />\n";



:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
  #3 (permalink)  
Old October 27th, 2003, 04:47 PM
Authorized User
 
Join Date: Oct 2003
Location: , Ok, .
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
Default

So this is what you are saying:

<html><head><title> Hidden Form Fields</title></head>
<body>
    <?php
        echo "The three options were:" ;
        echo $_POST['hidden1'];
        echo $_POST['hidden2'];
        echo $_POST['hidden3'];

        echo "You selected: ";
        echo $_POST['listbox'];

    ?>
</body>
</html>

Do I need the isset if Im only getting one variable?
Another question. If I use <br> or <br/> or <br /> to break for a new line, the page will not display at all. Why?


  #4 (permalink)  
Old October 27th, 2003, 05:01 PM
Authorized User
 
Join Date: Oct 2003
Location: , Ok, .
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I got a little curious. I added in this top line for the strict.dtd. I read someone where about a page might not be read correctly if the browser doesnt know what to expect. I also added the <br/> in side the " " and that did the trick. The last <br/> is a little odd to me but as long as it works on the page.

quesadilla5, Im reading the page you directed me to now. Ill go over it thoroughly. I will still be asking for help though.

Thanks for your help.

<!doctype html public "-//w3c//dtd html 4.01//en" "http://www.w3.org/tr/html4/strict.dtd">
<html><head><title> Hidden Form Fields</title></head>
<body>
    <?php
        echo "The three options were: <br/>" ;
        echo $_POST['hidden1'];
        echo $_POST['hidden2'];
        echo $_POST['hidden3'];

        echo "<br/>You selected: ";
        echo $_POST['listbox'];

    ?>
</body>
</html>

  #5 (permalink)  
Old October 27th, 2003, 05:12 PM
richard.york's Avatar
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

That would do it.
isset() determines if a variable exists. In PHP without isset, if a variable doesn't exist PHP automatically creates the variable temporarily with a null value. If your error_reporting directive in php.ini is set to E_ALL and you run the script and the variables do not exist php would issue a notice error. In the scope of something as small as this I would say, no, you don't need to use it. But in a larger application where there are many variables the use of this becomes increasingly more beneficial. It basically makes code execution more explicit and when combined with error_reporting = E_ALL can aid in tracking down bugs from variables not existing where you would want them to, or the opposite, variables existing where you don't want them to. There is a companion construct called empty() if true determines if a variable contains a false, empty, null or integer zero value, if false determines the opposite.

http://www.php.net/isset
http://www.php.net/empty

As far as the use of <br /> (includes a space between the 'r' and '/' forward slash). is concerned, this is said to be back-wards compatible. And should have nothing to do with a page not displaying. I personally have never seen a purely HTML error that would cause the whole page to not display, the page always displays and if there is an HTML error then you would see it in the rendering or the source code. When this happens there is usually something wrong in the server-side of things.

<br/> (no space) is not backwards-compatible and would imply a strictly-formed XHTML document.

<br> is deprecated and should be avoided.

When you view HTML source what do you see?

: )
Rich

:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
  #6 (permalink)  
Old October 27th, 2003, 05:14 PM
richard.york's Avatar
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

Yes Doctype settings are good things!



:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
  #7 (permalink)  
Old October 27th, 2003, 06:00 PM
Authorized User
 
Join Date: Oct 2003
Location: , Ok, .
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
Default

What does it mean exactly when you try to view the code of your page and all you see is <html><body></body></html>? Can that error tell you what you have done wrong in your php code?


  #8 (permalink)  
Old October 27th, 2003, 06:39 PM
richard.york's Avatar
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

Usually when PHP outputs a blank page there is some kind of error, in my experience its usually been a paradox in the code. I ran test scripts and was unable to reproduce your problem.

I ran the following code:
Code:
<html><head><title> Hidden Form Fields</title></head>
<body>
<?php 

# test.php

    $message1 = "Bugs";
    $message2 = "Homer";
    $message3 = "Ren &amp; Stimpy";

    echo"<form method='post' action='/test.php'>";
    echo"Which of the following would win in a shootout?";
    echo"<select name='listbox'>";
    echo"<option>$message1</option>";
    echo"<option>$message2</option>";
    echo"<option>$message3</option>";
    echo"</select>";

    echo"<input type='hidden' name='hidden1' value ='$message1' />";
    echo"<input type='hidden' name='hidden2' value ='$message2' />";
    echo"<input type='hidden' name='hidden3' value ='$message3' />";

    echo"<input type='submit' name='do_action' value='submit' />";
    echo"</form>";

    if (isset($_POST["do_action"]))
    {    
        echo "<br /><br />The three options were: <br />";

        if(isset($_POST['hidden1']))

            echo $_POST['hidden1']."<br />";

        if(isset($_POST['hidden2']))

            echo $_POST['hidden2']."<br />";

        if(isset($_POST['hidden3']))

            echo $_POST['hidden3']."<br />";

        echo "<br />";
        echo "You selected: <br />";
        echo $_POST['listbox'] ;
     }

    ?>
</body>
</html>
This script was executed in the following browsers without trouble:
NS 3
NS 4
NS 7
IE 6
Op 7

Note the use of XHTML compliant mark-up, and the exclusion of the w3c Doctype setting.
If the <br /> tag works in NS 3.. the oldest I have installed, then I'm willing to bet it'll work in *any* browser.

: )
Rich



:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
  #9 (permalink)  
Old October 27th, 2003, 06:51 PM
Authorized User
 
Join Date: Oct 2003
Location: , Ok, .
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
Default

This is very frustrating.

Im using Mozilla 1.5. A Standards based browser.

That is why I was wondering about the <br/>. When I took it out of the page, the page worked, on the last example. On this example, with or without it nothing comes up on the second page.



  #10 (permalink)  
Old October 27th, 2003, 07:05 PM
richard.york's Avatar
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

Mozilla is nearly 100% identical to NS. The script was also tested in Moz 1.5b and executed fine. Once again there is a distinction between <br/> and <br /> *do not* use <br/> (without the space), if you do use it you will need to set the doctype to XHTML, though it probably works in HTML doctypes also. The distinction being <br/> is intended for use explicitly from within the realm of XHTML. Whereas, <br /> (with a space) may be used in either the HTML or XHTML realm.

The <br /> is recommended by the W3C for back-wards compatibility.
There are links which discus this on the W3C website from the HTML FAQ link that I posted eariler.

: )
Rich


:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::




Similar Threads
Thread Thread Starter Forum Replies Last Post
MySQL Fatal Error Chap 3, Pg 96-96 joshkosmala BOOK: Beginning PHP5, Apache, and MySQL Web Development ISBN: 978-0-7645-7966-0 2 January 8th, 2007 01:55 PM
Changing textboxes to hidden fields - HELP!!! jroxit ASP.NET 1.0 and 1.1 Basics 4 February 10th, 2006 07:46 PM
hidden input fields and Mozilla panos CSS Cascading Style Sheets 1 February 3rd, 2006 05:45 PM
Creating hidden fields SmoothAl BOOK: Beginning ASP 3.0 0 February 6th, 2005 01:44 AM
CH 3 pg 96 Question mririe BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 8 August 25th, 2004 04:17 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.