t3chguy
asked on
Autofill fields when select box option is selected
I have a form that when a contact is selected from a drop down menu, I want to display the phone and email data that we have stored for that user.
I am using PHP/MySQL.
Here is the code that retrieves the data from the database:
I need help with the jQuery portion of this.
I am using PHP/MySQL.
<li>
<label for="contact">Company Contact:</label>
<select name="custContact" class="compContact" required>
<option value = "">Select Contact...</option>';
//Get Contacts
$getContacts = mysql_query("SELECT id, name, forename FROM pr_contacts WHERE company = '".$companyNameid."' AND active = '1' ORDER BY forename, name") or die("Cannot pull contacts: " . mysql_error());
if(mysql_num_rows($getContacts) > 0)
{
while($contData = mysql_fetch_assoc($getContacts))
{
echo '<option value = "'.$contData['id'].'"'; if($contData['id'] == $_POST['custContact']) { echo "Selected = 'SELECTED'";} echo '>'.$contData['forename'] . ' ' . $contData['name'] . '</option>';
}
}
echo '</select> <span class="boldblacklinks"><a href="addContact.php?comp='.$companyNameid.'" class="iframetall">Create Contact</span></a>
</li>
<li>
<label for="requireUpdate">Request Update:</label>
<table width = "450px" border = "0">
<tr>
<td width = "225px"><input type="radio" name="reqUpdate" value = "1" class="reqUpdate" style="width:10px; padding-right:0px;"/>Yes, Update Customer</td>
<td width = "225px"><input type="radio" name="reqUpdate" class="reqUpdate" value = "0" style="width:10px; padding-right:0px;" />No Update Required</td>
</tr>
</table>
</li>
<div id="contactInfo" style="display:none;">
<li>
<label for="officePhone">Office Phone:</label>
<input type="text" name="officePhone" id="officePhone" />
</li>
<li>
<label for = "mobilePhone">Mobile Phone:</label>
<input type="text" name="mobilePhone" id="mobilePhone" />
</li>
<li>
<label for = "email">Email: </label>
<input type="text" name="emailAddress" id="emailAddress" />
</li>
</div>
Here is the code that retrieves the data from the database:
<?php
$programid = "5036";
require_once($_SERVER['DOCUMENT_ROOT']."/includes/verifyaccess.php");
require_once($_SERVER['DOCUMENT_ROOT']."/includes/mainfunctions.php");
require_once($_SERVER['DOCUMENT_ROOT']."/includes/expFunctions.php");
$contactId = $_GET['contactid'];
$sql = mysql_query("SELECT phone1, phone2, email FROM pr_contacts WHERE id = '".$contactId."'") or die("Cannot get contact info: " . mysql_error());
if(mysql_num_rows($sql) > 0)
{
while($row = mysql_fetch_assoc($sql))
{
//$row_array["id"] = $rs['nameid'];
//$row_array["label"] = ucwords(strtolower($rs['name'])) . ' ('.$rs['kdaccount'].')';
//$row_array["value"] = $rs['kdaccount'];
$row_array["officePhone"] = ucwords(strtolower($row['phone1']));
$row_array["mobilePhone"] = ucwords(strtolower($row['phone2']));
$row_array["email"] = ucwords(strtolower($row['email']));
array_push($json, $row_array);
}
header('Content-Type: application/json');
echo json_encode($json);
?>
I need help with the jQuery portion of this.
ASKER
Great idea. I've got the code in place, but it's not filling in the fields. Where did I go wrong?
echo '<form class="contact_form" action="#" method="post" name="contact_form" id="issueForm">
<li>
<label for="contact">Company Contact:</label>
<select name="custContact" class="compContact" required>
<option value = "">Select Contact...</option>';
//Get Contacts
$getContacts = mysql_query("SELECT id, name, forename, phone1, phone2, email FROM pr_contacts WHERE company = '".$companyNameid."' AND active = '1' ORDER BY forename, name") or die("Cannot pull contacts: " . mysql_error());
if(mysql_num_rows($getContacts) > 0)
{
while($contData = mysql_fetch_assoc($getContacts))
{
echo '<option value = "'.$contData['id'].'"'; if($contData['id'] == $_POST['custContact']) { echo "Selected = 'SELECTED'";} echo ' data-phone1= '.$contData['phone1'] . ' data-phone2= ' . $contData['phone2'] . ' data-email= ' . $contData['email'] . '>'.$contData['forename'] . ' ' . $contData['name'] . '</option>';
}
}
echo '</select> <span class="boldblacklinks"><a href="addContact.php?comp='.$companyNameid.'" class="iframetall">Create Contact</span></a>
</li>
<li>
<label for="requireUpdate">Request Update:</label>
<table width = "450px" border = "0">
<tr>
<td width = "225px"><input type="radio" name="reqUpdate" value = "1" class="reqUpdate" style="width:10px; padding-right:0px;"/>Yes, Update Customer</td>
<td width = "225px"><input type="radio" name="reqUpdate" class="reqUpdate" value = "0" style="width:10px; padding-right:0px;" />No Update Required</td>
</tr>
</table>
</li>
<div id="contactInfo" style="display:none;">
<li>
<label for="officePhone">Office Phone:</label>
<input type="text" name="officePhone" id="officePhone" />
</li>
<li>
<label for = "mobilePhone">Mobile Phone:</label>
<input type="text" name="mobilePhone" id="mobilePhone" />
</li>
<li>
<label for = "email">Email: </label>
<input type="text" name="emailAddress" id="emailAddress" />
</li>
</div>';
?>
<script type='text/javascript'>
$(document).ready(function()
{
$('.reqUpdate').click(function(){
if($(this).val() == 0)
{
$('#contactInfo').hide();
}
else
{
$('#contactInfo').show();
}
});
$('select[name="custContact"]').change(function() {
$('#officePhone').val( $(this).data('phone1') );
$('#mobilePhone').val( $(this).data('phone2') );
$('#emailAddress').val( $(this).data('email') );
});
});
</script>
The code that creates your <option> looks a little off (it's quite difficult to tell when concatenating strings like this). Maybe some missing quotes around the data attributes - the easiest way to tell is to view the source once you've loaded your page.
An easier option, which I think makes things much easier to read and debug, is to use the printf() function, instead of trying to concatenate with echo statements. Have a look at this and see if it makes sense:
An easier option, which I think makes things much easier to read and debug, is to use the printf() function, instead of trying to concatenate with echo statements. Have a look at this and see if it makes sense:
if(mysql_num_rows($getContacts) > 0) :
while($contData = mysql_fetch_assoc($getContacts)):
$option = '<option value="%s" data-phone1="%s" data-phone2="%s" data-email="%s" %s>%s</option>';
$selected = ($contData['id'] == $_POST['custContact']) ? "Selected = 'SELECTED'" : '';
printf(
$option,
$contData['id'],
$contData['phone1'],
$contData['phone2'],
$contData['email'],
$selected,
$contData['forename'] . ' ' . $contData['name']
);
endwhile;
endif;
ASKER
The code above did not work either. here is what the page produces:
<li>
<label for="contact">Company Contact:</label>
<select name="custContact" class="custContact" required>
<option value = "">Select Contact...</option>
<option value = "60598" data-phone1= "847-451-5000" data-phone2= "312-623-2376" data-email= "alex.shleymovich@hillmech.com">Alex Shleymovich</option>
<option value = "92563" data-phone1= "847-451-4218 " data-phone2= "773 220 3882" data-email= "anthony.borgnini@hillmech.com">Anthony Borgnini</option>
</select>
</li>
$('.custContact').change(function() {
$('.officePhone').val( $(this).data('phone1') );
$('.mobilePhone').val( $(this).data('phone2') );
$('.emailAddress').val( $(this).data('email') );
});
ASKER
Please note the above code are just the two extracts that we are working with. I figured it would be cleaner to post that instead of the full code.
ASKER
<div id="contactInfo" style="display:none;">
<li>
<label for="officePhone">Office Phone:</label>
<input type="text" class="officePhone" name="officePhone" id="officePhone" />
</li>
<li>
<label for = "mobilePhone">Mobile Phone:</label>
<input type="text" class="mobilePhone" name="mobilePhone" id="mobilePhone" />
</li>
<li>
<label for = "email">Email: </label>
<input type="text" class="emailAddress" name="emailAddress" id="emailAddress" />
</li>
</div>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
That'll do it it! Thank you so much!!
Build your <OPTION> tags like this:
<option value="id" data-phone1="phone1" data-phone2="phone2" data-email="email">Persons Name</option>
* Replace the italics with your database values!
Then in jQuery, bind the change() event to the SELECT and update the form fields by grabbing the relevant info using the data() method:
Open in new window
All the data-* attributes you store with each <option> are then available in your jQuery - no round trip to the server..Hope that makes sense but ask if not :)