2 Replies - 458 Views - Last Post: 21 August 2012 - 01:27 PM

#1 sonalmac  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 83
  • Joined: 24-June 11

ajax or javascript problem

Posted 21 August 2012 - 12:57 PM

I have two forms on my index page. Both of these forms uses ajax to fetch and display data on the page from different php scripts. Both works great individually, showing results as expected. But, when I put both of them at the same time one of them is not showing resulting data. I have used addEventListener in my javascript to add events and my ajax part of both scripts use ajax.send(null) at the end of a function sending data off to php. What can be the problem? What am I missing?

thanks in advance

Is This A Good Question/Topic? 0
  • +

Replies To: ajax or javascript problem

#2 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2896
  • View blog
  • Posts: 10,031
  • Joined: 08-August 08

Re: ajax or javascript problem

Posted 21 August 2012 - 01:10 PM

Possibly they're using the same global variables. Hard to say without seeing code.

Since the PHP works I'll move this to Javascript.
Was This Post Helpful? 0
  • +
  • -

#3 sonalmac  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 83
  • Joined: 24-June 11

Re: ajax or javascript problem

Posted 21 August 2012 - 01:27 PM

I'm trying to copy-paste javascript codes of my two different files. I tried to change most of the variable names in my second script, but still problem is the same.

  //Global array declareation:
var titlesArray = new Array();
var namesArray = [];
var ltitlesArray = new Array();
var authorsArray = [];
var details = [];
var nbookArray = [];
var isbnArray = [];
var isbn13Array = [];
var summaryArray = [];
var publisherArray = [];
var editionArray = [];
var pricesArray = [];


//functio to handle ajax response:
function handleAjaxResponse(e) {
    'use strict';
    if(typeof e == 'undefined') e = window.event;
    var ajax = e.target || e.srcElement;
    
    if(ajax.readyState == 4 ) {
        if ((ajax.status >=200 && ajax.status < 300) || ajax.status == 304) {
            //clean up previous DOM elements:
            var rNode = document.getElementById('details');
            clearDetails(rNode);
            
                 if(ajax.responseXML) {
                            var bookList = ajax.responseXML.getElementsByTagName('BookList')[0];
                            var books = bookList.getAttribute('total_results');
                            
                            if(books > 0){
                            console.log(books);
                            var allBooks = ajax.responseXML.getElementsByTagName('BookData');
                            
                            for(var i=0, count = allBooks.length; i<count; i++){
                                titlesArray[i] = allBooks[i].getElementsByTagName('Title')[0].firstChild;
                                var title = titlesArray[i].nodeValue;
                                namesArray[i] = title; //sArray[i].nodeValue;
                                var bigTitle = allBooks[i].getElementsByTagName('TitleLong')[0].firstChild;
                                var longTitle = bigTitle ? bigTitle.nodeValue : title;
                                ltitlesArray[i] = longTitle;
                                
                                var author = allBooks[i].getElementsByTagName('AuthorsText')[0].firstChild;
                                var aname = author ? author.nodeValue : ' ';
                                authorsArray[i] = aname; 
                                
                                var summary = allBooks[i].getElementsByTagName('Summary')[0].firstChild;
                                var bsummary = summary ? summary.nodeValue : ' ';
                                summaryArray[i] = bsummary;
                                
                            //var bookData = allBooks[i].getElementsByTagName('BookData')[0];
                            var bookData = allBooks[i].getAttribute('isbn');
                            var nos = allBooks[i].getAttribute('isbn13');
                            //console.log(bookData); //[0].firstChild;
                            isbnArray[i] = bookData;
                            isbn13Array[i] = nos;
                            
                            var publisher = allBooks[i].getElementsByTagName('PublisherText')[0];
                            var pText = publisher.getAttribute('publisher_id');
                            publisherArray[i] = pText;
                            
                            //var prices = allBooks[i].getElementsByTagName('Prices')[0];
                            //var p1 = prices.getElementsByTagName('Price');
                               //var p2 = p1.getAttribute('store_title');
                               //console.log(p1.length);
                               //pricesArray[i] = p1.getAttribute('store_title');
                               //console.log(pricesArray[i]);
                            
                            
                            
                            }
                            } else {rNode.appendChild(document.createTextNode('No Book found'));} 
                                                       
                            } //else { rNode.appendChild(document.createTextNode('No Book found')); //document.writeln('No books'); }
                            
                            searchSuggest();  //function to show autolist. I have not included it here, to make it short.
        }
    }
   
  function sendValue() {
     var searchTerm = document.getElementById('searchTerm');
     searchTerm = searchTerm.value;
     if(searchTerm.length > 3) {    
    var ajax = getXMLHttpRequestObject();
    ajax.onreadystatechange = handleAjaxResponse;
    //ajax.open('GET', 'isbn31.php?searchterm=' + encodeURIComponent(searchTerm.value), true);
    ajax.open('GET', 'isbn31.php?searchterm=' + encodeURIComponent(searchTerm), true);
    
        ajax.send(null);
       // return false;
     }
}

function submitCheck(e) {
    e = e || window.event;
    var target = e.target || window.event.srcElement;
    
    var term = document.getElementById('searchTerm').value;
    if(!term || term.length == 0 || term == ' ') {
        term.className = 'error';
        term.value = "Please enter book title";
        alert("Please enter book title.")
        return false;
    }
    
    if(e.preventDefault) {
        e.preventDefault();
    } else {
        e.returnValue = false;
    }
    return false;
}

window.onload = function() {
    
    'use strict';
    console.log("booksearch");
    //document.getElementById('searchTerm').onkeyup = sendValue;
    //document.getElementById('isbnForm').onsubmit = submitCheck;
   U.addEvent(document.getElementById('searchTerm'), 'keyup', sendValue);
   U.addEvent(document.getElementById('isbnForm'), 'submit', submitCheck);
    
        
}
 
}





Second file:

function getData(e) {
    'use strict';
    if(typeof e == 'undefined') e = window.event;
    var Iajax = e.target || e.srcElement;
   
    Iajax.onreadystatechange = function() {
   
        if(Iajax.readyState==4) {
            if((Iajax.status >= 200&& Iajax.status<300) || (Iajax.status==304)) {
               var IrNode = document.getElementById('show');
               IrNode.innerHTML = '';
            
                 if(Iajax.responseXML) {
                            var Ibooks = Iajax.responseXML;
                            var IbookList = Ibooks.getElementsByTagName('BookList');
                            var Ibookno = IbookList[0].getAttribute('total_results');
                            if(Ibookno == 1) {
                            var Ititle = Ibooks.getElementsByTagName("Title")[0].firstChild.nodeValue;
                            //console.log(title);
                            IrNode.innerHTML = Ititle;
                            }
                            
            }
        }  else { document.getElementById('show').innerHTML = 'No book found'; }
    }
}
    
}  //End of function getData.

function sendVal() {
     var IsearchTerm = document.getElementById('searchterm');
     IsearchTerm = IsearchTerm.value;
     var Iajax = getXMLHttpRequestObject();
     Iajax.onreadystatechange = getData;
    Iajax.open('GET', 'newisbn.php?searchterm=' + encodeURIComponent(IsearchTerm), true);
    
        Iajax.send(null);
       // return false;
}

window.onload = function() {
    
    U.addEvent(document.getElementById('searchterm'), 'blur', sendVal);
    U.addEvent(document.getElementById('isbn'), 'blur', sendVal);
    U.addEvent(document.getElementById("isbn"), 'onsubmit', sendVal);
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1