Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: Getting two values from AJAX json array

  1. #1
    Join Date
    Aug 2009
    Posts
    399
    Thanks
    42
    Thanked 4 Times in 4 Posts

    Default Getting two values from AJAX json array

    Hi,

    I have script.php page:
    PHP Code:
    <?php
    if (isset($_GET['nick'])) {
    $name 'aurimas';
    $ats $_GET['nick'];
    if (
    $ats == '') {$nikas "Įveskite niką";} elseif ($name == $ats) {$nikas "Užimtas";} else {$nikas "Laisvas";}
    }
    if (isset(
    $_GET['email'])) {
    $email 'auriaks@gmail.com';
    $get_mail $_GET['email'];
    if (
    $email == $get_mail) {$emailas "Užimtas";} else {$emailas "Laisvas";}
    }
    $answer = array ($nikas$emailas);
    echo 
    json_encode($answer);
    ?>
    and I use this AJAX script to get the array from script:
    PHP Code:
    // Get the HTTP Object
    function gethttpObject(){
       if (
    window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP");
       else if (
    window.XMLHttpRequest) return new XMLHttpRequest();
       else {
          
    alert("Your browser does not support AJAX.");
          return 
    null;
       }
    }

    // Change the value of the outputText field
    function setOutput(){
        if(
    httpObject.readyState == 4){
            var 
    answer = eval('(' httpObject.responseText ')');
            
    document.getElementById('nikas').innerHTML answer[0];
            
    document.getElementById('emailas').innerHTML answer[1];
        }
     
    }
     
    // Implement business logic    
    function doWork(){    
        
    httpObject gethttpObject();
        if (
    httpObject != null) {
            
    httpObject.open("GET""script.php?nick=" document.getElementById('inputas').value+"&email="+document.getElementById('email').valuetrue);
            
    httpObject.send(null); 
            
    httpObject.onreadystatechange setOutput;
        }
    }
    setInterval(doWork5000);
     
    var 
    httpObject null;
     
    //--> 
    I know that something is wrong with this part:
    PHP Code:
    function setOutput(){
        if(
    httpObject.readyState == 4){
            var 
    answer = eval('(' httpObject.responseText ')');
            
    document.getElementById('nikas').innerHTML answer[0];
            
    document.getElementById('emailas').innerHTML answer[1];
        }
     

    Because I have to get those two values from array in two different places like:
    PHP Code:
    <div id='nikas'>FIRST VALUE HERE</div>
    <
    div id='emailas'>SECOND VALUE HERE</div
    Last edited by auriaks; 03-29-2010 at 10:48 PM.

  2. #2
    Join Date
    Aug 2009
    Posts
    399
    Thanks
    42
    Thanked 4 Times in 4 Posts

    Default

    Btw, my output was in format
    PHP Code:
    ["Laisvas","Laisvas"
    maybe this will help...

  3. #3
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Works here. Except that the ž in Užimtas was messing things up, and in IE the script.php page was getting cached, so try this for the script.php page (works here):

    Code:
    <?php 
    if (isset($_GET['nick'])) { 
    $name = 'aurimas'; 
    $ats = $_GET['nick']; 
    if ($ats == '') {$nikas = "Iveskite nika";} elseif ($name == $ats) {$nikas = "U#0382;imtas";} else {$nikas = "Laisvas";} 
    } 
    if (isset($_GET['email'])) { 
    $email = 'auriaks@gmail.com'; 
    $get_mail = $_GET['email']; 
    if ($email == $get_mail) {$emailas = "U#0382;imtas";} else {$emailas = "Laisvas";} 
    } 
    $answer = array ($nikas, $emailas); 
    echo json_encode($answer); 
    ?>
    and to fix the caching problem (on the requesting page, addition highlighted):

    Code:
    httpObject.open("GET", "script.php?nick=" + document.getElementById('inputas').value+"&email="+document.getElementById('email').value+"&bust="+new Date().getTime(), true);
    Last edited by jscheuer1; 03-27-2010 at 01:04 PM. Reason: fix and highlight unicode entity
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  4. #4
    Join Date
    Aug 2009
    Posts
    399
    Thanks
    42
    Thanked 4 Times in 4 Posts

    Default

    what about ajax?? Here is something wrong:
    PHP Code:
    function setOutput(){ 
        if(
    httpObject.readyState == 4){ 
            var 
    answer = eval('(' httpObject.responseText ')'); 
            
    document.getElementById('nikas').innerHTML answer[0]; 
            
    document.getElementById('emailas').innerHTML answer[1]; 
        } 
      


  5. #5
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    All I can say is that it works here under WAMP in both Firefox and IE. Nothing wrong with that part of the code, other than the fact that it uses eval, but I suppose that would be hard to avoid here, and causes no error for the page (it might if an unexpected value were entered). Here is my requesting page:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
       "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <script type="text/javascript">
    // Get the HTTP Object 
    function gethttpObject(){ 
       if (window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP"); 
       else if (window.XMLHttpRequest) return new XMLHttpRequest(); 
       else { 
          alert("Your browser does not support AJAX."); 
          return null; 
       } 
    } 
    
    // Change the value of the outputText field 
    function setOutput(){ 
        if(httpObject.readyState == 4){ 
            var answer = eval('(' + httpObject.responseText + ')'); 
            document.getElementById('nikas').innerHTML = answer[0]; 
            document.getElementById('emailas').innerHTML = answer[1]; 
        } 
      
    } 
      
    // Implement business logic     
    function doWork(){     
        httpObject = gethttpObject(); 
        if (httpObject != null) { 
            httpObject.open("GET", "script.php?nick=" + document.getElementById('inputas').value+"&email="+document.getElementById('email').value+"&bust="+new Date().getTime(), true); 
            httpObject.send(null);  
            httpObject.onreadystatechange = setOutput; 
        } 
    } 
    setInterval(doWork, 5000); 
      
    var httpObject = null; 
      
    </script>
    </head>
    <body>
    <input type="text" id="inputas">
    <input type="text" id="email">
    <div id='nikas'>FIRST VALUE HERE</div> 
    <div id='emailas'>SECOND VALUE HERE</div> 
    </body>
    </html>
    My script.php page is in my previous post.
    Last edited by jscheuer1; 03-27-2010 at 01:02 PM. Reason: detail
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  6. #6
    Join Date
    Aug 2009
    Posts
    399
    Thanks
    42
    Thanked 4 Times in 4 Posts

    Default

    have you tried to enter those two inputs??? OUTPUT IS: undefined

  7. #7
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Fully tested and works here. Go back to post #3. I had some trouble getting the editor to show the entities. Without substituting the &#0382; for the two ž characters, I was getting 'null' or an empty string (depending upon the browser) for aurimas as nick and auriaks@gmail.com as email. But even without that change, it was fine for other values. You must pass a string value for each of the _GET's (nick and email) with the request though (even if it is an empty string):

    HTML Code:
    <input type="text" id="inputas">
    <input type="text" id="email">
    Without those on the requesting page, you will have an error on the script.php and one on the requesting page.

    However, perhaps your problem is a lack of support for some part(s) of the PHP code on the server.

    If you want more help:

    Please post a link to a page on your site that contains the problematic code so we can check it out.
    Last edited by jscheuer1; 03-28-2010 at 01:20 PM. Reason: spelling
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  8. #8
    Join Date
    Aug 2009
    Posts
    399
    Thanks
    42
    Thanked 4 Times in 4 Posts

    Default

    it works... I don't know why in wamp server it doesn't worked... Thanks for help anyway.

  9. #9
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    So you are saying it doesn't work on your WAMP and does work live? It could depend upon settings in WAMP, like default encoding and/or PHP version.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  10. #10
    Join Date
    Aug 2009
    Posts
    399
    Thanks
    42
    Thanked 4 Times in 4 Posts

    Default

    what I can change in wamp?

    Also, can you write me an example how to connect to wamp mysql? I googled, but found nothing valuable...

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •