Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11

Thread: Wrong syntax?

  1. #1
    Regular Coder
    Join Date
    Nov 2002
    Posts
    567
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Wrong syntax?

    I'm following a turtorial on webmasterbase.com to learn PHP. The author creates a simple html file that calls a PHP file and passes a variable.

    The contents of the html file are
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <html>
    <head>
    	<title>Untitled</title>
    </head>
    <body>
    	  <a href="target.php?name=Scott">Fire</a>
    </body>
    </html>
    The contents of the PHP file are;
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <html>
    <head>
    	<title>Untitled</title>
    </head>
    <body>
     <?php echo("This is the target file.  I sent $name"); ?>
    </body>
    </html>
    I've also tried this file without the header. The results are;

    This is the target file. I sent

    This is all on the "http://localhost" running Apache server.

    What's missing here? I have another file that uses $_Post["name"] that works.
    Scott Stewart
    Always happy to learn from pros.

  • #2
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,273
    Thanks
    4
    Thanked 83 Times in 82 Posts
    There is this little setting in PHP called register_globals. In your case on your server it is set to off. The author of that tutorial is explaing things as if it were set to on.

    With register_globals set to on (this is a bad idea) then all you have to do to use a passed value is create a variable of the same name as you had done. With register_globals on the proper way would have been to declare it first like so:

    $name = $HTTP_POST_VARS["name"];
    $name = $HTTP_GET_VARS["name"];

    That is the proper way.

    In the latest versions of PHP the register globals setting is set to off by default so in that case you cannot automatically access thos passed values anymore by just declaring a variable of the same name as that tutorial suggests. Now to access that passed variable you would need to declare it like so:

    $name = $_POST["name"];
    $name = $_GET["name"];
    Last edited by Spookster; 02-21-2003 at 04:48 PM.
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #3
    Regular Coder
    Join Date
    Nov 2002
    Posts
    567
    Thanks
    2
    Thanked 4 Times in 4 Posts
    Thanks for clearing that up. That is why my other test worked and this one didn't. I did find a link that described the .htaccess entry for global variables, but putting that in the file (using notepad) and restarting apache didn't seem to do anything. I guess I'll take that out and just use the sample you provided.

    Should I always initialize the variables like $name=_POST["name"] or is using the <?php echo $_POST["name"]; ?> as good or no different?

    edited post:

    BTW I tried this
    <head>
    <?php
    $name=_GET["name'] <--- line 6
    ?>
    </head>

    <body>
    I sent this <?php $name ?>
    ...
    </body>

    But I get an error on line 6

    Thanks again,
    Last edited by ScottInTexas; 02-21-2003 at 03:32 PM.
    Scott Stewart
    Always happy to learn from pros.

  • #4
    Regular Coder
    Join Date
    Jun 2002
    Location
    UK
    Posts
    577
    Thanks
    0
    Thanked 0 Times in 0 Posts
    all declarations in php end with a ;

    that should clear your error.

    $name=_GET["name'];
    Ökii - formerly pootergeist
    teckis - take your time and it'll save you time.

  • #5
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,266
    Thanks
    6
    Thanked 48 Times in 48 Posts
    I'm surprised okii didn't pick up on this

    $name=_GET["name']

    should be

    $name=$_GET["name"];

    3 errors,

    1. No ; at the end of the line
    2. No $ at the start of the global
    3. Incorrect use of " and ' around the name, you can use a set of " or ', but not like "name' or 'name"

  • #6
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,273
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Ooops. That's what I get for posting early in the morning.
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #7
    Regular Coder
    Join Date
    Nov 2002
    Posts
    567
    Thanks
    2
    Thanked 4 Times in 4 Posts
    Thanks All,

    I was able to fix the problems within this tutorial. There are still problems but I got the gist of it. I was able to open a DB and gather the data, so the prupose was served. I see a major problem with variables though. One statement in the code was written like a javascript if. A button was defined in a form - "<input Type="submit" name="submitbutton" value="Submit">"

    PHP Code:

    if ($submitbutton=="Submit"){
     echo(
    "Submit is true");
    .
    .

    The problem is the if never returned true even though nothing was changing the name value pair of the button.

    Open to input.
    Scott Stewart
    Always happy to learn from pros.

  • #8
    Regular Coder
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    183
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Same thing buddy.

    PHP Code:
    if ($_POST["submitbutton"]=="Submit"){
     echo(
    "Submit is true");
    .
    .

    Don't forget the $_POST or $_GET (depends on your <form> method).

    Variables transfered through the query string (page.php?variable=value) are always $_GET. Variables that are transfered through forms where method="post" are always $_POST

    That help?
    Offtone.com - In the works...

  • #9
    Regular Coder
    Join Date
    Nov 2002
    Posts
    567
    Thanks
    2
    Thanked 4 Times in 4 Posts
    Thanks Sypher,

    Here is another example I tried. To me the problem seems like calling a javascript with the wrong name except no error is invoked.

    PHP Code:
    <body>

    <?php 
    if($submit){ 
        echo 
    "the $_Post array contains<br /><br />"
        while(list(
    $key,$var)=each($_POST)){echo "$key , $var<br />";} 

        
    ?> 
            <form name="bella" method="post" action="<?echo $_SERVER[PHP_SELF];?>"> 
                <input type="hidden" name="hidden_value" value="a hidden value"> 
                <input type="text" name="text_value" value=""> 
                <textarea name="textarea_value"></textarea> 
                <select name="select_value"> 
                    <option value="select_val_1">value 1</option> 
                    <option value="select_val_2">value 2</option> 
                    <option value="select_val_3">value 3</option> 
                </select> 
            <input type="submit" name="submit" value="submit"> 


    </body>
    This was lifted right out of a sample tutorial and pasted into the body of my doc. It is saved with the php extension and run from localhost. The form shows up and the submit does submit the data but is does not show the true side of the IF.

    I have to assume that it worked before the authro put it up as a sample.
    Scott Stewart
    Always happy to learn from pros.

  • #10
    Regular Coder
    Join Date
    Jun 2002
    Location
    UK
    Posts
    577
    Thanks
    0
    Thanked 0 Times in 0 Posts
    same again

    if($_POST['submit'])

    or better

    if($_POST['submit'] == "submit")
    Ökii - formerly pootergeist
    teckis - take your time and it'll save you time.

  • #11
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,273
    Thanks
    4
    Thanked 83 Times in 82 Posts
    There is a nice little function in PHP called isset(). This is what you should use in this situation:

    PHP Code:

    <?php

    $submit 
    $_POST["submit"];
     
    if(isset(
    $submit)){ 
        echo 
    "the $_POST array contains<br /><br />"
        while(list(
    $key,$var)=each($_POST)){echo "$key , $var<br />";} 

        
    ?>
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •