473,394 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Passing variables to fill a form

67
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 ?
Aug 11 '07 #1
6 3274
Atli
5,058 Expert 4TB
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.  
Aug 11 '07 #2
coool
67
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
Aug 11 '07 #3
Atli
5,058 Expert 4TB
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.  
Aug 11 '07 #4
coool
67
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..
Aug 11 '07 #5
Atli
5,058 Expert 4TB
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.
Aug 11 '07 #6
hg
hg
gh
h

h

h
h

h
h

h
h

h
h
h

h
h
h


h
sdhg
fgj
f
Dec 29 '10 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Newbie | last post by:
OK, this may be impossible since I'm using 3rd party shopping cart ASP software, but I've been able to finagle a lot of other stuff I thought wouldn't work, so here we go: I'm using a form in...
3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
5
by: Jack | last post by:
Hi, I need to pass multple variables in a link in order to go to a asp page with the two varables. The following are the values of the variables using response.write: <%'Response.Write Mypage...
0
by: Jason Steeves | last post by:
I have one .aspx form that my users fill out and this then takes that information and populates a second .aspx form via session variables. I need to screen scrape the second .aspx form and e-mail...
11
by: Johnny | last post by:
I'm a rookie at C# and OO so please don't laugh! I have a form (fclsTaxCalculator) that contains a text box (tboxZipCode) containing a zip code. The user can enter a zip code in the text box and...
8
by: Johnny | last post by:
I'm a rookie at C# and OO so please don't laugh! I have a form (fclsTaxCalculator) that contains a text box (tboxZipCode) containing a zip code. The user can enter a zip code in the text box and...
7
by: Khai | last post by:
First off, yes, I understand the crapload of tutorials out there, (well, rather, I understand there /are/ a crapload of tutorials out there), the problem is my comprehension. I'm trying to pass...
1
by: Shawn | last post by:
As if it won't be clear enough from my code, I'm pretty new to C programming. This code is being compiled with an ANSI-C compatible compiler for a microcontroller. That part, I believe, will be...
1
by: johnjsforum | last post by:
Buddies, I have a web page to create HTML buttons dynamically as in the “formDivColorPicker” function ////////////////////////////////////////////////////////////////////////////////////...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.