Connecting Tech Pros Worldwide Help | Site Map

Passing variables to fill a form

Member
 
Join Date: Aug 2007
Posts: 67
#1: Aug 11 '07
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's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,733
#2: Aug 11 '07

re: Passing variables to fill a form


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

Like so:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.   // Get the variables
  3.   $firstName = @$_GET['firstName'];
  4.   $lastName = @$_GET['lastName'];
  5.  
  6.   // Print the form
  7.   print '<form action="process.php" method="GET">';
  8.   print '  <input type="text" name="firstName" value="'. $firstName .'" /><br />';
  9.   print '  <input type="text" name="lastName" value="'. $lastName.'" /><br />';
  10.   print '  <input type="submit" />';
  11.   print '</form>';
  12. ?>
  13.  
Member
 
Join Date: Aug 2007
Posts: 67
#3: Aug 11 '07

re: Passing variables to fill a form


page.php
Expand|Select|Wrap|Line Numbers
  1.  
  2. <?php
  3. $items = "item1,item3";
  4. ?>
  5.  
  6. <a href="form.php?selectedItems=<?=$items?>" >View Form<a/>
  7.  
  8.  

form.php
Expand|Select|Wrap|Line Numbers
  1. <form name="selectionform" method="post" action="">
  2.  
  3.               <select  size="3" multiple name="selectedItems[]">
  4.                 <option value="item1">Item1</option>
  5.                 <option value="item2">Item2</option>
  6.                 <option value="item3">Item3</option>
  7.               </select>
  8.  
  9.               <input type="submit" value="View Result">
  10. </form>
  11.  
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
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,733
#4: Aug 11 '07

re: Passing variables to fill a form


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:
Expand|Select|Wrap|Line Numbers
  1. // Print form header
  2. echo '<form name="selectionform" method="post" action="">';
  3. echo '<select size="3" multiple name="selectedItems[]">';
  4.  
  5. // Get items from GET
  6. $items = explode(",", $_GET['items']);
  7.  
  8. // Show each item
  9. for($x = 1; $x <= 3; $x++) {
  10.     // Check if it is seleted
  11.     $isSelected = false;
  12.     foreach($items as $item) {
  13.         if($item == "item". $x) {
  14.             $isSelected = true;
  15.         }
  16.     }
  17.  
  18.     // Print the item
  19.     if($isSelected) {
  20.         echo '<option selected value="item'. $x .'">Item'. $x .'</option>';
  21.     }
  22.     else {
  23.         echo '<option value="item'. $x .'">Item'. $x .'</option>';
  24.     }
  25. }
  26.  
  27. // Close the form
  28. echo '</select><input type="submit" value="View Result"></form>';
  29.  
Also, the $_POST['selectedItems'] is an array, so try this to view the results:
Expand|Select|Wrap|Line Numbers
  1. echo "<pre><b>Selected Items:</b>\n";
  2. foreach($_POST['selectedItems'] as $item) {
  3.     echo "\t$item\n";
  4. }
  5. echo "</pre>";
  6.  
Member
 
Join Date: Aug 2007
Posts: 67
#5: Aug 11 '07

re: Passing variables to fill a form


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..
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,733
#6: Aug 11 '07

re: Passing variables to fill a form


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.
Reply