1

I am trying to pass an array via the data var of an href link:

<a href="#" class="edit" data-myvar="<?php echo json_encode($row); ?>">Test</a>

I am using Jquery to process the variable:

$(function(){

     $('.edit').click(function(){

          var res=jQuery.parseJSON($(this).data("myvar"));

      console.log(res);

});


});

The console log says: Uncaught SyntaxError: Unexpected end of input

Any ideas?

Thanks.

2
  • To get that error you have to pass nothing into the parse function. Do a console.log of $(this).data("myvar") and see what that returns :) Commented Sep 16, 2013 at 15:52
  • Show us the actual rendered HTML, not with php tags in it, and we can help. It's the value that's in data-myvar that is the problem. Commented Sep 16, 2013 at 15:57

2 Answers 2

1

First of all, apply htmlentities() in your json_encode() call to ensure that all quotes are okay in HTML.

jQuery already does JSON parsing on data- attributes for you, if it contains valid JSON syntax. So doing the following is the right way of doing it:

// this will contain the JSON presentation of $row variable from PHP.    
var res = $(this).data("myvar");

From the jQuery Docs:

Every attempt is made to convert the string to a JavaScript value (this includes booleans, numbers, objects, arrays, and null). A value is only converted to a number if doing so doesn't change the value's representation. For example, "1E02" and "100.000" are equivalent as numbers (numeric value 100) but converting them would alter their representation so they are left as strings. The string value "100" is converted to the number 100.

When the data attribute is an object (starts with '{') or array (starts with '[') then jQuery.parseJSON is used to parse the string; it must follow valid JSON syntax including quoted property names. If the value isn't parseable as a JavaScript value, it is left as a string.

Working fiddle

Sign up to request clarification or add additional context in comments.

9 Comments

ok I removed the json encode used within the HTML. Now when I try to access the array: res["user_id"]=undefined, res[0]=A, res[1]=r... and so on.. I'm confused
You don't remove json_encode(), you remove $.parseJSON, which is done internally by jQuery itself.
I have also enhanced my answer to better clarify future readers about this.
I had already trried your suggestion, using json_encode on the html side and NOT using $.parseJSON. now my output is "undefined" for the 3 examples I provided above!
So check the contents of your $row variable.
|
1

You never declare a variable:

var jQuery.parseJSON(res=$(this).data("myvar"));
                     ^ Not how to declare a variable

Should be:

var res = jQuery.parseJSON($(this).data("myvar"));

3 Comments

I have corrected my code, that was an error I made when making my post, the problem still exists with the correction
@AlanA -- You're also spelling edit wrong in your click handler. Also, what line is the error occurring on?
Apologies 'edit' was a typo, my original code was spelt correctly

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.