toLowerCase is not working

The php data is
foreach ($result as $row)
{
$name = array($row[‘name’]);
}

echo json_encode($name);

The value of tmp is from <input …>

js:
$(“#name”).keyup(function(){
$.ajax({
type: ‘GET’,
datatype: ‘json’,
url: “namevali.php”,
success: function(data) {
var tmp=document.getElementById(“name”).value;
var dataArr = eval(“(” + data + “)”);

var count=0;
for (var i=0; i<dataArr.length; i++)
{
if (dataArr[i] == tmp)
{
count=1;
}
}

but when I use dataArr[i].toLowerCase() == tmp.toLowerCase(), something goes wrong.
It look like toLowerCase can’t be used with dataArr[i] and tmp.

Please do not use eval - that tends to be terrible.

Instead, JSON.parse is the preferred manner in which to receive JSON data. For example:

var dataArr = JSON.parse(data);

Also, your for loop looks like it’s incomplete.

See how things go with those things fixed up.

1 Like

I think the loop is complete because I did not copy the rest… I did try using JASON.parse(data). It’s still the same.

Try adding console.log(data) and console.log(tmp) just after each is first defined and let us know what the values that are logged look like. If the value for data looks like a JSON string then add a console.log(dataArr) to see what that looks like once it is converted back to JSON.

That will tell us whether you have the correct values prior to starting to process it and narrow down the possible causes for the problem.

I don’t see how this can be considered a valid loop:

for (var i=0; i{

It seems that part of it is incomplete, needing to instead be:

for (var i = 0; i < dataArr.length; i += 1) {

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.