Problem extracting data from form

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Hamayun Khan
    New Member
    • Aug 2007
    • 106

    Problem extracting data from form

    Hi

    I m using form like below
    [HTML]<form action='' method = post>
    <input type=text name=txt id=txt>
    <select name=region id=region multiple>
    <option value=1>ONE</option>
    <option value=2>Two</option>
    <option value=3>Three</option>
    <option value=4>Four</option>
    </select>
    <input type=checkbox name=chkbox value=1>One
    <input type=checkbox name=chkbox value=2>Two
    <input type=checkbox name=chkbox value=3>Three
    <input type=checkbox name=chkbox value=4>Four
    <input type=submit name=summit value=Enter>
    </form>[/HTML]
    and I use the following code to get the values
    [PHP]
    echo "Text Field value = ". $_POST['txt'] . "<br>";
    echo "Select Field values=".$_POST['region']. "<br>";
    echo "Checkbox Field values=".$_POST['chkbox']. "<br>";
    [/PHP]
    Let I enter Hello in Text field, select One and Two from select field and Check Three and Four Check Boxes then the above PHP code show the following results

    Text Field value=Hello
    Select Field values=2
    Checkbox Field values=4

    where as I expect the follwoing

    Text Field value=Hello
    Select Field values=1,2
    Checkbox Field values=3,4

    Looking forword for Help
    Thanks in advance
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    When you give something a value you need to use quotations
    Code:
    option value="1"
    Could you do a print_r($_POST) on the page that receives the submitted values and show us the output.

    You shouldnt be able access $_POST['chxbox'] by simply doing that, because it should be a multidimensiona l array, as you have given all checkboxes the same name.

    Comment

    • hsriat
      Recognized Expert Top Contributor
      • Jan 2008
      • 1653

      #3
      When you are submitting variables with same name, you got to submit them as array.

      Use this instead...
      [HTML]<form action='' method = post>
      <input type=text name=txt id=txt>
      <select name="region[]" id=region multiple>
      <option value=1>ONE</option>
      <option value=2>Two</option>
      <option value=3>Three</option>
      <option value=4>Four</option>
      </select>
      <input type=checkbox name="chkbox[]" value=1>One
      <input type=checkbox name="chkbox[]" value=2>Two
      <input type=checkbox name="chkbox[]" value=3>Three
      <input type=checkbox name="chkbox[]" value=4>Four
      <input type=submit name=summit value=Enter>
      </form>[/HTML]
      On the action page, write
      [php]echo "<pre>";
      print_r($_POST)
      echo "</pre>";[/php] in the start and you can come to know how to manipulate the submitted data according to your requirement.

      Regards,
      Harpreet

      PS: Keeping values of attributes in quotes is really important for a proper mark-up, as markus said.

      Comment

      • Hamayun Khan
        New Member
        • Aug 2007
        • 106

        #4
        Thanks for replying to All.

        First Thing I don't want to print the values. This was just an example. If I want to save the values into database. If i set the the the method attribute of the form to get then I see the following URL

        page.php?txt=He llo&region=1&re gion=2&chkbox=3 &chkbox=4

        I seems not an array.

        Second I also I also tried the following

        [PHP]

        echo $_POST['region'];
        echo $_POST['region'][0];
        echo $_POST['region'][1];
        [/PHP]

        Here the first and 2nd statements print same values while the 3rd statement doesn't print any thing. ( Assume that I select one and Two form List).

        Third In ASP Every thing is OK.


        response.write( request.queryst ring("region"))

        this prints

        1,2 And I need the same from PHP.

        Please some more help

        Comment

        • hsriat
          Recognized Expert Top Contributor
          • Jan 2008
          • 1653

          #5
          I didn't force you to print the values. It was just so that you could understand in what form is the data received by PHP, as this is not ASP (but far better than ASP)

          and I think you didn't try what I said...

          <select name="region[ ]" id=region multiple>

          And last of all, use method="post" instead of get.

          Read the last post carefully and try what I said. That may help you.

          Comment

          • rpnew
            New Member
            • Aug 2007
            • 189

            #6
            Hi,
            All the above posts asked you to do this....
            Code:
            <form action='test.php' method = post>
            
            			  <input type=text name=txt id=txt>
            
            			  <select name=region[] id=region multiple>
            
            				  <option value=1>ONE</option>
            
            				  <option value=2>Two</option>
            
            				  <option value=3>Three</option>
            
            				  <option value=4>Four</option>
            
            			  </select>
            
            			  <input type=checkbox name=chkbox[] value=1>One
            	
            			  <input type=checkbox name=chkbox[] value=2>Two
            
            			  <input type=checkbox name=chkbox[] value=3>Three
            
            			  <input type=checkbox name=chkbox[] value=4>Four
            
            			  <input type=submit name=summit value=Enter>
            
            		  </form>
            To get the exact output you wanted i manipulated your PHP script a bit like this...
            Code:
            	echo "Text Field value = ". $_POST['txt'] . "<br>";
            	echo "Select Field values=".implode(",",$_POST['region']). "<br>";
            	 echo "Checkbox Field values=".implode(",",$_POST['chkbox']). "<br>";
            Implode will convert your array into string... and for reverse you can use Explode . To directly access array posted you can use foreach loop.

            Regards,
            RP

            Comment

            • Hamayun Khan
              New Member
              • Aug 2007
              • 106

              #7
              Originally posted by hsriat
              I didn't force you to print the values. It was just so that you could understand in what form is the data received by PHP, as this is not ASP (but far better than ASP)

              and I think you didn't try what I said...

              <select name="region[ ]" id=region multiple>

              And last of all, use method="post" instead of get.

              Read the last post carefully and try what I said. That may help you.
              Thanks for helping

              <select name="region[ ]" id=region multiple>

              I really forget to place [] braces after the name.

              Thanks once again

              Comment

              • hsriat
                Recognized Expert Top Contributor
                • Jan 2008
                • 1653

                #8
                You are welcome :)

                Comment

                • Markus
                  Recognized Expert Expert
                  • Jun 2007
                  • 6092

                  #9
                  I read somewhere that assigning the same name to an element automatically turned it into an array (no need for []).

                  Hm, I must've read incorrectly.

                  This post could've been resolved in your first reply, hs. If only people would listen :P

                  Comment

                  • hsriat
                    Recognized Expert Top Contributor
                    • Jan 2008
                    • 1653

                    #10
                    Not in PHP.... might be in ASP.

                    Try this... [php]//show_post.php
                    if (!isset($_POST['submit']))
                    {
                    echo "<from action=\"show_p ost.php\" method=\"post\" >";
                    for ($i=0; $i<10; $i++)
                    echo "<input type=\"text\" name=\"markus\" >";
                    //For the second time, use markus[] instead of markus
                    echo "<input type=\"submit\" name=\"submit\" value=\"Let's test\"></form>";
                    }
                    else
                    {
                    echo "<pre>";
                    print_r($_POST) ;
                    echo "</pre>";
                    }
                    ?>[/php]
                    Elaboration ;)

                    ... yeh... you are right... just in first reply..

                    Comment

                    Working...