How to get all set $_post ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Alino
    New Member
    • Aug 2009
    • 25

    How to get all set $_post ?

    So I have a javascript on my page, which makes me able to add another input text field when I click on a button. And it's named "test" when I click the button it creates another input named test1, on another click it creates test2 and so on... But I have a problem on my form processing page, I need to get know which is the last "test" and also I need to set variables for each of them. I imagine some small code which will make something like this for me:

    $test = $_POST('test');
    $test2 = $_POST('test2') ;
    $test3 = $_POST('test3') ;
    $test4 = $_POST('test4') ;

    And it will go on until last test. Thanks for helping me.
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    Originally posted by Alino
    So I have a javascript on my page, which makes me able to add another input text field when I click on a button. And it's named "test" when I click the button it creates another input named test1, on another click it creates test2 and so on... But I have a problem on my form processing page, I need to get know which is the last "test" and also I need to set variables for each of them. I imagine some small code which will make something like this for me:

    $test = $_POST('test');
    $test2 = $_POST('test2') ;
    $test3 = $_POST('test3') ;
    $test4 = $_POST('test4') ;

    And it will go on until last test. Thanks for helping me.
    There is a solution to this problem of yours - you can have your input elements become an array, that is to say, you can access them as an array inside the post array. To do this, you simply name the elements the same, and append '[]' to them, like so:

    Code:
    <input name="my_array[]" value="1" />
    <input name="my_array[]" value="2" />
    <input name="my_array[]" value="3" />
    <input name="my_array[]" value="4" />
    And to access those elements in PHP, you do something like so:

    Code:
    print_r($_POST['my_array']);
    
    foreach($_POST['my_var'] as $key => $my_var) {
        printf("Key #%d has the value <strong>%s</strong>", $key, $my_var);
    }
    Mark.

    Comment

    • Alino
      New Member
      • Aug 2009
      • 25

      #3
      thank you man, it works

      Comment

      • Markus
        Recognized Expert Expert
        • Jun 2007
        • 6092

        #4
        Originally posted by Alino
        thank you man, it works
        No problamo.

        Mark.

        Comment

        • Alino
          New Member
          • Aug 2009
          • 25

          #5
          now I have problem with imploding it
          Code:
          implode(" ", $pod_albumom_photo_id);
          I receive the following error message then:
          Warning: implode() [function.implod e]: Invalid arguments passed in /storage/www1/9/site46049/wwwroot/xxx/process.php on line 90
          what's wrong? ;[ how to fix it?

          Comment

          • Markus
            Recognized Expert Expert
            • Jun 2007
            • 6092

            #6
            Seems like $pod_albumom_ph oto_id is not an array. What does var_dump($pod_a lbumom_photo_id ); produce?

            Comment

            • Alino
              New Member
              • Aug 2009
              • 25

              #7
              it produces
              string(4) "12"
              thats number of my inputs (4) and value from the last. "12"

              Comment

              • Markus
                Recognized Expert Expert
                • Jun 2007
                • 6092

                #8
                Originally posted by Alino
                it produces

                thats number of my inputs (4) and value from the last. "12"
                No, that can't be right; 4 should be the length of the string, and it says 'string' not 'array'. :S

                Comment

                • Alino
                  New Member
                  • Aug 2009
                  • 25

                  #9
                  so what i have to do to have it in array? :P

                  Comment

                  • dlite922
                    Recognized Expert Top Contributor
                    • Dec 2007
                    • 1586

                    #10
                    Originally posted by Alino
                    so what i have to do to have it in array? :P
                    put empty brackets at the html form input name.

                    Comment

                    • Alino
                      New Member
                      • Aug 2009
                      • 25

                      #11
                      i have done that already, my code looks like
                      Code:
                      <script type="text/javascript">
                      
                      function addfile()
                      {
                      var obj = document.getElementById("sprytextfield2").cloneNode(true);
                      document.getElementById('upfiles').appendChild(document.createElement('br') );
                      document.getElementById('upfiles').appendChild(obj);
                      }
                      
                      </script>
                      Code:
                      		<fieldset class="border" id="upfiles">
                      		<legend>Vymazať fotku v profile pod albumom:</legend>
                      		<label for="pod_albumom_photo_id[]">ID fotky:</label><br />
                      		<span id="sprytextfield2">
                              <input type="text" name="pod_albumom_photo_id[]" value="" onKeyPress="ipt_onkeypress()" />
                      <span class="textfieldInvalidFormatMsg">Iba cifry prosím. </span></span>
                      		<input class="button" type="button" value="Pridať pole" onclick="return addfile()" />
                      		</fieldset>
                      and in my form process page:
                      Code:
                      			foreach($_POST['pod_albumom_photo_id'] as $key => $pod_albumom_photo_id)
                      			{
                          		printf("Foto s ID <strong>%s</strong> bude vymazaná<br />", $pod_albumom_photo_id);
                      			}
                      			$x = implode(" ", $pod_albumom_photo_id);
                      now i just need to get it work, that implode.

                      Comment

                      • dlite922
                        Recognized Expert Top Contributor
                        • Dec 2007
                        • 1586

                        #12
                        When you do a foreach, you're already "imploding" in a sense.

                        $_POST['pod_albumom_ph oto_id'] /is/ an array (do a var dump if you don't' believe me)

                        What do foreach's do? traverse arrays? to what? strings. thus $pod_albumom_ph oto_id is a string.

                        If you want it to be an array, just assign what's in the post:

                        $pod_albumom_ph oto_id = $_POST['pod_albumom_ph oto_id'];

                        TADA! you've got an array now you can implode.



                        Dan

                        Comment

                        • Alino
                          New Member
                          • Aug 2009
                          • 25

                          #13
                          man thank you, you make my day :) it just works !!! :D:D

                          Comment

                          • Alino
                            New Member
                            • Aug 2009
                            • 25

                            #14
                            my next problem is creating link of this $pod_albumom_ph oto_id
                            I wanted it to implode to be able to make link like
                            implode($iframe _part1 . "http://www.mywebpage.c om/x.php?action=10 0&uid=" .$user_id. "&iid=", $pod_albumom_ph oto_id);
                            but then i noticed that i am not able to make those links because i can't add
                            $iframe_part2
                            after array. How to solve this?

                            Comment

                            • dlite922
                              Recognized Expert Top Contributor
                              • Dec 2007
                              • 1586

                              #15
                              I don't get your question. Why can't you add a variable?

                              It looks like you're doing complex string building, why not stick with the foreach?

                              What is your expected results? What are your inputs and what do you want output? Give sample data.

                              Thanks,




                              Dan

                              Comment

                              Working...