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)

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

By nmsinc - March 12, 2013

Thanks Greg, I used the first option and it worked great!

nmsinc