Schlo_50 Posted February 15, 2008 Share Posted February 15, 2008 Hey guys, I have been working on a lengthy form and have come to a point where i am now stuck for ideas. I have two drop boxes within the form. The first contains usernames stored in my database, and the second is supposed to contain information which is unique to a username. So there is a one to many relationship. I want the second generated drop box to automatically display the unique values when a username is selected, but at the moment it displays all values associated with every username stored in my database. (so the list is rather long!) Can this be done? I have attempted below: <?php print "<select name=\"client\">"; $filea = file("../users.txt"); foreach($filea as $Keya => $Vala){ $Dataa[$Keya] = explode("|", $Vala); $client = $Dataa[$Keya][0]; print "<option value=\"$client\">$client</option>"; } print "</select>"; ?> </td> </tr> <tr> <td class="text"><img src="images/point.png" width="11" height="8" border="0" />Category</td> <td class="text"> <?php print "<select name=\"category\">"; $file = file("../usercats.txt"); foreach($file as $Key => $Val){ $Data[$Key] = explode("|", $Val); $username = $Data[$Key][0]; $catname = $Data[$Key][1]; $catid = $Data[$Key][2]; if ($client = $username){ print "<option value=\"$catid\">$catname</option>"; } } print "</select>"; ?> Thanks for any responses. If you need any questions answered please do ask! Thanks again. Quote Link to comment Share on other sites More sharing options...
micah1701 Posted February 15, 2008 Share Posted February 15, 2008 you're going to need to use javascript and have an onChange() event trigger when a user name is selected. the javascript should either: 1) [easist] reload the page with the username as a $_GET value in the url so you can use php to pull up the unique info for that user when the page first loads and populate it into the field. or 2) [dumbest] populate the second field with the corresponding user's information that you've pre-loaded into a javascript array when the page first loaded. (although this means all users information will be visible in the client-side source code) or 3) [best method but harder] use an "ajax" function to submit the username to a server-side php script that returns the unique information in the background and then populates the the field with the result. Quote Link to comment Share on other sites More sharing options...
Schlo_50 Posted February 15, 2008 Author Share Posted February 15, 2008 Will option 1 remove any previous information typed into the text boxes etc when the reload happens? Quote Link to comment Share on other sites More sharing options...
micah1701 Posted February 15, 2008 Share Posted February 15, 2008 unfortunately, it will not. you may need to find a creative work around (like ask for the username first before the user enters other field information) or have the javascript submit the partially submitted form and when the page reloads, php can use the $_POST values to populate the form fields Quote Link to comment Share on other sites More sharing options...
Schlo_50 Posted February 15, 2008 Author Share Posted February 15, 2008 Hmmm.. Thanks for the suggestions, i'll just make the user select an option which then moves onto the next part of the form. Thanks again for the help! (P.S has the 'topic solved' feature been removed from the forums? I can't seem to see it anymore??) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.