1

php urldecode() function is not work in php append function for example my description is store with encoded using urlencode function and then i will disply using json and json data used in jquery append function here is json code

[
{
"discription":"It%27s+too+hard+to+climb+on+that+but+we+got+life+one+time%2C+"   }
]

This json is read using jquery and load data in using jquery append method below is my jquery function

<script type="text/javascript">
    function loaddreampost()
    { 
        $.ajax({
            type:"POST",
            url:"dreamworld_ajax.php",
            success:function(data)
            {
                var dreamdata=JSON.parse(data);
                for(var i in dreamdata)
                {
                    $(".loaddreampost").append(                                             
                         "<div class='lblsizr'>"+
                            //here i try to decode data using urldecode function bt did not work
                             "<p><?php echo urldecode(dreamdis[i].discription)?></p>"+                                  
                         "</div>"
                    );  
                }    
            }
        });                     
    }

here is my html code

<div class="loaddreampost">

So please tell me how to decode data using php urldecode() function in jquery please help me thanks in advance

Hiren Jungi
  • 854
  • 8
  • 20
Vishal Patel
  • 49
  • 1
  • 6
  • 1
    Javascript is client side scripting and php is serverside scripting so you can't use varibles of javascript directly into php. Instead if you want to do urlencode then you can use javascript function **encodeURI()** – B. Desai Jan 28 '17 at 05:50

1 Answers1

0

You were near :)

use javascript's decodeURIComponent instead of php's urldecode

Here is the reference link

thanks to kennytm.

function loaddreampost()
    { 
        $.ajax({
            type:"POST",
            url:"dreamworld_ajax.php",
            success:function(data)
            {
                var dreamdata=JSON.parse(data);
                for(var i in dreamdata)
                {
                    $(".loaddreampost").append(                                             
                         "<div class='lblsizr'>"+
                            //here i try to decode data using urldecode function bt did not work
                             "<p>"+urldecode(dreamdis[i].discription)+"</p>"+                                  
                         "</div>"
                    );  
                }    
            }
        });                     
    }

function urldecode(url) {
  return decodeURIComponent(url.replace(/\+/g, ' '));
}
Community
  • 1
  • 1
milan kyada
  • 579
  • 5
  • 15