superdan_35 Posted March 23, 2009 Share Posted March 23, 2009 Hi all. I have a basic form with a drop down list then some text boxes whose purpose is to edit data stored in a database. The user would select what they want to edit from the drop down list and the current values would dynamically appear in the text boxes ready to be changed as required. Aside from the database, the form and populating the drop down I have nothing! I am aware that I could use a separate php script through the form action but I don't want to add an extra click as the user has already made two to get to this page. I therefore am thinking down the route of a java onChange event, which in know isn't php but both languages are going to need to be incorporated, aren't they? Any suggestions? Thanks, Dan Link to comment https://forums.phpfreaks.com/topic/150756-dynamic-form/ Share on other sites More sharing options...
Yesideez Posted March 23, 2009 Share Posted March 23, 2009 I didn't see this question slip by! You'll need Javascript to update the text boxes when the drop-downs are changed. They can be incorporated quite easily, yes, as long as they're called/used in the right order and in the right places. Link to comment https://forums.phpfreaks.com/topic/150756-dynamic-form/#findComment-792033 Share on other sites More sharing options...
superdan_35 Posted March 23, 2009 Author Share Posted March 23, 2009 Thanks for the quick reply. If the drop down has say 10 items, each with the id from the database as the key, could I use the onChange to set a variable called, for example, selected. Then use selected in a query to find all the associated details and add them to the text boxes. If this is correct I have the code set up below, but I don't know what to do for the onChange? // Fill the drop down <select name="staffname" >'; $i = 0; while ($i < $numstaff) { echo '<OPTION value='.$id[$i].'> '.$name[$i].''; $i++; } echo '</select>'; // Get the chosen ID and run query $selected = ; $query = mysql_query("SELECT * FROM staff WHERE staff_id LIKE $selected"); // Display result in text text boxes Link to comment https://forums.phpfreaks.com/topic/150756-dynamic-form/#findComment-792056 Share on other sites More sharing options...
Yesideez Posted March 23, 2009 Share Posted March 23, 2009 The javascript would have to go inside the HTML, not PHP. <select name="staffname" onchange="document.form.selectname.value='something here'"> Like that although you'd have to change the selectname to the name of the second selection box and modify the Javascript slightly - it isn't really "my thing" Link to comment https://forums.phpfreaks.com/topic/150756-dynamic-form/#findComment-792083 Share on other sites More sharing options...
superdan_35 Posted March 23, 2009 Author Share Posted March 23, 2009 Thanks, I will have a look and let you know tomorrow! Dan Link to comment https://forums.phpfreaks.com/topic/150756-dynamic-form/#findComment-792096 Share on other sites More sharing options...
superdan_35 Posted March 24, 2009 Author Share Posted March 24, 2009 Ok, this is where I am! I currently have the following for my form: // Show drop down of staff members <select name="staffname" onchange="showStaff(this)"> $i = 0; while ($i < $num) { echo '<OPTION value='.$assos_id[$i].'> '.$staff[$i].''; $i++; } </select> // Run query based on selection $query = @mysql_query("SELECT * FROM staff WHERE staff_id LIKE $staff_id"); $row = mysql_fetch_object($query); // Show results First Name: <input type="text" name="fname" value="'.$row->fname.'"/> Surname: <input type="text" name="lname" value="'.$row->lname.'"/> // And do on The JavaScript function has been created to show it works but has no relevant functionality: <script language="javascript"> function showStaff(choice) { var choice = choice.options[choice.selectedIndex].value; alert (choice); } </script> So how do I manipulate the php in the function and send it back? Or in otherwords, how do I get choices from the JavaScript to be staff_id in the php? Thanks again, Dan Link to comment https://forums.phpfreaks.com/topic/150756-dynamic-form/#findComment-792765 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.