Link to home
Start Free TrialLog in
Avatar of Clayton Decker
Clayton Decker

asked on

Issues with JavaScript/AJAX/MySQL/PHP

I have a web application that is built using JavaScript/JQuery. I need to pull values into this out of aMySQL database. I use AJAX to access a PHP script to do this and I can return the proper value. However the JavaScript does not recognize it as data and displays it as a string no matter what I do.
Avatar of Amar Bardoliwala
Amar Bardoliwala
Flag of India image

Hello InfiniumIT,

Can you please post your current code here?

what have you given in dataType property of your ajax call?

it should be set to json, if you want to access it as json object.

By default it is html and you return data will be treated as string.

how are you sending data back from php to javascript?

Hope this will help you.

Thank you.

Amar Bardoliwala
Avatar of Dave Baldwin
JavaScript does not recognize it as data and displays it as a string no matter what I do.
My first response is... of course it does.  Everything returned from the database and passed back by PHP is going to be text.

My first question is... what are you expecting?  If you were expecting binary numbers, that won't happen.  If you were expecting a particular text format, did you do anything to create that format?

Javascript is weakly 'typed' meaning that you have to tell it if you want a numeric text string to be used as a number.  I typically do that by adding 0 to it or multiplying by 1.  Either operation will force javascript to view that data as a number.
SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Clayton Decker
Clayton Decker

ASKER

OK I'll see if I can get my code to you. If I set it to json it doesn't return anything so I'm thinking the problem is on the php side. I'm reading a book on it and I'm not putting it in name value pairs and I'm not sure how to do that and then parse it correctly. Also in a lot of questions I've found online, pertaining to this subject, they talk about using a callback or else the data is unusable because of the asynchronous aspect of the function.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
OK yes I understand this. Sorry I was very short in my explanation. By returning data I meant that it was returning numbers but that I am not able to use them in my JavaScript. For example I'm returning a stock price. I can put it into a variable and display that variable but if I try to multiply that variable times three it doesn't return anything even if I've set it as a number. I think I have a few ideas now though thanks to your response.
Thanks!
If your numbers are coming back as strings try using parseInt and parseFloat to convert them to numbers that can be used in mathematical operations.