How to keep $_POST variable into array format.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • luckysanj
    New Member
    • May 2009
    • 68

    How to keep $_POST variable into array format.

    For example i have two files one in index.php & another is check.php.
    index.php
    [code=html]
    <body>
    <form id="form1" name="form1" method="post" action="check.p hp?id=tbl1">
    <table width="200" border="1">
    <tr>
    <td colspan="2">Pes onal Info Table 1 </td>
    </tr>
    <tr>
    <td width="72">Name :</td>
    <td width="112"><in put name="name" type="text" id="name" value="Ashok" /></td>
    </tr>
    <tr>
    <td>Address</td>
    <td><input name="address" type="text" id="address" value="Kathmand u" /></td>
    </tr>
    <tr>
    <td><input name="tbl1" type="submit" id="tbl1" value="Submit" /></td>
    <td><input type="reset" name="Submit2" value="Reset" /></td>
    </tr>
    </table>
    </form>
    </br>
    </br>
    <form id="form3" name="form3" method="post" action="check.p hp?id=tbl2">
    <table width="216" border="1">
    <tr>
    <td colspan="2">Per sonal Info Table 3 </td>
    </tr>
    <tr>
    <td width="56">Phon e</td>
    <td width="144"><in put name="phone" type="text" id="phone" value="98416143 20" /></td>
    </tr>
    <tr>
    <td>Email</td>
    <td><input name="email" type="text" id="email" value="alok_in1 @yahoo.com" /></td>
    </tr>
    <tr>
    <td><input name="tbl2" type="submit" id="tbl2" value="Submit" /></td>
    <td><input type="reset" name="Submit6" value="Reset" /></td>
    </tr>
    </table>
    </form>
    </body>
    [/code]

    & Second is check.php
    [code=php]
    foreach($_POST as $key=>$value)
    {
    //echo "</br>";
    $key.":".$$key= $value;
    //echo "</br>".$key;
    }

    [/code]

    So the problem is that, when we fetch the data from $_POST method. how to keep the key & value variable into different array.
    like:
    [code=php]$field=array('n ame,'address ',......');
    &
    $value=array('A shok','Kathmand u','... '); [/code]

    Thank you.
  • Canabeez
    New Member
    • Jul 2009
    • 126

    #2
    [code=php]
    $KeysArray = array_keys($_PO ST);
    $ValuesArray = array_values($_ POST);[/code]

    You mean this?

    Comment

    • luckysanj
      New Member
      • May 2009
      • 68

      #3
      ya sir,

      In the above code i am trying to fetch the table name through browser by using $_GET method.
      [code=html]
      <form id="form1" name="form1" method="post" action="check.p hp?id=tbl1">
      [/code]

      & Field & Values from $_POST.


      Now I want. the table name is also fetch from $_POST & table fields & values also fetch from $_POST. so how can i pass the table name from index page.

      And last
      [code=php]
      $KeysArray = array_keys($_PO ST);
      $ValuesArray = array_values($_ POST);
      [/code]

      It works but how to ingnore the last value of key & values. Because it is not necesssary for insert the database.

      Thanks

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        if you know the index you can unset it. or (and to make it more secure) use PHP Filters to sanitize the post data.

        Comment

        • luckysanj
          New Member
          • May 2009
          • 68

          #5
          & how can i ignore the last key & value of $_POST variable.

          Comment

          • Markus
            Recognized Expert Expert
            • Jun 2007
            • 6092

            #6
            Originally posted by luckysanj
            & how can i ignore the last key & value of $_POST variable.
            array_pop() it off?

            Comment

            • luckysanj
              New Member
              • May 2009
              • 68

              #7
              Thank You Markus. .

              Comment

              • Dormilich
                Recognized Expert Expert
                • Aug 2008
                • 8694

                #8
                Originally posted by luckysanj
                & how can i ignore the last key & value of $_POST variable.
                do you have proof that you actually need to remove the last key & value?

                Comment

                • luckysanj
                  New Member
                  • May 2009
                  • 68

                  #9
                  ya because the the last value & key are not used for insert the database.

                  Comment

                  • Dormilich
                    Recognized Expert Expert
                    • Aug 2008
                    • 8694

                    #10
                    Originally posted by luckysanj
                    ya because the the last value & key are not used for insert the database.
                    that's the reason, but is it proof?

                    Comment

                    • luckysanj
                      New Member
                      • May 2009
                      • 68

                      #11
                      I don't understand what do say sir? but..

                      There are differend form on main page. & I want to insert data from different form through one InsertData() function. So i need to pass the value & key of $_POST variable to insert function.

                      Thank you.

                      Comment

                      • Dormilich
                        Recognized Expert Expert
                        • Aug 2008
                        • 8694

                        #12
                        Originally posted by luckysanj
                        I don't understand what do say sir? but..
                        I'm just saying that the POST array may look different depending on the circumstances. maybe a user agent sends you the POST array ordered or reversed or scrambled. therefore the last key/value pair is not necessarily the same in every circumstance.

                        Comment

                        • Dheeraj Joshi
                          Recognized Expert Top Contributor
                          • Jul 2009
                          • 1129

                          #13
                          Yes.. I think we simply cant avoid the last element in the array..

                          Sometime back i too got some problems.

                          To avoid few parameters going into database we just checked all parameter then added it to DB.

                          Last element is not always what we expected.

                          Comment

                          • luckysanj
                            New Member
                            • May 2009
                            • 68

                            #14
                            then what i do? .

                            Comment

                            • Dormilich
                              Recognized Expert Expert
                              • Aug 2008
                              • 8694

                              #15
                              if you know, what key it is you can unset it (or the other way round, if you know all other keys you can apply a filter to only preserve those)

                              Comment

                              Working...