0

I am not that good at ajax, javascript and so on.

I echoed from my PHP file 2 JSON arrays:

{key":"R\/EPspiig3jNffjjE6YWTsB+rsdlCjqnm1LExC\/vJXE=","nonce":"1aab51c5d8b23b7c110ae3f2a2d440bf0797750c85bd1602e8d57c357f34dab9"}

and

{"error":1,"message":"\u05d4\u05d0\u05d9\u05de\u05d9\u05d9\u05dc \u05e9\u05d4\u05d6\u05e0\u05ea \u05e9\u05d2\u05d5\u05d9 \u05d0\u05d5 \u05dc\u05d0 \u05ea\u05e7\u05d9\u05df \u05e0\u05e1\u05d4 \u05e9\u05d5\u05d1 ,\u05d0\u05e0\u05d0 \u05d4\u05db\u05e0\u05e1 \u05e1\u05d9\u05e1\u05de\u05d0"}

in my ajax i have this :

success: function(data) {

           obj = JSON && JSON.parse(data) || $.parseJSON(data);

           if(data[0].login == true) 
            {  
                window.location = "index.php";

            }
            else
              {
             $("#siimage").trigger("click");
             $("form input:submit").effect("shake", {times:2}, 100);
             $("#errorMessage").html(data[0].message); 
             $("#nonce").val(data[1].nonce);
             $("#key").val(data[1].key);
              }




         if(data.cblocked == true)
         {
         $("#email").prop('disabled', true);
         $("#password").prop('disabled', true);
         $('input[type="submit"]').attr('disabled','disabled');
         document.getElementById("Submit").value = 'נעול';

         }
          if(data.csrf == true)
         {
         $("#email").prop('disabled', true);
         $("#password").prop('disabled', true);
         $('input[type="submit"]').attr('disabled','disabled');
         document.getElementById("Submit").value = 'נעול';

         }

this the problem :

$("#nonce").val(data[1].nonce);
             $("#key").val(data[1].key);

how i using that ,because i have 2 arrays anyway i using parse and handle 2 arrays this way ? like data[0] = array 1 and data[1] = array 2 ?

user2635001
  • 163
  • 1
  • 4
  • 21

1 Answers1

0

First, check the .responseText property of your Ajax request object (the HttpRequestObject) like this:

alert(request.responseText)

If that looks right, you've got two arrays sort of mashed together. You need to split them up using .split() and some character pattern (I used ::: for example):

twoarrays = request.responseText.split(":::");

Now, you've got two strings in JSON format at addresses [0] and [1] in twoarrays. Use .parse() to convert each one back into an array.

If you don't understand what I'm talking about, it's time for you to do some research. It's not tricky stuff, you'll just have to spend some time getting it in your head.


(old answer)

If you mean the Javascript side, then

yourstring.parse()

should work fine (Parse JSON in JavaScript?).

You may need to .split() the response using a newline or something:

twoarrays = request.responseText.split("\n");
array1 = twoarrays[0].parse();
array2 = twoarrays[1].parse();

If you mean the PHP side, then you can use json_decode($yourstring, true) which parses the JSON into an associative array (the one with names instead of numbers for addresses).

Just FYI, json_encode($yourarray) will do the opposite.

Community
  • 1
  • 1
Ben
  • 54,723
  • 49
  • 178
  • 224
  • thanks for ur help but not to fast i am newbie :P , i have this success: function(data) { can u ex how i do it on the data , sry i am very very newbie and learning things out.. – user2635001 Aug 11 '13 at 04:50
  • No worries. First, are you talking about PHP or Javascript? Looks like Javascript? Then, don't panic, and try what I said: `.parse()`. – Ben Aug 11 '13 at 04:51
  • yea javascript , i need to handle 2 arrays , 1 is error array and the other is just a hashed keys , please look my question i edited. – user2635001 Aug 11 '13 at 04:52
  • If there's no error, that means you're doing something right. Just a minute I'll edit my answer. – Ben Aug 11 '13 at 04:54
  • look my edited question , i have 2 json arrays , i need 1 to echo php error and the other just val hashed keys , as far i have the good json response but i dont know how to parse it so its both work – user2635001 Aug 11 '13 at 04:58
  • Wow now you've changed your question again. Sorry, I'm tired of helping now. Next time please make a clear, well formatted request that is easy for people to understand. Incidentally, it looks like there's a lot of code in your javascript that you don't understand. Start there. – Ben Aug 11 '13 at 05:00
  • sry, i have bad english :( .. i know everything i just dont know how to handle 2 json arrays that come from my php.. – user2635001 Aug 11 '13 at 05:02
  • Your English is OK. Your Javascript needs some work. Your code is too complicated for you to handle, I think. Start with something more basic. – Ben Aug 11 '13 at 05:06
  • i made that javascript alone... till now i used just 1 array that handle login form errors , but now i using 2 arrays so i dont know how to use with 2 arrays , so 1 will echo the errors like before and the 2 other will val hashed keys can u still help me to fix it so it will work with 2 arrays ? :\ – user2635001 Aug 11 '13 at 05:12