I'm attempting to create a table using PHP from a jQuery post method and I am failing miserably.
PHP/HTML
<tr>
<td>Name: </td>
<td>
<select id="name" onchange="showUserInfo()">
<option value="">Please select a person...</option>
<?php $names = getUserName($db); foreach($names as $key => $value) { ?>
<option value="<?php echo $key ?>"><?php echo $value ?></option>
<?php }?>
</select>
</td>
</tr>
...
<div id="editUserTable">hi</div>
jQuery
function showUserInfo() {
var str = $('#name option:selected').text(); // THIS VARIABLE IS NOT BEING SET
$.post('/TrakFlex/functions/getUserInfo.php', { input: str }, function(data) {
$('#editUserTable').html(data);
})
}
Now I know this variable is not being set because if I replace
var str = $('#name option:selected').text();
With an actual username like this
var str = 'De Bug';
Everything works perfectly the table loads into the form with all the user information.
As it is now, the div 'editUserTable' shows "hi" when the form loads but when I select a user "hi" disappears and nothing is showing.
It should be noted I have tried this and it does not work
var str = $('#name').val();
So again, the question is, why is this variable not being set to the inner HTML or 'text' of the selected <option>. Any help is appreciated
EDIT If I view the page source after I select a user this is what I see
<select id="name" onchange="showUserInfo()">
<option value="">Please select a person...</option>
<option value="0">De Bug</option>
<option value="1">Jon Doe</option>
<option value="2">A Name</option>
</select>
EDIT 2
If I hard code the name of the user in my jQuery everything loads in my html page.
this is the original
function showUserInfo() {
var str = $('#name option:selected').text(); // THIS VARIABLE IS NOT BEING SET
$.post('/TrakFlex/functions/getUserInfo.php', { input: str }, function(data) {
$('#editUserTable').html(data);
})
}
if I change it to this, it works
function showUserInfo() {
var str = 'De Bug'; // I can also use 'A Name' or 'Jon Doe' they all work
$.post('/TrakFlex/functions/getUserInfo.php', { input: str }, function(data) {
$('#editUserTable').html(data);
})
}
;var str = $('#name option:selected').text();With an actual username like thisvar str = 'De Bug';Everything works perfectly;or add a;somewhere I wasn't supposed to?