form passing value problem in loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • littlegreen
    New Member
    • Mar 2008
    • 17

    form passing value problem in loop

    Hi all,
    I would like to know how to assign and get the form value when I using it in a loop. I cannot get all the input that I inserted in 'Desc' part and it is from this site. May I know to get this work when I do a loop? Thanks.

    a.php
    [HTML]// this can work
    <font size="2">Title: </font><br><input type="text" name="Title[]" size="40" value="<?php echo htmlentities($m atch[$counter], ENT_QUOTES); ?>">
    </tr>
    <tr>&nbsp;</tr>
    // but this can only get the last input when do multiple post
    <tr>
    <font size="2">Descri ption:</font>
    <script language="JavaS cript" type="text/javascript">
    <!--
    function submitForm() {
    updateRTE('Desc ');
    return true;
    }

    initRTE("images/", "", "");
    writeRichText(' Desc', '', 350, 200, true, false);

    //-->
    </script>
    </tr>[/HTML]
    b.php
    [PHP]$title= $_POST['Title'];
    $desc= $_POST['Desc'];
    mysql_query("in sert into post(title, description)val ues ('$title[$counter]', '$desc[$counter]')")or die (mysql_error()) ;[/PHP]
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #2
    This piece of code [php]<input type="text" name="Title[]" [/php] shows that you have the value of that input field in an array with the name 'Title'. Since I see that it is the only value assigned to that array, it will be stored in the first entry. So to get that value out, assuming you use a posted form, you specify[php]$title=$_POST['title'][0];[/php]Ronald

    Comment

    • littlegreen
      New Member
      • Mar 2008
      • 17

      #3
      Originally posted by ronverdonk
      This piece of code [php]<input type="text" name="Title[]" [/php] shows that you have the value of that input field in an array with the name 'Title'. Since I see that it is the only value assigned to that array, it will be stored in the first entry. So to get that value out, assuming you use a posted form, you specify[php]$title=$_POST['title'][0];[/php]Ronald
      thanks, oops..sorry about that, but I had used the counter in that place also, but I can only get the value of title, and i can't get the value of decription while in the loop. The problem is lied on the description, it works fine when single, but when there's a loop, then it will have problem in getting the value of the 'Desc' part.

      Comment

      • Markus
        Recognized Expert Expert
        • Jun 2007
        • 6092

        #4
        Originally posted by littlegreen
        thanks, oops..sorry about that, but I had used the counter in that place also, but I can only get the value of title, and i can't get the value of decription while in the loop. The problem is lied on the description, it works fine when single, but when there's a loop, then it will have problem in getting the value of the 'Desc' part.
        I cant even see you using a loop.

        Comment

        • TheServant
          Recognized Expert Top Contributor
          • Feb 2008
          • 1168

          #5
          Can you post all your relevant code, because there is no loop there, so not sure what you're talking about.

          Comment

          • littlegreen
            New Member
            • Mar 2008
            • 17

            #6
            a.php
            [PHP]
            <?php
            $counter=0;
            while($counter< $test)
            {
            ?>
            <form name="myform" action="b.php" method="post">
            <table>
            // this can work
            <font size="2">Title: </font><br><input type="text" name="Title[]" size="40" value="<?php echo htmlentities($m atch[$counter], ENT_QUOTES); ?>">
            </tr>
            <tr>&nbsp;</tr>
            // but this can only get the last input when do multiple post
            <tr>
            <font size="2">Descri ption:</font>
            <script language="JavaS cript" type="text/javascript">
            <!--
            function submitForm() {
            updateRTE('Desc ');
            return true;
            }

            initRTE("images/", "", "");
            writeRichText(' Desc', '', 350, 200, true, false);

            //-->
            </script>
            </tr>
            </table>
            <?php
            $counter++;}
            ?>
            </form>
            <input type="submit" value="Save"/>
            [/PHP]
            b.php
            [PHP]
            <?php
            $counter=0;
            while($counter< $test)
            {

            $title= $_POST['Title'];
            $desc= $_POST['Desc'];
            mysql_query("in sert into post(title, description)val ues ('$title[$counter]', '$desc[$counter]')")or die (mysql_error()) ;

            $counter++;
            }
            [/PHP]

            Comment

            • ronverdonk
              Recognized Expert Specialist
              • Jul 2006
              • 4259

              #7
              Information is lacking, but I'll give it a try:
              Assuming that :
              1. Title and Desc are both user (form) filled arrays with an equeal count of entries
              2. You want to store each of those fields in a db table
              3. You look at the following sample for B.PHP[php]<?php
              for ($counter=0; $counter<count( $_POST['Title']); $counter++)
              {
              $title= $_POST['Title'][$counter];
              $desc = $_POST['Desc'][$counter];
              mysql_query("in sert into post(title, description)val ues ('$title', '$desc'")
              or die (mysql_error()) ;

              }
              ?>[/php]Ronald

              Comment

              • littlegreen
                New Member
                • Mar 2008
                • 17

                #8
                Thanks, ronverdonk, my while loop is seems like your for loop, but I still can't get the second 'Desc' part for example if it loops twice. I not sure how to get the value from the 'Desc' after it is posted.

                Originally posted by ronverdonk
                Information is lacking, but I'll give it a try:
                Assuming that :
                1. Title and Desc are both user (form) filled arrays with an equeal count of entries
                2. You want to store each of those fields in a db table
                3. You look at the following sample for B.PHP[php]<?php
                for ($counter=0; $counter<count( $_POST['Title']); $counter++)
                {
                $title= $_POST['Title'][$counter];
                $desc = $_POST['Desc'][$counter];
                mysql_query("in sert into post(title, description)val ues ('$title', '$desc'")
                or die (mysql_error()) ;

                }
                ?>[/php]Ronald

                Comment

                • ronverdonk
                  Recognized Expert Specialist
                  • Jul 2006
                  • 4259

                  #9
                  Read the assumptions in my post and it will be clearer. E.g. I assumed that youn have as many Desc's as Title's and both are in an array with the same number of enries.
                  If that is not true, you must retrieve the Desc value outside the loop.

                  Problem is that I cannot read (do not understand) how the form you showed is constructe (RTE??). Title is clear, but I cannot work out how and where you insert Desc into the form.

                  Ronald

                  Comment

                  • littlegreen
                    New Member
                    • Mar 2008
                    • 17

                    #10
                    Originally posted by ronverdonk
                    Read the assumptions in my post and it will be clearer. E.g. I assumed that youn have as many Desc's as Title's and both are in an array with the same number of enries.
                    If that is not true, you must retrieve the Desc value outside the loop.

                    Problem is that I cannot read (do not understand) how the form you showed is constructe (RTE??). Title is clear, but I cannot work out how and where you insert Desc into the form.

                    Ronald
                    oo..sorry about that, I think here should make you clearer. http://www.dynamicdrive.com/dynamici...itor/index.htm
                    For the 'Desc' part, I take from here. Thanks.

                    Comment

                    • ronverdonk
                      Recognized Expert Specialist
                      • Jul 2006
                      • 4259

                      #11
                      Now I see what you were doing: using a richt text editor to get and pass the values.

                      Now the quickest way to tackle this and to see what is actually passed to your B.PHP script, is to put the following statement at the top of your B.PHP.[php]echo '<pre>'; print_r($_POST) ; echo '</pre>';[/php]That will show you exactly what is passed to B.PHP and in what type of format (single vars or array).

                      When you know that you can determine how to handle the $_POSTed values.

                      Ronald

                      Comment

                      • littlegreen
                        New Member
                        • Mar 2008
                        • 17

                        #12
                        Originally posted by ronverdonk
                        Now I see what you were doing: using a richt text editor to get and pass the values.

                        Now the quickest way to tackle this and to see what is actually passed to your B.PHP script, is to put the following statement at the top of your B.PHP.[php]echo '<pre>'; print_r($_POST) ; echo '</pre>';[/php]That will show you exactly what is passed to B.PHP and in what type of format (single vars or array).

                        When you know that you can determine how to handle the $_POSTed values.

                        Ronald
                        Thanks, I can see the output already. The 'Desc' part only get the last value that I entered. I wonder why it cannot get the both values if there are two (loop)...

                        Comment

                        • ronverdonk
                          Recognized Expert Specialist
                          • Jul 2006
                          • 4259

                          #13
                          You can only do that when keeping an index counter and concatenate that with the 'desc' name in your RTE statement. Or make 2 RTE's with different names.
                          So then you get 'desc1' and 'desc2' fields.

                          Ronald

                          Comment

                          • aktar
                            New Member
                            • Jul 2006
                            • 105

                            #14
                            Yep, I would second that

                            Comment

                            • ronverdonk
                              Recognized Expert Specialist
                              • Jul 2006
                              • 4259

                              #15
                              Originally posted by aktar
                              Yep, I would second that
                              Why would you do that?

                              Ronald

                              Comment

                              Working...