Wampserver 2.0 and php

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • fatroshi
    New Member
    • Dec 2009
    • 12

    Wampserver 2.0 and php

    Hi,
    I´ve just started using php, and i´m trying to make it work with wampserver!
    I´m using wamperver 2 , PHP 5.30
    on windows 7


    I have installed wampserver correctly and the localhost seems to work.
    When i try this script:

    <?php echo "something" ?>
    It works!

    But when I try to send information from a html file to php file, the varibel doesn't go throw. I get this message: Notice: Undefined variable: name in C:\wamp\www\php \results.php on line 3

    Please guys help me out :(



    Code:
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title> Connect To Database</title>
    </head>
    <body>
    
    <form action="results.php" method="post">
    <input name="name" type="text" value="name">
    <input name="" type="submit" value="search">
    </form>
    
    </body>
    </html>
    Code:
    <?php
    echo $name;
    ?>
    Last edited by Dormilich; Dec 11 '09, 05:10 AM. Reason: fixed [code] tags
  • kovik
    Recognized Expert Top Contributor
    • Jun 2007
    • 1044

    #2
    The form uses the method "post," as you can see in the <form> element's attributes. So, you will find the values from the form in the $_POST superglobal array in PHP.

    Code:
    if (!empty($_POST)) { // A form has been submitted
        if (!empty($_POST['name'])) {
            echo $_POST['name'];
        }
    }

    Comment

    • fatroshi
      New Member
      • Dec 2009
      • 12

      #3
      Thanks for the help kovik =)

      Now there are no errors, but the browser page is emty! It doesn´t show anything...don´ t know why!

      Comment

      • kovik
        Recognized Expert Top Contributor
        • Jun 2007
        • 1044

        #4
        Try to print_r() the $_POST array to see if anything has been submitted.

        Code:
        print_r($_POST);

        Comment

        • fatroshi
          New Member
          • Dec 2009
          • 12

          #5
          No, nothing.

          I tried this:

          Code:
          <?
          if (!empty($_POST)) { // A form has been submitted
               if (!empty($_POST['name'])) {
                echo $_POST['name'];
              }
           }
          
          print_r($_POST);
          ?>
          Last edited by Dormilich; Dec 11 '09, 05:12 AM. Reason: fixed [code] tags

          Comment

          • kovik
            Recognized Expert Top Contributor
            • Jun 2007
            • 1044

            #6
            Then that means that nothing has been submitted. The $_POST array only has data after you press the submit button on a form that posts to the page.

            Comment

            • fatroshi
              New Member
              • Dec 2009
              • 12

              #7
              Ok, I see. But script is right for both php and html right?
              So what is the problem then, what have i done wrong?

              As I said before when i just "echo" something it does show up..
              But of course I understand it´s difficult for you to point the problem out with the information provided by me.

              Thanks anywaym, please let me know if you get an new idea =)

              Comment

              • kovik
                Recognized Expert Top Contributor
                • Jun 2007
                • 1044

                #8
                What I'm trying to tell you is that you have to test this page AFTER pressing the submit button...

                A better way to do it is to keep it in the same page:

                Code:
                <?php
                print_r($_POST);
                ?>
                <form method="post" action="#">
                  <input type="text" name="name" value="" />
                  <button type="submit">Submit</button>
                </form>

                Comment

                • fatroshi
                  New Member
                  • Dec 2009
                  • 12

                  #9
                  ok sorry, didn´t understand that. This is very new for me =p

                  I tried that out and got this message in the browser when I typed hello;
                  Array ( [name] => hello )

                  Comment

                  • kovik
                    Recognized Expert Top Contributor
                    • Jun 2007
                    • 1044

                    #10
                    Good. That means that if you echo $_POST['name'], it would output "hello."

                    Comment

                    • fatroshi
                      New Member
                      • Dec 2009
                      • 12

                      #11
                      yes it does, it (action = "#") but not when send it to another page.
                      But I can live with that for now I guess.

                      thanks a lot =D

                      Comment

                      • kovik
                        Recognized Expert Top Contributor
                        • Jun 2007
                        • 1044

                        #12
                        It should work fine going from one page to another. Not sure what went wrong with yours, but it is still common practice to submit forms to the same page

                        Comment

                        • fatroshi
                          New Member
                          • Dec 2009
                          • 12

                          #13
                          I dont know why it doesn´t work, I use this script for the html and php file:

                          Code:
                          <body>
                          <?php
                          echo $_POST['name']
                          ?>
                          
                          <form action="#" method="post">
                          <input name="name" type="text" value="name">
                          <input name="" type="submit" value="search">
                          </form>
                          
                          </body>
                          Code:
                          <?
                          if (!empty($_POST)) { // A form has been submitted
                               if (!empty($_POST['name'])) {
                                echo $_POST['name'];
                              }
                           }
                          echo $_POST['name']
                          ?>
                          Last edited by Dormilich; Dec 11 '09, 06:22 AM. Reason: fixed [code] tags

                          Comment

                          • kovik
                            Recognized Expert Top Contributor
                            • Jun 2007
                            • 1044

                            #14
                            The "action" attribute of the <form> element must correspond to the file that should be receiving the data.

                            Comment

                            • fatroshi
                              New Member
                              • Dec 2009
                              • 12

                              #15
                              Tried that 2 doesnt work. :/
                              Programming can´t be this hard lol

                              meybe it´s something wrong with wampserver.. dont know..

                              Comment

                              Working...