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.
Clayton DeckerPresidentAsked:
Who is Participating?
Ray PaseurConnect With a Mentor Commented:
This question is a little bit like "My car won't start."  We might need to know a little more of the specifics before we can tell you what to do next :-)  An example, with test data, or maybe a link to the failing URL?

There are several "transport layers" involved in your processes.  For binary data to survive across the transport layers, base64_encode() is often helpful (for example, when sending encrypted data or putting images into a data base).  Other than that, pretty much everything you send will be a string, and will be interpreted in "duck type" meaning that if it quacks like a duck, it must be a duck.  So a numeric string like "123" can be used directly in arithmetic.

You might find this article to be helpful.  It shows the essential moving parts of the interaction between jQuery and a PHP back-end script.
Amar BardoliwalaCommented:
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
Dave BaldwinFixer of ProblemsCommented:
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.
Easily Design & Build Your Next Website

Squarespace’s all-in-one platform gives you everything you need to express yourself creatively online, whether it is with a domain, website, or online store. Get started with your free trial today, and when ready, take 10% off your first purchase with offer code 'EXPERTS'.

Julian HansenConnect With a Mentor Commented:
Here is a basic sample that illustrates what you need to do to get this to work.
// Get any parameters sent to the script here
$id = empty($_POST['id'])?0:$_POST['id'];
// Do your database query here
// Assuming data is returned in an array

// Sample data to be sent back
$data = array();
$data['name'] = 'John';
$data['email'] = '';

// Terminate script sending data back as a json string

Open in new window

<!doctype html>
<title>Test AJAX PHP MYSQL</title>
<script src="">></script>
<script type="text/javascript">
// On ready
$(function() {
  // trap event to do aJAX
  $('#data').click(function(e) {
    // disable default behaviour
    // Call the script with parameters as needed
    $.post('ajax.php',{id: 1}, function(resp) {
      // set the values of the fields based on response
    }, 'json'); // Important - don't leave this out or else
                   // JQuery won't translate the returned JSON and you will just get a string
<input type="button" id="data" value="Get Data" /><br/>
Name <input type="text" id="name" /><br/>
Email <input type="text" id="email" /><br/>

Open in new window

Clayton DeckerPresidentAuthor Commented:
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.
Ray PaseurConnect With a Mentor Commented:
The easiest way to get code to us is to post the code in the comment block, using the Code tag.  Copy your code, then in the gray bar with the formatting symbols, click Code and then paste right where it's put the cursor.
Clayton DeckerPresidentAuthor Commented:
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.
Clayton DeckerPresidentAuthor Commented:
Julian HansenCommented:
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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.