JSON-problem (maybe)

Discussion in 'jQuery' started by PoPSiCLe, May 17, 2014.

  1. #1
    Okay. I'm returning a result-set from a PHP-file via JSON (json_encode()).

    However, for some reason, I can't access the result in the displaying file. Let me clarify:
    I'm returning an HTML form, with radiobuttons and checkboxes etc. - this returns fine, but I can't, for some reason, access the form elements via jQuery.

    I thought a simple $('selector').on('click', function() {}) would allow me to access the returned values, but for some reason it doesn't work.

    I'm a little baffled - how would I access the returned form/elements from an outside function? As far as I understand, .on() should target dynamically created elements, and should work on the Ajax-returned elements.
    I had a look at the JSON returned, and every HTML-element is returned with escaped double quotes - I'm just wondering if that's the reason?

    Anyone have any hints?
     
    PoPSiCLe, May 17, 2014 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    Well, it seems I fixed it (sorta) by adding it to a named function, and calling that function in the callback from the Ajax-function. Although I still have some issues with the form, I'll post separate questions about that.
     
    PoPSiCLe, May 17, 2014 IP
  3. Jeremy Benson

    Jeremy Benson Well-Known Member

    Messages:
    364
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    123
    #3
    I find it easier to mess with form fields with straight JavaScript and use Jquery for Ajax alone :p
     
    Jeremy Benson, May 22, 2014 IP
  4. somasounds

    somasounds Member

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    31
    #4
    Not sure if this was the issue, but it seems like you had problems with where you wanted to handle the new data. It should be in your callback, and oftentimes I find putting my new jquery in my ajax file (the php file being called) works the best, but really only if what is being returned is supposed to be HTML and javascript (not variables).
     
    somasounds, May 22, 2014 IP
  5. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #5
    Figured the problem out - a bit too lengthy to get into here, but it was some other parts of the script messing with IDs and name-tags, hence making what I thought was the correct access-function behave a bit irratic. Fixed it, as I said, and it works just fine now.
    However, I had to make the things I wanted to do into a function, and call it inside the callback for the $.post to be able to access all the information I needed.
     
    PoPSiCLe, May 22, 2014 IP