Retrieving the multiple values set by a form

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Girish

    Retrieving the multiple values set by a form

    Hi Everyone,

    I am passing a form to a php script for further processing.


    I am able to retrieve the last value set for that given form variable
    using
    $variable=$_REQ UEST['form_variable'];


    My question is, what is the Php way of retrieving all the values passed
    for the same form variable?

    For example, if the php script is called with a syntax like




    , how do I iterate through all the values that form_variable has been
    set to?

    Thanks and regards,
    Girish

  • lorento

    #2
    Re: Retrieving the multiple values set by a form

    Girish wrote:Above request will be read by php like this:
    $_REQUEST["form_varia ble"] = "value1";
    $_REQUEST["form_varia ble"] = "value2";
    $_REQUEST["form_varia ble"] = "value3";

    so only the last value will be the result. Because the first will be
    replaced with second value and second value will be replace with the
    last value. I think its better you use different variable name.


    You can get all the values of above variables.

    ---



    Comment

    • Johnny

      #3
      Re: Retrieving the multiple values set by a form


      "Girish" <girishbhat6620 @gmail.comwrote in message
      news:1159359055 .574104.168360@ h48g2000cwc.goo glegroups.com.. .
      Hi Everyone,
      >
      I am passing a form to a php script for further processing.
      >
      >
      I am able to retrieve the last value set for that given form variable
      using
      $variable=$_REQ UEST['form_variable'];
      >
      >
      My question is, what is the Php way of retrieving all the values passed
      for the same form variable?
      >
      For example, if the php script is called with a syntax like
      >
      >

      _variable=varia ble
      >
      >
      , how do I iterate through all the values that form_variable has been
      set to?
      >
      Thanks and regards,
      Girish
      >
      if register_global s is ON then the variables are already set by php (which,
      BTW, can cause all kinds of unexpected grief when html tag names end up as
      php vars).

      You can test the results by doing this:
      <?php
      if (!empty($_GET)) { # can't use !empty($_REQUES T) as it always has
      something
      echo "before foreach a=".$a.",b=".$b .",c=".$c."< br />"; // if
      register_global s is on these are already set

      # if register_global s is OFF this will set all request vars
      foreach ($_REQUEST as $key =$value){
      $$key = $value;
      }
      echo "after foreach a=".$a.",b=".$b .",c=".$c."< br />";
      }
      else {

      echo <<<FORM
      <form method="get" action="{$_SERV ER['PHP_SELF']}">
      <input type="text" name="a" />
      <input type="text" name="b" />
      <input type="text" name="c" />
      <input type="submit" />
      </form>
      FORM;
      }
      ?>


      but be aware that $_REQUEST has extra stuff in it.


      Comment

      • Ron Barnett

        #4
        Re: Retrieving the multiple values set by a form

        "Girish" <girishbhat6620 @gmail.comwrote in message
        news:1159359055 .574104.168360@ h48g2000cwc.goo glegroups.com.. .
        Hi Everyone,
        >
        I am passing a form to a php script for further processing.
        >
        >
        I am able to retrieve the last value set for that given form variable
        using
        $variable=$_REQ UEST['form_variable'];
        >
        >
        My question is, what is the Php way of retrieving all the values passed
        for the same form variable?
        >
        For example, if the php script is called with a syntax like
        >

        >
        >
        , how do I iterate through all the values that form_variable has been
        set to?
        >
        Thanks and regards,
        Girish
        Hi Girish,

        In the example you have given, which equates to a Get, the variables will
        indeed overwrite each other with only the last value surviving, as stated by
        lorento.

        You can however create an array by suffixing the variable name (in the HTML
        form) this
        <input name='inp[0]' type='hidden' value='first value'>
        <input name='inp[1]' type='hidden' value='second value'>
        <input name='inp[2]' type='hidden' value='third value'>

        once this is received by your PHP script the values may be retrieved :

        $myArray = $_REQUEST('ip') ; // note no subscripts required

        $myArray['0'] will contain 'first value', $myArray['1'] the second value,
        and so on

        BTW Register globals should be OFF on any production machine so there is
        little point in having it on in a development machine.

        HTH

        Ron


        Comment

        • Johnny

          #5
          Re: Retrieving the multiple values set by a form


          "Ron Barnett" <ron@RJBarnett. co.ukwrote in message
          news:451a9129.0 @entanet...
          BTW Register globals should be OFF on any production machine so there is
          little point in having it on in a development machine.
          >
          HTH
          >
          Ron
          >
          >
          Agreed if you have control of the server, however many shared host LAMP
          servers are stuck back at PHP4.x (most likely due to cpanel having not
          caught up yet), and very often have register_global s on by default.
          Of course if those hosts allow .htaccess files you can then turn it off.


          Comment

          • Girish

            #6
            Re: Retrieving the multiple values set by a form

            Ron Barnett wrote:
            In the example you have given, which equates to a Get, the variables will
            indeed overwrite each other with only the last value surviving, as stated by
            lorento.
            >
            Hi Ron and everyone else,

            Thanks for your response. Let me expand a bit on my requirement. I have
            a dynamically generated form where I am now trying something similiar.
            To wit:

            <input name='inp[0]' type='checkbox' value='first value'>
            <input name='inp[1]' type='checkbox' value='second value'>
            <input name='inp[2]' type='checkbox' value='third value'>
            ..
            ..
            etc

            Basically, I present a user with a dynamically generated checklist and
            I am trying to see all the values that he has checked.
            >
            once this is received by your PHP script the values may be retrieved :
            >
            $myArray = $_REQUEST('ip') ; // note no subscripts required
            >
            $myArray['0'] will contain 'first value', $myArray['1'] the second value,
            and so on

            Doing as you have suggested, count($myArray) gives the correct number
            of values ticked.

            But only $myArray['0'] is set!
            $myArray['1'], $myArray['2'] , ..., etc are not set!

            Not to flame or anything, I AM quite surprised by how arcane processing
            a checklist in php is turning out to be. :-)


            Thanks again!
            Girish

            Comment

            • Girish

              #7
              Re: Retrieving the multiple values set by a form

              And this is how I have worked around this problem. Admittedly this is
              not a very elegant solution, but I present it for the benefit of those
              who like me fail to find a better solution and will trawl the archives
              in search of this very workaround in the future. :-)

              In the main form I have,

              <input name='"choice0' "type="checkbox " value='"first value">
              <input name='"choice1" type="checkbox" value='"second value">
              <input name='"choice2" type="checkbox" value='"third value">


              etc.
              plus a hidden input field
              <input name="displayed choices" type="hidden" value='"3"//equal to the
              number displayed.

              In the processing script

              $choicesDisplay ed = $_REQUEST['displayedchoic es'];
              for($i=0; $i< $choicesDisplay ed; $i++)
              {
              $read = 'choice'.$i;
              $setvalue = $_REQUEST[$read];
              echo "Value of $read is ".$setvalue ;
              echo "<br>\n";
              }


              Cheers,
              Girish

              Comment

              • anthony@mypetprogrammer.com

                #8
                Re: Retrieving the multiple values set by a form

                I have to wonder why you're using GET at all for form submission. If
                you used post, you could just pass in an array:

                <form method="POST" action="somesec ript.php">
                <input type="text" name="values[]"/>
                <input type="text" name="values[]"/>
                </form>

                And in the processing:

                foreach ($_POST["values"]):

                // do something with each value

                endforeach;

                ~A!


                lorento wrote:
                Girish wrote:>
                Above request will be read by php like this:
                $_REQUEST["form_varia ble"] = "value1";
                $_REQUEST["form_varia ble"] = "value2";
                $_REQUEST["form_varia ble"] = "value3";
                >
                so only the last value will be the result. Because the first will be
                replaced with second value and second value will be replace with the
                last value. I think its better you use different variable name.
                >

                You can get all the values of above variables.
                >
                ---

                http://www.theaussiemap.com

                Comment

                • Jerry Stuckle

                  #9
                  Re: Retrieving the multiple values set by a form

                  Ron Barnett wrote:
                  "Girish" <girishbhat6620 @gmail.comwrote in message
                  news:1159359055 .574104.168360@ h48g2000cwc.goo glegroups.com.. .
                  >
                  >>Hi Everyone,
                  >>
                  >>I am passing a form to a php script for further processing.
                  >>
                  >>
                  >>I am able to retrieve the last value set for that given form variable
                  >>using
                  > $variable=$_REQ UEST['form_variable'];
                  >>
                  >>
                  >>My question is, what is the Php way of retrieving all the values passed
                  >>for the same form variable?
                  >>
                  >>For example, if the php script is called with a syntax like
                  >>
                  >>http://xxxx/get_variables.php?form_v...iable=variable
                  >>
                  >>
                  >>, how do I iterate through all the values that form_variable has been
                  >>set to?
                  >>
                  >>Thanks and regards,
                  >>Girish
                  >
                  >
                  Hi Girish,
                  >
                  In the example you have given, which equates to a Get, the variables will
                  indeed overwrite each other with only the last value surviving, as stated by
                  lorento.
                  >
                  You can however create an array by suffixing the variable name (in the HTML
                  form) this
                  <input name='inp[0]' type='hidden' value='first value'>
                  <input name='inp[1]' type='hidden' value='second value'>
                  <input name='inp[2]' type='hidden' value='third value'>
                  >
                  once this is received by your PHP script the values may be retrieved :
                  >
                  $myArray = $_REQUEST('ip') ; // note no subscripts required
                  >
                  $myArray['0'] will contain 'first value', $myArray['1'] the second value,
                  and so on
                  >
                  BTW Register globals should be OFF on any production machine so there is
                  little point in having it on in a development machine.
                  >
                  HTH
                  >
                  Ron
                  >
                  >
                  Actually, it's even easier than that. No need to specify the index when
                  you're using the default.

                  <input name='inp[]' type='hidden' value='first value'>
                  <input name='inp[]' type='hidden' value='second value'>
                  <input name='inp[]' type='hidden' value='third value'>

                  inp[0] == 'first value'
                  inp[1] == 'second value'
                  inp[2] == 'third value'

                  --
                  =============== ===
                  Remove the "x" from my email address
                  Jerry Stuckle
                  JDS Computer Training Corp.
                  jstucklex@attgl obal.net
                  =============== ===

                  Comment

                  • Jerry Stuckle

                    #10
                    Re: Retrieving the multiple values set by a form

                    Girish wrote:
                    Ron Barnett wrote:
                    >
                    >>In the example you have given, which equates to a Get, the variables will
                    >>indeed overwrite each other with only the last value surviving, as stated by
                    >>lorento.
                    >>
                    >
                    >
                    Hi Ron and everyone else,
                    >
                    Thanks for your response. Let me expand a bit on my requirement. I have
                    a dynamically generated form where I am now trying something similiar.
                    To wit:
                    >
                    <input name='inp[0]' type='checkbox' value='first value'>
                    <input name='inp[1]' type='checkbox' value='second value'>
                    <input name='inp[2]' type='checkbox' value='third value'>
                    .
                    .
                    etc
                    >
                    Basically, I present a user with a dynamically generated checklist and
                    I am trying to see all the values that he has checked.
                    >
                    >
                    >>once this is received by your PHP script the values may be retrieved :
                    >>
                    >>$myArray = $_REQUEST('ip') ; // note no subscripts required
                    >>
                    >>$myArray['0'] will contain 'first value', $myArray['1'] the second value,
                    >>and so on
                    >
                    >
                    >
                    Doing as you have suggested, count($myArray) gives the correct number
                    of values ticked.
                    >
                    But only $myArray['0'] is set!
                    $myArray['1'], $myArray['2'] , ..., etc are not set!
                    >
                    Not to flame or anything, I AM quite surprised by how arcane processing
                    a checklist in php is turning out to be. :-)
                    >
                    >
                    Thanks again!
                    Girish
                    >
                    Girish,

                    First of all, it should be $myArray[0], not $myArray['0']. It has a
                    numeric index. And the fact count($myArray) has the correct value
                    indicates the values are correct.

                    Also, the way you have it coded, if the 'second value' checkbox is not
                    checked, myValue[1] will not be set and you'll get a notice if you try
                    to use it. Unless you absolutely have to have the indexes like that, a
                    better way is to use:

                    <input name='inp[]' type='checkbox' value='first value'>
                    <input name='inp[]' type='checkbox' value='second value'>
                    <input name='inp[]' type='checkbox' value='third value'>

                    Now inp[0] will contain the value from the first checked box, imp[1]
                    will contain the value from the second checked box, etc.

                    Also, I agree with Anthony but for different reasons. I suggest you use
                    POST instead of GET. Not because of the array (it should work in
                    either), but because it can get to be quite a long URL if the user
                    checks several boxes.

                    --
                    =============== ===
                    Remove the "x" from my email address
                    Jerry Stuckle
                    JDS Computer Training Corp.
                    jstucklex@attgl obal.net
                    =============== ===

                    Comment

                    • Girish

                      #11
                      Re: Retrieving the multiple values set by a form


                      Jerry Stuckle wrote:
                      >
                      Girish,
                      >
                      First of all, it should be $myArray[0], not $myArray['0']. It has a
                      numeric index. And the fact count($myArray) has the correct value
                      indicates the values are correct.
                      >
                      Hi Jerry, I had tried both $myArray[0], not $myArray['0'], behaviour
                      was the same in both cases.
                      "
                      Also, the way you have it coded, if the 'second value' checkbox is not
                      checked, myValue[1] will not be set and you'll get a notice if you try
                      to use it. Unless you absolutely have to have the indexes like that, a
                      better way is to use:
                      >
                      <input name='inp[]' type='checkbox' value='first value'>
                      <input name='inp[]' type='checkbox' value='second value'>
                      <input name='inp[]' type='checkbox' value='third value'>
                      >
                      Now inp[0] will contain the value from the first checked box, imp[1]
                      will contain the value from the second checked box, etc.
                      >
                      Ah. gracias. This probably would have worked. The reason I was using
                      POST was just that GET is easier to debug AND to save as a link to the
                      report page.

                      thanks everyone!
                      Girish

                      Comment

                      Working...