Grab and Set users 'email' in addition to 'fullname' from Options select

3 posts by 2 authors in: Forums > CMS Builder
Last Post: March 12, 2013   (RSS)

By nmsinc - March 9, 2013

Ref: From the Options code below I can choose a user from the drop down - I also need to set a value for the suers email when I choose a user. How can that be accomplished?

-------------------------------------------------------------------------------------------

<select id="user_drop" name="user_name">

<option value = "<?php echo $table4Record['num'] ?>"><?php echo $table4Record['fullname'] ?></option>

<?php foreach ($accountsRecords as $user): ?>

<?php if ($user['member_company_accounts'] == $table3Record['num'] AND $user['user_type'] == "Adjuster" AND !$user['disabled'] OR $user['member_company_accounts'] == $table3Record['num'] AND $user['user_type'] == "Dispatcher/Adjuster" AND !$user['disabled']): ?>

<option value = "<?php echo $user['num'];?>"><?php echo $user['fullname'];?></option>

<?php endforeach ?>

</select>

----------------------------------------------------------------------------------------

Thanks - nmsinc

nmsinc

By gregThomas - March 11, 2013

Hi nmsinc,

There are a couple of ways you could achieve this. Do you need to get the e-mail before the form is submitted? If not you could retrieve it after the form is submitted:

$user = mysql_get('accounts', intval($_REQUEST['user_name']));
echo $user['email']; 

The second option would be to use Jquery to add the e-mail address to a field when the user has selected something from the drop-down:

<select id="user_drop" name="user_name">


  <?php foreach ($accountsRecords as $user): ?>

    <option data-email="<?php echo $user['email']; ?>" value="<?php echo $user['num'];?>"><?php echo $user['fullname'];?></option>

  <?php endforeach ?>

</select>

<input id="email" type="text" name="email" value="" />
<!-- if the user selects something from the drop down, add the e-mail address to the email field -->
<script>
$(document).ready(function() {
  $('#user_drop').change(function() {
    var selected = $(this).find('option:selected');
    var email = selected.data('email'); 
    alert(email);
    $('#email').val(email);
  });
});
</script>

This is just test code, so you'll need to make some changes to get it working on your site. 

Let me know if you have any questions.

Thanks!

Greg

Greg Thomas







PHP Programmer - interactivetools.com