caputuring the var on address bar

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ddtpmyra
    Contributor
    • Jun 2008
    • 333

    caputuring the var on address bar

    I have 1st php script where i assigned the value 'id' on the next page where this time I use html coding. My question is how can you capture the id number (see below quote) on html code to pass it to the next page with php script this time?

    http://myrab.ddtp.org/add_reviewer.ph p?id=1
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    This is not possible with HTML. You can either use PHP or JavaScript (if PHP isn't an option) to retrieve the value.

    Comment

    • numberwhun
      Recognized Expert Moderator Specialist
      • May 2007
      • 3467

      #3
      I fully agree with acoder. HTML/CSS are only for the display of the web page and not able to process values passed between pages. That is what scripting languages, such as PHP and Javascript, are for. You will need to use one of them to capture the information passed.

      Regards,

      Jeff

      Comment

      • Markus
        Recognized Expert Expert
        • Jun 2007
        • 6092

        #4
        Moving to PHP forum.

        Comment

        • Markus
          Recognized Expert Expert
          • Jun 2007
          • 6092

          #5
          You can access variables in the url by $_GET['key']. In your case it would be $_GET['id']

          Markus.

          Comment

          • gabosom
            New Member
            • Sep 2008
            • 9

            #6
            I feel kind of confused with your question, so I'm going to answer how to pass the variable with and without PHP.

            With PHP:

            use the $_REQUEST array to get the information defined, in your case, it's $_REQUEST["id"]

            WIthout PHP:
            You can use javascript to parse the URL, var url = window.location and you'd get the url. You can parse it and then use the variable stored in JS or forward to another page.

            Comment

            • Atli
              Recognized Expert Expert
              • Nov 2006
              • 5062

              #7
              Just to clarify.

              The passing of variables via the URL, as you demonstrated, is referred to as the HTTP GET protocol.

              In PHP, you get these sort of variables via the $_GET super-global, like Markus demonstrated.

              There are other HTTP protocols, such as the POST protocol, which PHP makes available via the $_POST super-global.

              Both of these, as well as all cookies, are combined into the $_REQUEST super-global. As a result, when you fetch the variable "id" from the $_REQUEST super-global, that element can be coming from any of these protocols.

              Using the $_GET, $_POST or $_COOKIES super-globals is generally better than using the $_REQUEST super-global, because that way you know exactly where you data is coming from.

              Comment

              • Markus
                Recognized Expert Expert
                • Jun 2007
                • 6092

                #8
                Originally posted by Atli
                Using the $_GET, $_POST or $_COOKIES super-globals is generally better than using the $_REQUEST super-global, because that way you know exactly where you data is coming from.
                I wonder how $_REQUEST would act if it found 'id' in GET and POST?

                Comment

                • ddtpmyra
                  Contributor
                  • Jun 2008
                  • 333

                  #9
                  Thanks for all your help :)
                  I just use $POST

                  Comment

                  • Atli
                    Recognized Expert Expert
                    • Nov 2006
                    • 5062

                    #10
                    Originally posted by Markus
                    I wonder how $_REQUEST would act if it found 'id' in GET and POST?
                    To quote the default PHP.ini configuration file. (PHP 5.2.6)
                    Originally posted by php.ini
                    ; This directive describes the order in which PHP registers GET, POST, Cookie,
                    ; Environment and Built-in variables (G, P, C, E & S respectively, often
                    ; referred to as EGPCS or GPC). Registration is done from left to right, newer
                    ; values override older values.
                    variables_order = "EGPCS"
                    So basically, Session variables override all other values. Cookies are second, then POST, GET and finally Environment.

                    So in your situation, you would get the ID from POST.

                    Comment

                    • acoder
                      Recognized Expert MVP
                      • Nov 2006
                      • 16032

                      #11
                      Originally posted by gabosom
                      With PHP:

                      use the $_REQUEST array to get the information defined, in your case, it's $_REQUEST["id"]
                      Atli's already responded to this - it's better to use a specific super-global rather than $_REQUEST.

                      Originally posted by gabosom
                      WIthout PHP:
                      You can use javascript to parse the URL, var url = window.location and you'd get the url. You can parse it and then use the variable stored in JS or forward to another page.
                      You can use something more specific here too - window.location .search - see link.

                      Comment

                      • acoder
                        Recognized Expert MVP
                        • Nov 2006
                        • 16032

                        #12
                        Originally posted by ddtpmyra
                        Thanks for all your help :)
                        I just use $POST
                        Shouldn't that be $_GET ?

                        Comment

                        • ddtpmyra
                          Contributor
                          • Jun 2008
                          • 333

                          #13
                          Here I go again....

                          now it's not working and it's weird maybe you can find what's wrong in between.


                          here's my 1st page where my submit button is..
                          [PHP]<input name="id" type="hidden" id="id" value="<?id=$_G ET['id']?>">[/PHP]

                          here's my insert SQL
                          [PHP]$id=$_POST['id'];
                          etc....

                          $sql="INSERT INTO $tbl_name(topic , detail, name, email, datetime, cmrid)
                          VALUES('$topic' , '$detail', '$name', '$email', '$datetime','$i d')";
                          $result=mysql_q uery($sql) or die(mysql_error ());[/PHP]

                          and here's my 2nd page where I got the error message
                          myraYou have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'id']?>')' at line 2

                          Comment

                          • Atli
                            Recognized Expert Expert
                            • Nov 2006
                            • 5062

                            #14
                            Try replacing:
                            [code=php]
                            <?id=$_GET['id']?>
                            [/code]
                            with:
                            [code=php]
                            <?php echo $_GET['id']; ?>
                            [/code]
                            The former uses the short-tags PHP block (incorrectly by the way), which is not enabled in PHP by default, and should therefore be avoided.
                            If that is the case, then, rather than printing the result of the PHP code, it would simply display the PHP code, which would mean that the PHP code would be passed into your query, obviously causing an error.

                            Comment

                            • ddtpmyra
                              Contributor
                              • Jun 2008
                              • 333

                              #15
                              Your are right Atli thanks!

                              Comment

                              Working...