Reputation: 33
So I have an ajax call to an external php file that is using a mysql query to look up users with a unique ID. This function works beautifully if the ID is all numbers, but if the ID starts with a letter, then nothing even posts to the php file.
I have printed to the error log, and nothing is happening at all when the button is clicked and the ID starts with a letter. I printed the data types of both a fully-numeric ID and an alphanumeric ID, and they are both strings with no hidden special characters in them.
I am completely lost and confused, can anybody offer some insight on how to get these values to post? Here is my code for the ajax call:
var value = <?php echo $userinfo['ID']; ?>;
$(function() {
$( "#emailbutton" ).click(function() {
$.post('emailaddresses.php',{value:value}, function(data){
$("#email").html(data);
});
return false;
});
});
Upvotes: 1
Views: 433
Reputation: 93571
You have simply left out the string delimiters around "<?php echo $userinfo['ID']; ?>"
... only numerics can work as literals without the quotes :)
var value = "<?php echo $userinfo['ID']; ?>";
$(function() {
$( "#emailbutton" ).click(function() {
$.post('emailaddresses.php',{value:value}, function(data){
$("#email").html(data);
});
return false;
});
});
for example, if the value returned was 123
, then you would generate the following JavaScript (which is valid):
var value = 123;
but when you returned a non-numeric ID it generated invalid JavaScript like:
var value = a123; <=== Assigns undefined unless a variable called a123 also exists with a value
when you really needed:
var value = "a123";
Upvotes: 1
Reputation: 401
Because javascript is encountering error due to var value = a12313
is not a valid JS syntax for string. Correct one should be var value = 'a12313'
. So you need to change var value = <?php echo $userinfo['ID']; ?>;
to var value = '<?php echo $userinfo['ID']; ?>';
to handle string ids
Upvotes: 0