air
air

Reputation: 6264

Show JSON with jquery

I have following PHP code

 $val="<div id=user".$row['cid']." userid=".$row['cid']." class=innertxt><img src=images/images.jpg width=50 height=50><strong>".$uname."</strong><ul> <li>Email: ".$row['cemail']."</li> <li> <input type=checkbox id=select".$row['cid']." value=".$row['cid']." class=selectit /></li> </ul> </div>" ;

 $return["foo"] =$val;

 print json_encode($return);

but once i get result i get in following format ?

<div id=user11 userid=11 class=innertxt>
    <img src=images\/images.jpg width=50 height=50>
    <strong>Ruby<\/strong>
    <ul>
        <li>Email: [email protected]<\/li>
        <li> <input type=checkbox id=select11 value=11 class=selectit \/><\/li> 
    <\/ul> 
<\/div>

why i am getting this / and how to solve it?

Upvotes: 0

Views: 147

Answers (2)

Irshad Hussain
Irshad Hussain

Reputation:

The stripslashes method can be used. Checkout the following snippet:

print stripslashes(json_encode($return));

more details can be found at the php documentation

Upvotes: 1

K. Norbert
K. Norbert

Reputation: 10674

Slashes should be escaped with a backslash, so the output is correct.

Try this:

var x = eval({ var: "<\/div>" });
alert(x.var);

It will produce the correct output. (</div>)

The interesting thing is, that you got a string, instead of an object. Are you sure that the code fragment is correct? Because it shoud be:

{ foo: <div id=user11 userid=11 class=innertxt><img src=images\/images.jpg width=50 height=50><strong>Ruby<\/strong><ul> <li>Email: [email protected]<\/li> <li> <input type=checkbox id=select11 value=11 class=selectit \/><\/li> <\/ul> 
<\/div> }

Are you sure you wrote json_encode($return), instead of json_encode($return['foo'])?

Upvotes: 1

Related Questions