SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Thread: While loop help

  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2005
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    While loop help

    I have a page that has 2 forms and I would like to use this while loop within each form, but for some reason this code only runs once. Any ideas on how to make it run in each form?

    Code:
    <form action="page1.php" method="post" name="form1" >
    <?php 
    	while (list($key,$value) = each($_REQUEST)){
    		echo "<input type='hidden' name='".$key."' value='".$value."'/>\n";
    	}
    ?>
    </form>
    <form action="page2.php" method="post" name="form2" >
    <?php 
    	while (list($key,$value) = each($_REQUEST)){
    		echo "<input type='hidden' name='".$key."' value='".$value."'/>\n";
    	}
    ?>
    </form>

  2. #2
    derrrp
    Join Date
    Aug 2006
    Location
    earth
    Posts
    923
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if you're not married to the idea of using a while loop, this works for what you're trying to do.

    PHP Code:
    <form action="page1.php" method="post" name="form1" >
    <?php 
        
    foreach ($_REQUEST as $key=>$value){
            echo 
    "<input type='hidden' name='".$key."' value='".$value."'/>\n";
        }
    ?>
    </form>
    <form action="page2.php" method="post" name="form2" >
    <?php 
        
    foreach ($_REQUEST as $key=>$value){
            echo 
    "<input type='hidden' name='".$key."' value='".$value."'/>\n";
        }
    ?>
    </form>
    Why are you using $_REQUEST?
    No, I REALLY dislike having to use Joomla.

  3. #3
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,804
    Mentioned
    157 Post(s)
    Tagged
    3 Thread(s)
    Start an empty variable and then fill it using a loop - then use that variable in the form:

    PHP Code:
    $display '';

    foreach (
    $_REQUEST as $key=>$value){
        
    $display .=  "<input type='hidden' name='".$key."' value='".$value."'/>\n";

    then use the display variable in the forms:

    PHP Code:
    <form action="page1.php" method="post" name="form1" >
    <?php 
        
    echo $display;
    ?>
    </form>
    <form action="page2.php" method="post" name="form2" >
    <?php 
        
    echo $display;
    ?>
    </form>
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  4. #4
    derrrp
    Join Date
    Aug 2006
    Location
    earth
    Posts
    923
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Aaaand, his answer is better.
    No, I REALLY dislike having to use Joomla.

  5. #5
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,804
    Mentioned
    157 Post(s)
    Tagged
    3 Thread(s)
    lol, not better - just efficient
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  6. #6
    SitePoint Enthusiast
    Join Date
    Oct 2005
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Perfect thanks


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •