/    Sign up×
Community /Pin to ProfileBookmark

JQuery and AJAX – Getting information from php file

So I am well aware that I have another post talking about this file, but as the problem on there seemed solved, I wanted to post a new thread. Anyway, I am having a new issue (which is technically the last thing I need done in order to officially be finished).

(yes this is in the other thread, sorry if this bothers someone, but needed an answer to this so I can be done.)

So now I am having trouble getting the rankings. I did get the names to populate the list and got the meaning to display and work (for the most part I gotta fix my error checking to make sure if the meaning is empty it tells them no meaning found). Not sure how to get the information in the format it is in. Below will be an example. Basically need to get the year and the rank numbers in order to display.

Example of the data

<baby name=”Morgan”>
<rank year=”1900″>430</rank>
<rank year=”1910″>477</rank>

<rank year=”2000″>25</rank>
</baby>

My code

[CODE]
$(document).ready(function() {
//$(‘<p> Select a name from the list.</p>’).appendTo($(‘#allnames’));
$(‘#search’).hide();
$.ajax(
‘http://…./babynames.php’,
{
‘type’: “GET”,
‘data’: {“type”:”list”},
‘success’: showNames,
‘error’: ajax_failed
});

// hide loading image
$(‘#loadingmeaning’).hide();
$(‘#loadinggraph’).hide();

});

function showNames(data) {

$(‘#allnames’).removeAttr(‘disabled’);
$(‘#loadingnames’).hide();
var names = data.split(“n”);
var listLEN = names.length;
listLEN –;
//add the names into the list
for(var i = 0; i < listLEN; i++){
//console.log(names[i]);
$(‘<option>’).text(names[i]).click(getRankings).attr(‘value’, names[i]).appendTo($(‘#allnames’));
}
//$(‘#search’).click(showMeaning);
}

function showMeaning(){
$(‘#loadingmeaning’).show();
$(‘#resultsarea’).show();
$.ajax(
‘http://…./babynames.php’,
{
‘type’ : “GET”,
‘data’: {
‘type’: ‘meaning’,
‘name’: $(this).text(),
delay: 2

},
‘success’ : disMeaning,
‘error’ : ajax_failed,

});
$(‘#meaning’).empty();
}

function disMeaning(data){
var meaning = data;
//console.log(meaning);
$(‘#loadingmeaning’).hide();
if (!data){
$(‘<p> No meaning found! </p>’).appendTo($(‘#meaning’));
}
else{
$(‘<p>’ + meaning + ‘</p>’).appendTo($(‘#meaning’));
}
}

function getRankings(){
$(‘#resultsarea’).show();
$.ajax(
‘http://…./babynames.php’,
{
‘type’ : “GET”,
‘data’: {
‘type’: ‘rank’,
‘name’: $(this).text(),
delay: 2
},
‘success’ : disRanking,
‘error’ : ajax_failed
});
}

function disRanking(data){
$(‘#loadinggraph’).hide();
var rank = data;

console.log(rank);

}

// ajax_failed is a handler function for the ajax “error” event
function ajax_failed(xhr, status, error){
if (xhr.status == 404){
// error 404 has occurred
}
}
[/CODE]

to post a comment
JavaScript

8 Comments(s)

Copy linkTweet thisAlerts:
@flyntcoalauthorDec 11.2015 — Here is what I got going for me so far. I have got to be able to get the data from the PHP file (which is formatted in XML it looks like, example is in the first post above) when the user selects from the list of names (which that part is working) it will then display the meaning and the rankings (1-1000) for popularity of that name during different years. Problem is I am not sure how to get that information and then display it back. I've been working with it for a while and still can't get it to work. Really really really could use help with this. I gotta show this today to my instructor, (looking right now like partial points, which I am still doing well in that class) but I would like to have a finished site. (Have asked him, but he makes it more confusing and even asks questions about if I am even trying and stuff like that, so not a great instructor.) I will post the HTML and the JQuery code that I have and will be working with it, but I need help from someone ASAP. Thank you soooo much if you do help me, because this is frustrating me and I know I probably have annoyed some of you, but I want to be able to present this to him and be done with it.

HTML

[code=html]<!DOCTYPE html>
<html>
<!--
Homework 7 (Baby Names)
You should download and use this names.html, but do not modify it.
Your JS code should work with an unmodified version of this file.
-->

<head>
<title>Baby Names</title>

<!-- instructor-provided JavaScript files; do not modify -->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<!-- your files -->
<link href="names.css" type="text/css" rel="stylesheet" />
<script type="text/javascript" src="names.js"></script>
</head>

<body>
<h1>
Baby Names
<span id="w3c">
<a href="http://validator.w3.org/check/referer"><img src="w3c-html.png" alt="Valid HTML" /></a>
<a href="http://jigsaw.w3.org/css-validator/check/referer?profile=css3"><img src="w3c-css.png" alt="Valid CSS" /></a>
</span>
</h1>

<div id="namearea">
<h2>First Name:</h2>

<div>
<!-- list of all baby names should be inserted into this select box -->
<select id="allnames" name="allnames" disabled="disabled">
<option value="">(choose a name)</option>
</select>

<button id="search">
<img src="pacifier.gif" alt="icon" />
Search
</button>

<span class="loading" id="loadingnames">
<img src="loading.gif" alt="icon" />
Loading...
</span>
</div>
</div>

<!-- un-hide this 'resultsarea' div when you fetch data about the name -->
<div id="resultsarea" style="display: none;">
<div id="originmeaning">
<h2>Origin/Meaning:</h2>

<div class="loading" id="loadingmeaning">
<img src="loading.gif" alt="icon" />
Searching...
</div>

<!-- baby name meaning data should be inserted into this div -->
<div id="meaning"></div>
</div>

<div id="grapharea">
<h2>Popularity:</h2>

<div class="loading" id="loadinggraph">
<img src="loading.gif" alt="icon" />
Searching...
</div>

<!-- if there is no ranking data for the given name, show this error message -->
<div id="norankdata" style="display: none;">
There is no ranking data for that name/gender combination.
</div>

<!-- baby name ranking data should be inserted into this table -->
<table id="graph"></table>
</div>
</div>

<!-- an empty div for inserting any error text -->
<div id="errors"></div>
</body>
</html>
[/code]


[CODE]
$(document).ready(function() {
//$('<p> Select a name from the list.</p>').appendTo($('#allnames'));
$('#search').hide();
$.ajax(
'http://...../babynames.php',
{
'type': "GET",
'data': {"type":"list"},
'success': showNames,
'error': ajax_failed
});

// hide loading image
$('#loadingmeaning').hide();
$('#loadinggraph').hide();

});


function showNames(data) {

$('#allnames').removeAttr('disabled');
$('#loadingnames').hide();
var names = data.split("n");
var listLEN = names.length;
listLEN --;
//add the names into the list
for(var i = 0; i < listLEN; i++){
$('<option>').text(names[i]).click(getRankings).attr('value', names[i]).appendTo($('#allnames'));
}
}


function showMeaning(){
$('#loadingmeaning').show();
$('#resultsarea').show();
$.ajax(
'http://...../babynames.php',
{
'type' : "GET",
'data': {
'type': 'meaning',
'name': $(this).text(),
delay: 2

},
'success' : disMeaning,
'error' : ajax_failed,

});
$('#meaning').empty();
}

function disMeaning(data){
var meaning = data;
//console.log(meaning);
$('#loadingmeaning').hide();
if (!data){
$('<p> No meaning found. </p>').appendTo($('#meaning'));
}
else{
$('<p>' + meaning + '</p>').appendTo($('#meaning'));
}
}

function getRankings(){
$('#resultsarea').show();
$('#loadinggraph').show();
$.ajax(
'http://...../babynames.php',
{
'type' : "GET",
'data': {
'type':'rank',
'name': $(this).text(),

},
'success' : disRanking,
'error' : ajax_failed
});
}

function disRanking(ajax){
$('#loadinggraph').hide();
var rank = $(ajax);
rank.find('name').each(function(idx, e){
var year = $(e).attr('year');
var ranks = $(e).attr('rank');

$('<th>' + year(e)+'</th>').appendTo($('#graph'));
$('<td><div class = "bar">' + ranks(e) + '</div></td>').appendTo($('#graph'));

if (ranks > 250){
var newHeight = (((1000 - ranks) - 250)/4);
}
else{
var newHeight = ((250 - (1000 - ranks))/4);
}
$('.bar').css('height', newHeight + 'px');
if (rank <= 10){
$('.bar').css('color', 'red');
}
});
}

// ajax_failed is a handler function for the ajax "error" event
function ajax_failed(xhr, status, error){
if (xhr.status == 404){
// error 404 has occurred
}
}
[/CODE]
Copy linkTweet thisAlerts:
@flyntcoalauthorDec 11.2015 — Update to the code I have now: I have been working with error checking and so far working (except if there is no meaning). Still need help getting the rankings, not working still.

[CODE]
$(document).ready(function() {
$('<p> Select a name from the list.</p>').appendTo($('#namearea'));
$('#search').hide();
$.ajax(
'http://.../babynames.php',
{
'type': "GET",
'data': {"type":"list"},
'success': showNames,
'error': ajax_failed
});

// hide loading image
$('#loadingmeaning').hide();
$('#loadinggraph').hide();

});


function showNames(data) {

$('#allnames').removeAttr('disabled');
$('#loadingnames').hide();
var names = data.split("n");
var listLEN = names.length;
listLEN --;
//add the names into the list
for(var i = 0; i < listLEN; i++){
$('<option>').text(names[i]).click(showMeaning).attr('value', names[i]).appendTo($('#allnames'));
}
}


function showMeaning(){
$('#loadingmeaning').show();
$('#resultsarea').show();
window.selName = $(this).text();

$.ajax(
'http://.../babynames.php',
{
timeout:10000,
'type' : "GET",
'data': {
'type': 'meaning',
'name': window.selName,
},
'success' : disMeaning,
'error' : function(response){
if(response.timeout) {
disMeaning}
else{ajax_failed}
}

});
$('#meaning').empty();
}

function disMeaning(data){
var meaning = data;
//console.log(meaning);
$('#loadingmeaning').hide();
if (meaning.empty){
$('<p> No meaning found. </p>').appendTo($('#meaning'));
}
else{
$('<p>' + meaning + '</p>').appendTo($('#meaning'));
}
getRankings();
}

function getRankings(){
$('#resultsarea').show();
$('#loadinggraph').show();
$.ajax(
'http://.../babynames.php',
{
timeout:30000,
'type' : "GET",
'data': {
'type':'rank',
'name': window.selName,

},
'success' : disRanking,
'error' : ajax_failed,
})

}

function disRanking(ajax){
$('#loadinggraph').hide();
var rank = $(ajax);
rank.find('name').each(function(idx, e){
var year = $(e).attr('year');
var ranks = $(e).attr('rank');

$('<th>' + year(e)+'</th>').appendTo($('#graph'));
$('<td><div class = "bar">' + ranks(e) + '</div></td>').appendTo($('#graph'));

if (ranks > 250){
var newHeight = (((1000 - ranks) - 250)/4);
}
else{
var newHeight = ((250 - (1000 - ranks))/4);
}
$('.bar').css('height', newHeight + 'px');
if (rank <= 10){
$('.bar').css('color', 'red');
}
});
}

// ajax_failed is a handler function for the ajax "error" event
function ajax_failed(xhr, status, error){
if (xhr.status == 404){
// error 404 has occurred
alert('Error: 404! File was not found.')
}
}
[/CODE]
Copy linkTweet thisAlerts:
@flyntcoalauthorDec 11.2015 — Um okay.....so I keep getting an error message that

I keep getting msgs about how my variables are not functions ?

My Code:

[CODE]
$(document).ready(function() {
$('<p> Select a name from the list.</p>').appendTo($('#namearea'));
$('#search').hide();
$.ajax(
'http://.../babynames.php',
{
'type': "GET",
'data': {"type":"list"},
'success': showNames,
'error': ajax_failed
});

// hide loading image
$('#loadingmeaning').hide();
$('#loadinggraph').hide();

});


function showNames(data) {

$('#allnames').removeAttr('disabled');
$('#loadingnames').hide();
var names = data.split("n");
var listLEN = names.length;
listLEN --;
//add the names into the list
for(var i = 0; i < listLEN; i++){
$('<option>').text(names[i]).click(showMeaning).attr('value', names[i]).appendTo($('#allnames'));
}
}


function showMeaning(){
$('#loadingmeaning').show();
$('#resultsarea').show();
window.selName = $(this).text();

$.ajax(
'http://.../babynames.php',
{
timeout:10000,
'type' : "GET",
'data': {
'type': 'meaning',
'name': window.selName,
},
'success' : disMeaning,
'error' : function(response){
if(response.timeout) {
disMeaning}
else{ajax_failed}
}

});
$('#meaning').empty();
}

function disMeaning(data){
var meaning = data;
//console.log(meaning);
$('#loadingmeaning').hide();
if (meaning.empty){
$('<p> No meaning found. </p>').appendTo($('#meaning'));
}
else{
$('<p>' + meaning + '</p>').appendTo($('#meaning'));
}
getRankings();
}

function getRankings(){
$('#resultsarea').show();
$('#loadinggraph').show();
$('#graph').empty();
$.ajax(
'http://.../babynames.php',
{
//timeout:30000,
'type' : "GET",
'data': {
'type':'rank',
'name': window.selName,

},
'success' : disRanking,
'error' : ajax_failed,
})

}

function disRanking(data){
$('#loadinggraph').hide();
var rank = data;
for(var i = 0; i < 12; i++){

var year = $(i).attr('year');
var ranks = data;

//console.log(year);
console.log(ranks);
$('<th>' + year(i)+'</th>').appendTo($('#graph'));
$('<td><div class = "bar">' + ranks(i) + '</div></td>').appendTo($('#graph'));

if (ranks > 250){
var newHeight = (((1000 - ranks) - 250)/4);
}
else{
var newHeight = ((250 - (1000 - ranks))/4);
}
$('.bar').css('height', newHeight + 'px');
if (rank <= 10){
$('.bar').css('color', 'red');
}
}
}

// ajax_failed is a handler function for the ajax "error" event
function ajax_failed(xhr, status, error){
if (xhr.status == 404){
// error 404 has occurred
alert('Error: 404! File was not found.')
}
}
[/CODE]
Copy linkTweet thisAlerts:
@flyntcoalauthorDec 11.2015 — TypeError: rank is not a function
Copy linkTweet thisAlerts:
@flyntcoalauthorDec 11.2015 — Now I am getting information. Only problem....I am getting the same bit of information over and over again. If I go for the for loop method, (which is the way I think will work) all I get is the first number followed by "undefined." Below is my new code. I need it to print the rankings once for each and separate them and then the years, it needs to go through each year.

Example of the output:

1900 1900 1900 1900 1900 1900 1900 1900 1900 1900 1900 1900

0 0 0 0 0 0 0 0 537 451 428

0 0 0 0 0 0 0 0 537 451 428

0 0 0 0 0 0 0 0 537 451 428

0 0 0 0 0 0 0 0 537 451 428

0 0 0 0 0 0 0 0 537 451 428

0 0 0 0 0 0 0 0 537 451 428

0 0 0 0 0 0 0 0 537 451 428

0 0 0 0 0 0 0 0 537 451 428

0 0 0 0 0 0 0 0 537 451 428

0 0 0 0 0 0 0 0 537 451 428

0 0 0 0 0 0 0 0 537 451 428

0 0 0 0 0 0 0 0 537 451 428


My code
[CODE]
$(document).ready(function() {
$('<p> Select a name from the list.</p>').appendTo($('#namearea'));
$('#search').hide();
$.ajax(
'http://.../babynames.php',
{
'type': "GET",
'data': {"type":"list"},
'success': showNames,
'error': ajax_failed
});

// hide loading image
$('#loadingmeaning').hide();
$('#loadinggraph').hide();

});


function showNames(data) {

$('#allnames').removeAttr('disabled');
$('#loadingnames').hide();
var names = data.split("n");
var listLEN = names.length;
listLEN --;
//add the names into the list
for(var i = 0; i < listLEN; i++){
$('<option>').text(names[i]).click(showMeaning).attr('value', names[i]).appendTo($('#allnames'));
}
}


function showMeaning(){
$('#loadingmeaning').show();
$('#resultsarea').show();
window.selName = $(this).text();

$.ajax(
'http://.../babynames.php',
{
timeout:10000,
'type' : "GET",
'data': {
'type': 'meaning',
'name': window.selName,
},
'success' : disMeaning,

'error' : function(response){
if(response.timeout) {
disMeaning}
else{ajax_failed}
}

});
$('#meaning').empty();
}

function disMeaning(data){
var meaning = data;
//console.log(meaning);
$('#loadingmeaning').hide();
if (meaning.empty){
$('<p> No meaning found. </p>').appendTo($('#meaning'));
}
else{
$('<p>' + meaning + '</p>').appendTo($('#meaning'));
}
getRankings();
}

function getRankings(){
$('#resultsarea').show();
$('#loadinggraph').show();
$('#graph').empty();
$.ajax(
'http://.../babynames.php',
{
//timeout:30000,
'type' : "GET",
'data': {
'type':'rank',
'name': window.selName,

},
'success' : disRanking,
'error' : ajax_failed,
});

}

function disRanking(data){
$('#loadinggraph').hide();
var rank = data;


var year = $(data).find('rank').attr('year');
var ranks = $(data).text();
$('<tr>').appendTo($('#graph'));
for(var i = 0; i < 12; i++){

$('<th>' + year+'</th>').appendTo($('#graph'));
$('</tr>').appendTo($('#graph'));
}
$('<tr>').appendTo($('#graph'));
for(var i = 0; i < 12; i++){

$('<td><div class = "bar">' + ranks + '</div></td>').appendTo($('#graph'));

if (ranks > 250){
var newHeight = (((1000 - ranks) - 250)/4);
}
else{
var newHeight = ((250 - (1000 - ranks))/4);
}
$('.bar').css('height', newHeight + 'px');
if (rank <= 10){
$('.bar').css('color', 'red');
}
}
$('</tr>').appendTo($('#graph'));

}

// ajax_failed is a handler function for the ajax "error" event
function ajax_failed(xhr, status, error){
if (xhr.status == 404){
// error 404 has occurred
alert('Error: 404! File was not found.');
}
}
[/CODE]
Copy linkTweet thisAlerts:
@flyntcoalauthorDec 11.2015 — well I split it and now i get

1900 undefined undefined undefined undefined undefined undefined undefined undefined undefined undefined undefined
Copy linkTweet thisAlerts:
@flyntcoalauthorDec 11.2015 — well....if anyone is following this (cuz feel like I am not going to get help here)...I do have the rankings separate, but they are going to a new line like they should since they are wrapped in a table row.
Copy linkTweet thisAlerts:
@benmartin101Dec 14.2015 — So this ranking data is on another file that is in xml format? And your problem is you don't know how to parse the data (in xml format)?
×

Success!

Help @flyntcoal spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...