problem with variables working cooperatively... extreme newbie

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • montana
    New Member
    • Jul 2008
    • 10

    problem with variables working cooperatively... extreme newbie

    below i have code from a simple php form program. you are supposed to enter your name in a text box and push the submit button. it then is supposed to bring you to a page that says "hello --your name here-- "
    but instead it says "Hello $userName!; ?>" i tried rewriting it, ive tried fixing it different ways, and i thought maybe it had something to do with "register_globa ls" but i have a severe lack of understanding of it. however i do have it turned on (as i remember). anyways, here is the code of the original page first and after i will have the code of the second page. thanks

    Code:
    <html>
    <head>
    <title> psuedo login </title>
    </head>
    
    <body>
    <h1> What is your name?</h1>
    <h3> form for user input</h3>
    <form method = "post"
    	action = "hiUser.php">
    Please type name here:
    <input type = "text"
    	name = "userName"
    	value = "">
    
    <br>
    <input type = "submit">
    
    </form>
    </body>
    </html>
    now here is the second page
    Code:
    <html>
    <head>
    <title> then you must be...</title>
    </head>
    
    <body>
    
    
    <h1>Hi User</h1>
    <h3> php program that recieves a value from "index.html"</h3>
    
    <?
    print <h3> Hello $userName!</h3>;
    
    ?>
    
    </body>
    </html>
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    Try putting your print statement in quotes.

    Code:
    print "hello";

    Comment

    • leeogrady
      New Member
      • Jun 2008
      • 11

      #3
      to use the 'username' variable on the new page you need to access it by using $_POST['username']...

      Comment

      • montana
        New Member
        • Jul 2008
        • 10

        #4
        wow, thats really funny. ive been reading out of this php book that i got as a gift and it didnt even mention the post command. what a piece of garbage. thanks guys

        Comment

        • montana
          New Member
          • Jul 2008
          • 10

          #5
          alright well i tested it out and it didnt work... instead of posting "hello --your name here--" it doesnt show anything at all. this is SO confusing. can someone please PLEASE give me an idea where to go from here?

          Comment

          • Markus
            Recognized Expert Expert
            • Jun 2007
            • 6092

            #6
            Originally posted by montana
            alright well i tested it out and it didnt work... instead of posting "hello --your name here--" it doesnt show anything at all. this is SO confusing. can someone please PLEASE give me an idea where to go from here?
            You're using shorttags (<? /*code*/ ?>) try using full tags (<?php /*code*/?>)

            That book seems abit out-dated.

            The code would work, had register_global s been enabled.

            Comment

            • montana
              New Member
              • Jul 2008
              • 10

              #7
              well im curious now, what version of php is the most current stable version?

              Comment

              • montana
                New Member
                • Jul 2008
                • 10

                #8
                the full tags didnt do the trick. im running out of ideas. i think maybe there might be something wrong with my .ini file? or something i did wrong in the setup maybe? i even did a cross browser check and its all the same.

                Comment

                • coolsti
                  Contributor
                  • Mar 2008
                  • 310

                  #9
                  Code:
                  <html>
                  <head>
                  <title> then you must be...</title>
                  </head>
                   
                  <body>
                   
                   
                  <h1>Hi User</h1>
                  <h3> php program that recieves a value from "index.html"</h3>
                   
                  <?php
                  $userName = trim($_POST['userName']);
                  echo "<h3> Hello " . $userName . "!</h3>";
                   
                  ?>
                   
                  </body>
                  </html>
                  Try the above. The changes I make are as follows:

                  1) use of <?php instead of <? as PHP tag.
                  2) echo instead of print. Maybe print works fine, I just never used it :)
                  3) I broke up the argument to the echo statement to three pieces using the string concatenation symbol which is a period. I did this because you have a ! just after the PHP variable name. This is probably no problem, but I do this just to make sure the line is handled correctly by PHP. There are many ways of writing this line, but what I did above should work.
                  4) You need to retrieve the value of userName from the $_POST array. Note that the $_POST array may be case sensitive. I use Linux, which definitely is case sensitive, so in this case "userName" would be present but "username" would not be! I am not sure about PHP on Windows, whether that is case sensitive, but it is best to keep case sensitivity in case you ever need to port your scripts.

                  If the above does not work, then you need to examine your $_POST array and see if it is present and correct. To do this, try putting this statement in the script above, after the <body> tag:

                  echo "<pre>";
                  print_r($_POST) ;
                  echo "</pre>";

                  That should print a listing of your $_POST array. If it is empty or you do not see the entry for 'userName', then you need to inspect your php configuration to see why. Also make sure that the page that calls this script (the one the user submits) is properly set up such that the submit button and the userName input field are contained within the same form tags (if you put two sets of form tags on your page or made some mistake here, you may not get the input values to your $_POST correctly.

                  Comment

                  • andu22u
                    New Member
                    • Jul 2008
                    • 1

                    #10
                    please don't forget them next time

                    try to use the global variable $_POST and don't forget that the print function has one parameter
                    [CODE=php]
                    <html>
                    <head>
                    <title> then you must be...
                    </head>
                    <body>

                    <h1>Hi User
                    <h3> php program that recieves a value from "index.html "

                    <?php

                    print "<h3> Hello ".$_POST['userName']."!";
                    ?>

                    </body>
                    </html>[/CODE]
                    Last edited by r035198x; Jul 15 '08, 11:21 AM. Reason: added code tags

                    Comment

                    Working...