Passing variables to fill a form

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • coool
    New Member
    • Aug 2007
    • 67

    Passing variables to fill a form

    Hi :)

    anyone knows how can I send variables from a php page to a form - i need to fill the form with these variables ?

    maybe using (the process of passing variables to other pages - through url)

    but how can i assign them to form variables ?
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    You could use PHP to create the form, passing the variables as you print it.

    Like so:
    [code=php]
    <?php
    // Get the variables
    $firstName = @$_GET['firstName'];
    $lastName = @$_GET['lastName'];

    // Print the form
    print '<form action="process .php" method="GET">';
    print ' <input type="text" name="firstName " value="'. $firstName .'" /><br />';
    print ' <input type="text" name="lastName" value="'. $lastName.'" /><br />';
    print ' <input type="submit" />';
    print '</form>';
    ?>
    [/code]

    Comment

    • coool
      New Member
      • Aug 2007
      • 67

      #3
      page.php
      [code=html]

      <?php
      $items = "item1,item 3";
      ?>

      <a href="form.php? selectedItems=< ?=$items?>" >View Form<a/>

      [/code]


      form.php
      [code=html]
      <form name="selection form" method="post" action="">

      <select size="3" multiple name="selectedI tems[]">
      <option value="item1">I tem1</option>
      <option value="item2">I tem2</option>
      <option value="item3">I tem3</option>
      </select>

      <input type="submit" value="View Result">
      </form>
      [/code]

      What I'm looking for is when user click "View Form" link, they get the for filled with items - i.e some items are alreay selected ---- then if the user want to deselect one of the items or select more items he should able to do that.. !

      to check the result: - in form.php
      [php]
      if($_POST)
      echo implode($_POST['selectedItems'],",");
      [/php]

      so what do you think ? having $_GET inside form.php will solve the problem ! .. but I've tried it and it doesn't work.. I didn't know how to handle the array and nothing selected !! .. can you take my sample code and add to it the proper things so it will work ! please

      Comment

      • Atli
        Recognized Expert Expert
        • Nov 2006
        • 5062

        #4
        Ok. You weren't that far off there.

        You will have to create the form in PHP, so that you can print the items you select in the GET string.

        That can be done like this:
        [code=php]
        // Print form header
        echo '<form name="selection form" method="post" action="">';
        echo '<select size="3" multiple name="selectedI tems[]">';

        // Get items from GET
        $items = explode(",", $_GET['items']);

        // Show each item
        for($x = 1; $x <= 3; $x++) {
        // Check if it is seleted
        $isSelected = false;
        foreach($items as $item) {
        if($item == "item". $x) {
        $isSelected = true;
        }
        }

        // Print the item
        if($isSelected) {
        echo '<option selected value="item'. $x .'">Item'. $x .'</option>';
        }
        else {
        echo '<option value="item'. $x .'">Item'. $x .'</option>';
        }
        }

        // Close the form
        echo '</select><input type="submit" value="View Result"></form>';
        [/code]

        Also, the $_POST['selectedItems'] is an array, so try this to view the results:
        [code=php]
        echo "<pre><b>Select ed Items:</b>\n";
        foreach($_POST['selectedItems'] as $item) {
        echo "\t$item\n" ;
        }
        echo "</pre>";
        [/code]

        Comment

        • coool
          New Member
          • Aug 2007
          • 67

          #5
          You had your code under the assuption of 1,2,3 for items..

          but when I said item1, item2, item3 ..

          I meant anything...

          it's not a sequence.. items are different words..

          Comment

          • Atli
            Recognized Expert Expert
            • Nov 2006
            • 5062

            #6
            It doesn't really matter either way. I will never be able to write exactly the code you need, as this is your code.

            You should be able to write what you need based on the code I posted above, all you need to do is edit the part where the options are printed into the form so that it prints the options you need while matching the items passed in the GET string.

            I would reccomend building an array of all the items that should be listed and use a foreach loop to loop through it an print them. Just make sure you check if each item exists in the GET string and add a 'selected' parameter in the option tag if it does.

            Comment

            • karthipan
              New Member
              • Dec 2010
              • 1

              #7
              hg
              hg
              gh
              h

              h

              h
              h

              h
              h

              h
              h

              h
              h
              h

              h
              h
              h


              h
              sdhg
              fgj
              f

              Comment

              Working...