www.webdeveloper.com
Results 1 to 9 of 9

Thread: using an array with json

  1. #1
    Join Date
    Jan 2010
    Posts
    127

    using an array with json

    I've a bit of code which I'm having problems with.

    I've built my function to run on a button click which works fine, posts my varaibles into a php file and the php file sends back the information to my javascript. However I want to send an array back to my javascript but it doesnt seem to quite work.

    the php array:

    Code:
    $variable = array( newp => $newp, totalitem => $totalitem );
    	echo json_encode($variable);
    my success function:

    Code:
    success: function(hp) {
    		
        	document.getElementById('hp').innerHTML = hp.totalitem;
    		document.getElementById('points').innerHTML = hp.newp;
    Any help would be great,

    Thanks,

    Aravona
    L.U.N.E for anyone and everyone who has an interest in literature

  2. #2
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,648
    php
    PHP Code:
    $variable = array( newp => $newptotalitem => $totalitem );
    $mystring='';
    foreach(
    $variable as $val => $key){$mystring.=$key.';';}
    echo 
    json_encode($mystring); 
    js
    Code:
    function(hp) {
    hp=hp.split(';');		
    document.getElementById('hp').innerHTML = hp[1];
    document.getElementById('points').innerHTML = hp[0];
    Last edited by Padonak; 04-03-2012 at 04:53 AM. Reason: typo
    xxx: Guess Buddhist riddle: "What is the sound of one hand clapping?"
    yyy: facepalm

  3. #3
    Join Date
    Jan 2010
    Posts
    127
    Thanks for such a swift reply, but my two spans (hp / points) are still not updating.
    L.U.N.E for anyone and everyone who has an interest in literature

  4. #4
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,648
    hp=hp.split(';');
    alert(hp);

    and check what is there. it is hard to imagine what you are sending from php
    xxx: Guess Buddhist riddle: "What is the sound of one hand clapping?"
    yyy: facepalm

  5. #5
    Join Date
    Jan 2010
    Posts
    127
    Absolutely nothing happens, so I'm guessing the problem goes further back than that. In fact nothing within my success seems to be working.

    It should simply be two numbers being outputted.
    L.U.N.E for anyone and everyone who has an interest in literature

  6. #6
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,648
    what kind of error have you got?

    if there was no any alert() it would be good to check with alert() the hp variable which you're passing to the function - what is there?
    xxx: Guess Buddhist riddle: "What is the sound of one hand clapping?"
    yyy: facepalm

  7. #7
    Join Date
    Jan 2010
    Posts
    127
    No error, nothing changes on my page at all.

    I don't understand what you mean by the second sentance sorry.
    L.U.N.E for anyone and everyone who has an interest in literature

  8. #8
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,648
    my english is not good sorry. i meant that it would be good to check what is the hp var:

    Code:
    function(hp) {
    hp=hp.split(';');		
    alert(hp);
    document.getElementById('hp').innerHTML = hp[1];
    document.getElementById('points').innerHTML = hp[0];
    but if this alert() doesn't work i would check the same way the hp var which is passed to the function:

    Code:
    function(hp) {
    xxx: Guess Buddhist riddle: "What is the sound of one hand clapping?"
    yyy: facepalm

  9. #9
    Join Date
    Jan 2010
    Posts
    127
    thats just part of my ajax - its the variable in the .php file.

    Code:
    echo json_encode($mystring);
    The code in my .php file works - everything that side works apart from where you gave me:

    Code:
    $variable = array( newp => $newp, totalitem => $totalitem );
    $mystring='';
    foreach($variable as $val => $key){$mystring.=$key.';';}
    echo json_encode($mystring);
    It should be sending the array back to here to be used in the function(hp) - Ive done this fine with a single variable just not an array.

    Code:
    $.ajax({
      		type: 'POST',
      		url: 'store.php',
      		data: { 
    				'function': 'hp',
    				'quantity': hpqty
    			  },
      		dataType: 'json',
      		cache: false,
      		success: function(hp) {
    
      		// code to change my spans
    
    }
    L.U.N.E for anyone and everyone who has an interest in literature

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles