Cannot send parameters

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pladdypants
    New Member
    • Nov 2008
    • 10

    Cannot send parameters

    Hey all,

    I'm new to PHP and i'm just trying to test out a simple script as follows:

    [PHP]
    #!C:/Program Files/PHP/PHP.exe
    <?php
    error_reporting (E_ALL);
    ini_set('displa y_errors', true);

    //import_request_ variables("gpc" ,"mvar_");

    print "Content-type: text/html\n\n";

    if (isset($_GET['name'])) {
    print "Hello, " . $_GET['name'];
    }
    else {
    print <<<HTML
    <form action="http://pladdypants.hom e/cgi-bin/ch6ex.php" method="get">
    Name: <input type="text" name="name" />
    <input type="submit" value="Submit" />
    </form>
    HTML;
    }
    ?>
    [/PHP]>

    I've worked at this and it runs fine, however when i click submit with a name in the field, it doesn't show up when it reruns. The url changes and it has a parameter defined there, but nothing prints to the screen. Am I overlooking something simple here? I've tried lots of different things and no luck.

    I'm using XP Pro, mozilla browser, apache 2.2. This is just my computer as a server and i'm accessing local files. Is this an issue with my code, how PHP was installed, how my server is set up? Sorry for such a silly question, I'm sure I'm doing something very basic and stupid...Any help would be greatly appreciated.

    -Pladdy
  • code green
    Recognized Expert Top Contributor
    • Mar 2007
    • 1726

    #2
    There is no such variable as $name; [PHP]if (isset($_GET['name'])) {
    print "Hello, " . $name; [/PHP] Unless you have GLOBALS switched on, which you shouldn't,
    you need to read the URL variables into the variable [PHP]$name = $_GET['name'])) [/PHP]

    Comment

    • pladdypants
      New Member
      • Nov 2008
      • 10

      #3
      Hey, I saw that error in the code and fixed it right before you responded. However even when i use
      [PHP]
      $name = $_GET['name'];
      print $name;
      [/PHP]>

      I still get nothing...

      -pladdy

      Comment

      • pladdypants
        New Member
        • Nov 2008
        • 10

        #4
        Originally posted by pladdypants
        Hey, I saw that error in the code and fixed it right before you responded. However even when i use
        [PHP]
        $name = $_GET['name'];
        print $name;
        [/PHP]>

        I still get nothing...

        -pladdy
        Also, here's the readout i get for the line that should set $name = to $_GET['name']
        "Notice: Undefined index: name in C:\Program Files\Apache Software Foundation\Apac he2.2\cgi-bin\ch6ex.php on line 9"

        So i see it being passed in the url, but it's like it's not ending up in $_GET...
        -pladdy

        Comment

        • code green
          Recognized Expert Top Contributor
          • Mar 2007
          • 1726

          #5
          Then I suspect your HTML [PHP]else {
          print <<<HTML
          <form action="http://pladdypants.hom e/cgi-bin/ch6ex.php" method="get">
          Name: <input type="text" name="name" />
          <input type="submit" value="Submit" />
          </form>
          HTML; [/PHP] I don't understand how this
          runs fine
          .
          How are you writing to the browser?
          PHP - No quotes
          HTML - No closing php tag
          Or that other method of bulk printing with the <<< syntax

          Comment

          • pladdypants
            New Member
            • Nov 2008
            • 10

            #6
            Originally posted by code green
            Then I suspect your HTML [PHP]else {
            print <<<HTML
            <form action="http://pladdypants.hom e/cgi-bin/ch6ex.php" method="get">
            Name: <input type="text" name="name" />
            <input type="submit" value="Submit" />
            </form>
            HTML; [/PHP] I don't understand how this .
            How are you writing to the browser?
            PHP - No quotes
            HTML - No closing php tag
            Or that other method of bulk printing with the <<< syntax
            Hi again,
            1) By 'runs fine' i mean it actually runs without critical errors that kill it.
            2) I'm trying to write the end result (Hello, 'name') to the browser just using
            [PHP]
            print "Hello, " . $_GET['name'];
            [/PHP]
            So, i either want the browser to show my simple form, using the here document (HTML), or if it has a value from the form, to just print "Hello, " $_GET['name'].

            The php script i posted originally is the entire script - is that missing something critical that i've overlooked, like end quotes, or end tags?

            thanks again, -pladdy

            Comment

            • Atli
              Recognized Expert Expert
              • Nov 2006
              • 5062

              #7
              Hi.

              There are few things that jump at me when I consider you code.
              • Line #1.
                If this is in fact being run through a Apache server, this line shouldn't be needed. The Apache server should know where the PHP executable is and know to execute .php files using it.
                I'm not sure if this actually matters, but I thought it worth mentioning :)
              • Line #8.
                Why do you *print* a HTTP header to the output?
                It shouldn't be sent as a header, but it could be throwing the browser off, which might be causing some problems.
              • Line #15. The action property of your form.
                I assume this URL work on your local network?
                If you are using your own computer to host this, try changing it to:
                Code:
                http://localhost/cgi-bin/ch6ex.php
                Again, I don't know if this will actually change anything, but this is what one typically does to connect to a local HTTP server.
              • And lastly, why do you use the GET protocol? Is the name value being passed on the URL as it should?
                I would try using the POST protocol instead, and of course using the $_POST super-global rather than $_GET.

              Comment

              • pladdypants
                New Member
                • Nov 2008
                • 10

                #8
                Hey there,

                Line #1.
                I removed the shebang but then it won't run, so i put it back.

                Line #8.
                I tried removing this and just putting the following code:

                [PHP]
                #!C:/Program Files/PHP/PHP.exe
                <?php
                error_reporting (E_ALL);
                ini_set('displa y_errors', true);
                ?>

                <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
                "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
                <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
                <head>
                <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
                <title>PHP Test</title>
                </head>
                <body>

                <?php
                //print "Content-type: text/html\n\n";

                if (isset($_GET["name"])) {
                print "Hello, " . $_GET["name"];
                }
                else {
                print <<<HTML
                <form action="http://localhost/cgi-bin/ch6ex.php" method="get">
                Name: <input type="text" name="name" />
                <input type="submit" value="Submit" />
                </form>
                </body>
                </html>
                HTML;
                }
                ?>
                [/PHP]

                But this generates a 500 error and my browser won't open it. I checked the error logs and it's due to a malformed header.

                Line #15.
                I changed this and it's working fine.

                Finally, i'm using get so i can verify that the parameter is being passed which it is. I'm just confused as to how i'm messing this up so badly. It seems from everything I read most people don't have any issues with getting parameters to pass in a script as simple as this.

                Comment

                • Atli
                  Recognized Expert Expert
                  • Nov 2006
                  • 5062

                  #9
                  Thats all extremely odd.
                  I have never heard of Apache needing anything like that first line in order to work.

                  Try this script. See what it does:
                  [code=php]
                  <pre>
                  <?php
                  print_r($_GET);
                  ?>
                  </pre>
                  <form action="?" method="get">
                  <input type="text" name="test"><br >
                  <input type="submit">
                  </form>
                  [/code]
                  Very simple, but effective.
                  If your server is working properly, this should simply print GET variables at the top.

                  Comment

                  • pladdypants
                    New Member
                    • Nov 2008
                    • 10

                    #10
                    Hey again,

                    I tried it twice, here are the error outputs from error log:

                    [Wed Nov 05 22:13:01 2008] [error] [client 127.0.0.1] (9)Bad file descriptor: don't know how to spawn child process: C:/Program Files/Apache Software Foundation/Apache2.2/cgi-bin/Text1.php

                    ...added a shebang line and got this error:
                    [Wed Nov 05 22:13:45 2008] [error] [client 127.0.0.1] malformed header from script. Bad header=Array: Text1.php

                    So i changed the code to this, and it runs but no parameters pass:
                    [PHP]
                    #!C:\Program Files\PHP\PHP.e xe
                    <?php
                    print "Content-type: text/html\n\n";
                    print_r($_GET);
                    ?>
                    <form action="?" method="get">
                    <input type="text" name="test"><br >
                    <input type="submit">
                    </form>
                    [/PHP]

                    It outputs "Array()", then the input and submit button.

                    Perhaps this all means i set up apache poorly? I installed it from a windows installer and have done little to the config files. Really my only goal was to create a basic local server to learn php...

                    -pladdy

                    Comment

                    • Atli
                      Recognized Expert Expert
                      • Nov 2006
                      • 5062

                      #11
                      Yes. This would mean that it is in fact your Apache server that is causing the problem.

                      How did you install PHP, and what version did you install?

                      On the rare occasions that I set up PHP on a Windows machine, I usually just use the MSI installers. I set up Apache first and then have the PHP installer set up the Apache config for it.

                      You can try editing your Apache's httpd.conf file.
                      Adding these lines at the end might help:
                      Code:
                      PHPIniDir "C:/Program Files/PHP/"
                      LoadModule php5_module "C:/Program Files/PHP/php5apache2_2.dll"
                      AddType application/x-httpd-php .php
                      Remember to change the paths to if they don't match your setup.

                      Comment

                      • ak1dnar
                        Recognized Expert Top Contributor
                        • Jan 2007
                        • 1584

                        #12
                        Thread title changed from "parameter wont send (newbie error, sorry)" to "Cannot send parameters" .This will be better than earlier. Please read the posting guidelines provided in FAQ section.

                        MODERATOR

                        Comment

                        • code green
                          Recognized Expert Top Contributor
                          • Mar 2007
                          • 1726

                          #13
                          Are you actually running this in localhost?

                          Comment

                          • pladdypants
                            New Member
                            • Nov 2008
                            • 10

                            #14
                            Adding in PHP/Apache versions

                            Hey everyone,

                            I read posting guidelines, thank you for changing the title. I made the changes suggested by Atli. Code Green, I am running this as localhost. Unfortunately I'm still not getting any parameters passed. I think the consensus is it has to do with the Apache setup. Unless anyone has other suggestions, perhaps I should post on the Apache forum?

                            I installed Apache 2.2.10 first, then PHP 5.2.6. I used windows installers for both. When they installed I just selected all defaults. Php set up apache, and when prompted i selected the 2.2x option. Hope this helps,

                            Thanks again all,

                            -Pladdy
                            Last edited by pladdypants; Nov 6 '08, 01:06 PM. Reason: Left out answer to previous post

                            Comment

                            • pladdypants
                              New Member
                              • Nov 2008
                              • 10

                              #15
                              It was a setup error in the php.ini file. I was placing the php files in the cgi-bin folder of my server rather than htdocs folder. Once the scripts are moved to htdocs they work fine. Thanks all for helping me look in the right direction.

                              -pladdy

                              Comment

                              Working...