28 Replies Latest reply: Apr 10, 2012 1:18 PM by bregent RSS

    help with forms

    peterattrsb Community Member

      I am quite new to Dreamewaver CS4 and I am presently working my way through the book by Jeffery Bardzell "training from the source. I have reached lesson 5 "passing data between pages. I have created the first and last name text fields on the form and then run the form through the server. I works fine and displays the first and last names on the form processor ie "thank you ,john smith, for filling out my form" The book then tells me to open the form processor and it should look like "thank you ,, for filling out my form, however it displays:

       

      Thank You, Notice: undifined index:firstName in c:\xampp\htdocs\newland\test_form_processor.php on line 9.

                                      undifined index lastName in c:\xampp\htdocs\newland\test_form_processor.php on line 9

      ,for filling out my form.

       

      Could someone tell me what i have done wrong. I followed the book word for word and i have also checked the code on my page with the code on the "completed lesson" on the CD. theyr'e both identical.

       

      Any help would be apreciated

        • 1. Re: help with forms
          Ben Pleysier CommunityMVP

          Have you been in touch with Jefferey Bardzell, after all he is the one that sold the book to you.

           

          Failing that, you could show your code for us to see.

           

          Gramps

          • 2. Re: help with forms
            peterattrsb Community Member

            Not sure how to do that. Iv'e tried copy and paste but the paste doesn't work on this

            • 3. Re: help with forms
              Ben Pleysier CommunityMVP

                   peterattrsb User  

               

               

              Not sure how to do that. Iv'e tried copy and paste but the paste doesn't work on this

               

               

              I have just copied and pasted your last post. Not sure why it does not work for you. Please remember that for the PC you can paste using ctrl-v

               

              Gramps

              • 4. Re: help with forms
                peterattrsb Community Member

                Thanks it worked doing that I have just re looked at the code in my page and also the code in the lesson finished and theyre identical.

                 

                This is what it should say according to the book:

                 

                "thank you ,, for filling out my form"

                 

                however it displays: 

                 

                Thank You, Notice: undifined index:firstName in c:\xampp\htdocs\newland\test_form_processor.php on line 9.

                                   Notice: undifined index lastName in c:\xampp\htdocs\newland\test_form_processor.php on line 9

                ,for filling out my form.

                 

                 

                <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
                <html xmlns="http://www.w3.org/1999/xhtml">
                <head>
                <title>Untitled Document</title>
                <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
                </head>

                <body>

                <p>Thank you,<?php echo $_GET['firstName']; ?> <?php echo $_GET['lastName']; ?>, for filling out my form. </p>
                </body>
                </html>

                • 5. Re: help with forms
                  Ben Pleysier CommunityMVP

                  When you view the document in a browser, the URL will show

                  c:\xampp\htdocs\newland\test_form_processor.php

                  or similar. If we add the URL variables that the document asks for, Bob's your uncle. Add the highlighted as in

                  c:\xampp\htdocs\newland\test_form_processor.php?firstname=Uncle&lastname=Bob

                  The result will be

                  Thank you, Uncle Bob, for filling out my form.

                  What this means is, that the document that calls this document should include the URL variables in the link.

                   

                  Gramps

                  • 6. Re: help with forms
                    peterattrsb Community Member

                    Yes that part of it works fine but the book tells you to open the  test_form_processor.php and then press f12 and it should display:

                     

                    thank you ,, for filling out my form because the form looses its information when you close the test form

                     

                    I have the same trouble with the test_form_processor_cookies.php. everything works ok you open the test form, enter your first and last name and the form displayes correctly then it said follow the cookies link and that displayes correctly eg Hi Fred Smith ! But then it tells you close the browser, select test_form_processor_cookies.php and press F12.

                     

                    Earlier when you tried this experiment with test_form_processor.php It didn't work because there was no data in the queerystring that data was lost as soon as you closed your browser. PHP left the text blank but when you try the same experiment using cookies, even though you closed the browser the data persisted because it was saved on your hard drive as a cookie.so when I open the test_form_processor_cookies.php it should still display Hi Fred Smith ! but it doesn't it displayes:

                     

                    Hi,

                    Notice: Undefined index: firstName in C:\xampp\htdocs\newland\test_form_processor_cookies.php on line 9

                     

                    Notice: Undefined index: lastName in C:\xampp\htdocs\newland\test_form_processor_cookies.php on line 9

                    !

                     

                    The code for that page is:

                     

                    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
                    <html xmlns="http://www.w3.org/1999/xhtml">
                    <head>
                    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
                    <title>Untitled Document</title>
                    </head>

                    <body>
                    Hi, <?php echo $_COOKIE['firstName']; ?> <?php echo $_COOKIE['lastName']; ?> !
                    </body>
                    </html>

                    • 7. Re: help with forms
                      bregent CommunityMVP

                      We would need to see how you are setting the cookie. Cookies can  expire as soon as the browser is closed.

                      • 8. Re: help with forms
                        peterattrsb Community Member

                        I just followed the instructions in the book. This is the exact wording:

                         

                        In the bindings panel click the new binding button, choose cookie variable and type firstName. Repete step 7 to add lastName cookie to the bindings panel.

                         

                        Thats how it tells me to create the cookies.

                         

                        The book then goes on:

                         

                        Click test_form.php in the site panel and press F12 to test. Fill out the form, click submit, then follow the Check Cookie Link.

                         

                        As you would expect it works. Even though the data started on the first page , you got it to display on the third. The information was pulled not from the URL or the request body as form variables, but rather from your hard drive.

                         

                        Close your browser. Return to Dreamweaver, Select test_form_processor_cookies.php in the site panel, and press F12

                         

                        Earlier when you tried this experiment with test_form_processor.php It didn't work because there was no data in the queerystring that data was lost as soon as you closed your browser. PHP left the text blank but when you try the same experiment using cookies, even though you closed the browser the data persisted because it was saved on your hard drive as a cookie

                        • 9. Re: help with forms
                          Ben Pleysier CommunityMVP

                          I guess the book is wrong! Try to get your money back. If you want an outcome as you have described the line reading

                          <p>Thank you,<?php echo $_GET['firstName']; ?> <?php echo $_GET['lastName']; ?>, for filling out my form. </p>

                          should read

                          <p>Thank you,<?php echo (isset($_GET['firstName']) && isset($_GET['lastName'])) ? $_GET['firstName'].' '.$_GET['lastName'] : ' '; ?>, for filling out my form. </p>

                          The result will be

                          Thank you, , for filling out my form.

                          Gramps

                          • 10. Re: help with forms
                            osgood_ CommunityMVP

                            peterattrsb wrote:

                             

                            Thanks it worked doing that I have just re looked at the code in my page and also the code in the lesson finished and theyre identical.

                             

                            This is what it should say according to the book:

                             

                            "thank you ,, for filling out my form"

                             

                            however it displays: 

                             

                            Thank You, Notice: undifined index:firstName in c:\xampp\htdocs\newland\test_form_processor.php on line 9.

                                               Notice: undifined index lastName in c:\xampp\htdocs\newland\test_form_processor.php on line 9

                            ,for filling out my form.

                             

                             

                             

                            But it only displays the 'Notice: undifined index' IF you run it through the browser, right. I think what the book means by 'open' the 'test_form_processor.php' page is open it in Dreamweaver then the page will look like it says:

                            "thank you ,, for filling out my form"

                            • 11. Re: help with forms
                              peterattrsb Community Member

                              I tried the same experiment using the completed lessons form on the CD that came with the book and I got the same results. I then changed the code to the one gramps suggested and it worked OK.

                               

                              I have to say someone new to Dreamweaver and following the book word for word would expect it to work. I think the book could be a little clearer and if there is the possibility that an error could occur then the book should at least have some reference that this could happen. It would save someone like me spending hours looking through the code thinking they had done something wrong when in actual fact they hadn’t. It’s not a cheap book I paid £ 39.33 a few years ago so I think it would have gone up since then.

                              When I have worked my way through this book I am moving on to Dreamweaver CS5 Training form the source by David Powers. Hope this book is right.

                               

                              Thanks for all your help with this. It was really appreciated. At lease i know where to come to if i get stuck again.

                               

                              Thanks

                               

                              Paul

                              • 12. Re: help with forms
                                osgood_ CommunityMVP

                                peterattrsb wrote:

                                 

                                I tried the same experiment using the completed lessons form on the CD that came with the book and I got the same results. I then changed the code to the one gramps suggested and it worked OK.

                                 

                                I have to say someone new to Dreamweaver and following the book word for word would expect it to work. I think the book could be a little clearer and if there is the possibility that an error could occur then the book should at least have some reference that this could happen. It would save someone like me spending hours looking through the code thinking they had done something wrong when in actual fact they hadn’t. It’s not a cheap book I paid £ 39.33 a few years ago so I think it would have gone up since then.

                                When I have worked my way through this book I am moving on to Dreamweaver CS5 Training form the source by David Powers. Hope this book is right.

                                 

                                Thanks for all your help with this. It was really appreciated. At lease i know where to come to if i get stuck again.

                                 

                                Thanks

                                 

                                Paul

                                 

                                The first instance ("thank you ,, for filling out my form,) was never going to work as the page would never receive the correct information to return anything if just opened in the browser WITHOUT either going through the form or as gramps solution passing the variables firstName & lastName in the url

                                • 13. Re: help with forms
                                  peterattrsb Community Member

                                  Just an Afterthought. Is it the same code for the test_form_processor_cookies.php form with replacing the word $_GET with $_COOKIE?

                                  • 14. Re: help with forms
                                    osgood_ CommunityMVP

                                    I'd say you have to set the 'cookie' first:

                                     

                                     

                                    <?php

                                    setcookie("firstName", "Uncle");

                                    setcookie("lastName", "Bob");

                                    ?>

                                     

                                     

                                    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

                                    <html xmlns="http://www.w3.org/1999/xhtml">

                                    <head>

                                    <title>Cookie Test</title>

                                    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

                                    </head>

                                     

                                    <body>

                                     

                                    <p>Thank you, <?php echo $_COOKIE['firstName'];  ?> <?php echo $_COOKIE['lastName']; ?>for filling out my form. </p>

                                    </body>

                                    </html>

                                    • 15. Re: help with forms
                                      bregent CommunityMVP

                                      >In the bindings panel click the new binding button, choose cookie

                                      >variable and type firstName. Repete step 7 to add lastName cookie to the bindings panel.

                                      >

                                      >Thats how it tells me to create the cookies.

                                       

                                      They don't tell you to set an expiration time? Without a specified expiration, the cookie will expire when the browser is closed. So, it seems everything is working as expected.

                                       

                                      >even though you closed the browser the data persisted

                                      >because it was saved on your hard drive as a cookie

                                       

                                      This is wrong. If you don't specify an expiration, cookies expire when the browser is closed.

                                      • 16. Re: help with forms
                                        peterattrsb Community Member

                                        Thanks for all the help. Just one question for Gramps if that’s Ok.

                                         

                                        As I am trying to learn what all the various codes mean could you tell me what the code below means, what’s it telling the webpage to do?

                                         

                                        ,<?php echo (isset($_GET['firstName']) && isset($_GET['lastName'])) ? $_GET['firstName'].' '.$_GET['lastName'] : ' '; ?>,

                                         

                                        I am moving on to the next lesson in the book “Sending email from a web form”. The book does say that because I am using Apache and PHP on a local server the email won’t actually go any ware but I should complete the lesson anyway.

                                        • 17. Re: help with forms
                                          Ben Pleysier CommunityMVP

                                          <?php echo (isset($_GET['firstName']) && isset($_GET['lastName'])) ? $_GET['firstName'].' '.$_GET['lastName'] : ' '; ?>,

                                          In plain English

                                          echo

                                          print/show following echo. In our case we need a conditional statement to choose what will be shown. The conditional statement that I have used is called a ternary operator. This does the same as if condition is true then else this

                                          (isset($_GET['firstName']) && isset($_GET['lastName']))

                                          Our condition: firstName and lastName values are not empty

                                          ?

                                          if true, i.e. there is a value for both of the variables

                                          $_GET['firstName'].' '.$_GET['lastName']

                                          then go ahead and print the above

                                          :

                                          if false, i.e. there is no value for BOTH the variables

                                          ' '

                                          then print nothing

                                           

                                          The reason why we have to use the conditional statement is probably already clear to you, it is to make sure that we do not get error messages if the condition cannot be met.

                                           

                                          Gramps

                                          • 18. Re: help with forms
                                            peterattrsb Community Member

                                            Just a quick question. Is it possible to use apache / php and asp on the same computer? The reason I ask is the book gives 3 options for developing the site using a local server. When I was trying to choose, apache / php was the easiest to download and configure but asp does allow the site to perform functions on a local server that php doesn’t have.

                                            • 19. Re: help with forms
                                              peterattrsb Community Member

                                              Sorry I'm confused again. When I upload a form to the server this is what it looks like.

                                               

                                              The functionailty of the form works and it does give the right error ehrn you enter a non numeric value. I have also tested the completed lessons and i get the same message.

                                               

                                              Newland Tours: Tour Price Calculator 

                                              Enter the number of Adults and Children in your party as well as the tour name of the tour you want to calculate.

                                              All fields are required (enter 0 if nessesery)

                                               

                                              Notice: Undefined index: error in C:\xampp\htdocs\newland\tourprice.php on line 29

                                               

                                              Number of Adults:
                                              Number of Children:
                                              Tour Name:

                                               

                                              This is the code in my form and its identical to the one on the CD from the book.

                                               

                                              <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
                                              <html xmlns="http://www.w3.org/1999/xhtml">
                                              <head>
                                              <title>Newland Tours: Price Calculator</title>
                                              <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
                                              <link href="css/newland.css" rel="stylesheet" type="text/css" />
                                              </head>

                                              <body>
                                              <a href="#top"><img src="images/spacer.gif" alt="Skip to main page content." width="1" height="1" border="0" align="left" /></a>
                                              <table width="750" border="0" cellpadding="3" cellspacing="0">
                                              <tr>
                                                <td><img src="images/banner_left.gif" width="451" height="68"  alt="Newland Tours Banner, Left" /></td>
                                                <td width="280"><img src="images/banner_right.jpg" width="276" height="68" alt="Newland Tours Banner, Right" /></td>
                                              </tr>
                                              <tr>
                                                <td><img src="images/navbar.gif" name="navbar" width="450" height="20" border="0" usemap="#navbarMap" alt="Navigation Bar" /></td>
                                                <td><img name="copyright_bar" src="images/copyright_bar.gif" width="272" height="20" border="0" alt="Copyright 2004 Newland Tours" /></td>
                                              </tr>
                                              <tr>
                                                <td colspan="2">
                                              <h1><br />
                                                 <a name="top" id="top"></a>Newland Tours: Tour Price Calculator</h1>
                                                 <p>Enter the number of Adults and Children in your party as well as the tour name of the tour you want to calculate. </p>
                                                 <p>All fields are required (enter 0 if nessesery)</p>


                                                  <?php
                                              if ($_GET['error'] == "notnumeric")
                                              {
                                                   echo "<p class=\"error\">*** Error! One or more fields was left blank or contained a non-numeric character.</p>";
                                              }
                                              ?>
                                                     
                                                 <form id="frm_tourprice" name="frm_tourprice" method="post" action="tourprice_processor.php">
                                                   <table width="60%" border="0" cellpadding="3">
                                                     <tr>
                                                       <td>Number of Adults:</td>
                                                       <td><label>
                                                         <input type="text" name="numAdults" id="numAdults" />
                                                       </label></td>
                                                     </tr>
                                                     <tr>
                                                       <td>Number of Children:</td>
                                                       <td><label>
                                                         <input type="text" name="numChildren" id="numChildren" />
                                                       </label></td>
                                                     </tr>
                                                     <tr>
                                                       <td>Tour Name:</td>
                                                       <td><label>
                                                         <select name="tourName" id="tourName">
                                                           <option value="500">Highlights of Argentina</option>
                                                           <option value="700">Highlights of Western Canada</option>
                                                           <option value="900">Egyption Pyramids and More</option>
                                                         </select>
                                                       </label></td>
                                                     </tr>
                                                     <tr>
                                                       <td> </td>
                                                       <td><label>
                                                         <input type="submit" name="button" id="button" value="Submit" />
                                                       </label></td>
                                                     </tr>
                                                   </table>
                                                 </form></td>
                                              </tr>
                                              </table>

                                              <br />
                                              <br />


                                              <map name="navbarMap" id="navbarMap">
                                              <area shape="rect" coords="1,0,62,20" href="index.php" alt="Home" />
                                              <area shape="rect" coords="71,0,117,20" href="about.php" alt="About" />
                                              <area shape="rect" coords="129,0,196,20" href="tours.php" alt="Find Tours" />
                                              <area shape="rect" coords="209,0,311,20" href="profiles.php" alt="Country Profiles" />
                                              <area shape="rect" coords="327,0,434,20" href="contact.php" alt="Contact An Agent" />
                                              </map>
                                              </body>
                                              </html>

                                               

                                               

                                              • 20. Re: help with forms
                                                bregent CommunityMVP

                                                >if ($_GET['error'] == "notnumeric")

                                                 

                                                I'm not sure if this is the line causing the error, but here you are testing the value of a variable without first testing if it has been set. Always test if an array element is set before trying to access the value. http://us3.php.net/isset

                                                • 21. Re: help with forms
                                                  peterattrsb Community Member

                                                  This is really frustrating. I’m trying to follow a book that is supposed to teach you how to do things. It even goes so far as to have a disc with completed lessons that I would assume were right but I get the same errors even with the files on the disk that came with the book. I really feel like I’m trying to swim upstream.

                                                  • 22. Re: help with forms
                                                    peterattrsb Community Member

                                                    Thanks bregent you was right.

                                                    I played around with the code that gramps gave me for the test form processor and the correct code was:

                                                     

                                                    if (isset($_GET['error']) &&  $_GET['error'] == "notnumeric")

                                                     

                                                    (I think) anyway the error has gone now

                                                     

                                                    Next lesson is databases so that should be fun.

                                                    • 23. Re: help with forms
                                                      peterattrsb Community Member

                                                      I have got through most of the code but i got stuck again. If I have 2 fields i understand the code is:

                                                       

                                                      <?php echo (isset($_GET['firstName']) && isset($_GET['surName'])) ? $_GET['firstName'].' '.$_GET['surName'] : ' '; ?>

                                                       

                                                      But if I just have one field I have tried taking out the second field ie

                                                       

                                                      <?php echo (isset($_GET['firstName']) ? $_GET['firstName'].' '. ?>

                                                       

                                                      Could someone help please

                                                      • 24. Re: help with forms
                                                        osgood_ CommunityMVP

                                                        <?php echo (isset($_GET['firstName']) ? $_GET['firstName'].' '. ?>

                                                         

                                                         

                                                        I'd write it like:

                                                         

                                                        <?php

                                                         

                                                        $firstName = $_GET['firstName'];

                                                         

                                                        if (isset($firstName)) {

                                                        echo $firstName;

                                                        }

                                                        else {

                                                        echo "First name not set";

                                                        }

                                                         

                                                        ?>

                                                        • 25. Re: help with forms
                                                          osgood_ CommunityMVP

                                                          Or following your technique like below:

                                                           

                                                          <?php echo (isset($_GET['firstName'])) ? $_GET['firstName'] : ' '; ?>

                                                          • 26. Re: help with forms
                                                            peterattrsb Community Member

                                                            Thanks very much for the help. Both ways work fine.

                                                             

                                                            Paul

                                                            • 27. Re: help with forms
                                                              peterattrsb Community Member

                                                              I have created a contact us form and when the visitor presses submit the site directs them to a message sent page. I have put all the usual stuff on the page ie thanks for contacting us. I have also created a table so the visitor can see the message they sent. I have bound all the fields from the contact form and they work fine, The only problem I have is the text element.  When the data is sent to the message sent form the body of the message does not wrap and create new lines in the box. It just widens the box and dissapears off the end of the page. Is there some sort of setting I need to set in the field propoties?

                                                               

                                                              • 28. Re: help with forms
                                                                bregent CommunityMVP

                                                                Text entered into a text area is plain text, not html. Line feeds and carriage returns are considered whitespace in html. So if you want to display the text and have line feeds you need to convert them to <br>. Use the nl2br() function.