Jump to content

Populate address fields when button clicked or checkbox checked


jponte

Recommended Posts

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

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.