Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 501
  • Last Modified:

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.
0
Clayton Decker
Asked:
Clayton Decker
  • 3
  • 2
  • 2
  • +2
3 Solutions
 
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
0
 
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.
0
 
Julian HansenCommented:
Here is a basic sample that illustrates what you need to do to get this to work.
PHP
<?php
// 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'] = 'john@domain.com';

// Terminate script sending data back as a json string
die(json_encode($data));
?>

Open in new window

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

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Ray PaseurCommented:
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.
http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Jquery/A_10712-The-Hello-World-Exercise-with-jQuery-and-PHP.html
0
 
Clayton DeckerAuthor 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.
0
 
Ray PaseurCommented:
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.
0
 
Clayton DeckerAuthor 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.
0
 
Clayton DeckerAuthor Commented:
Thanks!
0
 
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.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now