jponte Posted February 22, 2010 Share Posted February 22, 2010 Hi Freaks, I have a form where I would like to give the possibility to a user to use a check box or click a button to populate some address fields with info from MySQL DB before submitting the form. Does anyone know of any good script or example I could follow to get me started? Thanks, JP Quote Link to comment https://forums.phpfreaks.com/topic/193001-populate-address-fields-when-button-clicked-or-checkbox-checked/ Share on other sites More sharing options...
jl5501 Posted February 22, 2010 Share Posted February 22, 2010 If this is a server lookup based on some entered data, like an email adress or other identifier to get the other info, then you need your button or checkbox to trigger an ajax call to the server, the response from which, can populate the fields Quote Link to comment https://forums.phpfreaks.com/topic/193001-populate-address-fields-when-button-clicked-or-checkbox-checked/#findComment-1016441 Share on other sites More sharing options...
Psycho Posted February 22, 2010 Share Posted February 22, 2010 I think AJAX is overkill for this. Since this is a checkbox, I assume the address values are predetermined. Simply do the query of the database when building the page and populate the address data into the javascript. Then create a function to run onclick of the checkbox to populate the values. Working example: <html> <head> <script type="text/javascript"> //use PHP code to write these JavaScript variable var address1 = "123 Main Street"; var address2 = "Suite 200"; var city = "Los Angeles"; var state = "CA"; var zip = "97140"; function useSavedAddress(useSaved) { document.getElementById('address1').value = (useSaved) ? address1 : ''; document.getElementById('address2').value = (useSaved) ? address2 : ''; document.getElementById('city').value = (useSaved) ? city : ''; document.getElementById('state').value = (useSaved) ? state : ''; document.getElementById('zip').value = (useSaved) ? zip : ''; return; } </script> </head> <body> <input type="checkbox" name="useSaved" onclick="useSavedAddress(this.checked);" /> Use saved address <br /><br /> Address 1: <input type="text" id="address1" name="address1" /> <br /> Address 2: <input type="text" id="address2" name="address2" /> <br /> City, ST ZIP: <input type="text" id="city" name="city" /> <input type="text" id="state" name="state" size="2" /> <input type="text" id="zip" name="zip" size="5" /> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/193001-populate-address-fields-when-button-clicked-or-checkbox-checked/#findComment-1016448 Share on other sites More sharing options...
jponte Posted February 23, 2010 Author Share Posted February 23, 2010 Hi, Thanks mjdamato. That is exactly what I needed. I had the PHP values already at the page level. Peace, JP Quote Link to comment https://forums.phpfreaks.com/topic/193001-populate-address-fields-when-button-clicked-or-checkbox-checked/#findComment-1017023 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.