Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Location
    On planet earth
    Posts
    131
    Rep Power
    14

    Ajax help needed


    I am trying to scan a folder, well I have done that part with the php script.

    I am using AJAX/Javascript to parse the results to a html page. It is showing the echoed out results, but not in the way that I like it

    It is showing the result in a continuous line, which I want it to be in a different line break rather than a single line

    Code:
    <script type="text/javascript">
        $(document).ready(function(){
        //$("button").click(function(){
            $.ajax({
                type: 'POST',
                url: 'fold.php',
                success: function(data) {
                    $("p").text(data);
                }
            });
    });
    </script>
    This is the echoed out display
    1354876944ABF.jpg_MG_0085.jpg
    And this is how I want it to look like

    1354876944ABF.jpg
    _MG_0085.jpg
  2. #2
  3. Lord of the Dance
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Oct 2003
    Posts
    4,131
    Rep Power
    2011
    The content you print comes from the text returned by fold.php.
    You have to look at that page on how it is formatted.

    Comments on this post

    • cssbonding agrees : Thanks for trying to help
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Location
    On planet earth
    Posts
    131
    Rep Power
    14
    Originally Posted by MrFujin
    The content you print comes from the text returned by fold.php.
    You have to look at that page on how it is formatted.
    In the php foreach loop, it has just
    PHP Code:
    echo $file
    I have tried to use html element tags like <br>, but all that happens is gets displayed on the output
  6. #4
  7. Lord of the Dance
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Oct 2003
    Posts
    4,131
    Rep Power
    2011
    I am not sure what you mean with:
    is gets displayed on the output
    How did you add the <br> tag? you need to show more code.

    Have you tried to call fold.php directly in the browser, to see how the raw output looks like?
  8. #5
  9. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Location
    On planet earth
    Posts
    131
    Rep Power
    14
    Originally Posted by MrFujin
    I am not sure what you mean with:
    How did you add the <br> tag? you need to show more code.

    Have you tried to call fold.php directly in the browser, to see how the raw output looks like?
    Not sure either, but what I was trying to say is that that with the <br> tag, it was showing the same all the time

    About the fold.php, tried it directly, and yes, it is showing the same thing, should I use the "return" statement or whatever it is?
  10. #6
  11. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2009
    Location
    Jakarta, Indonesia.
    Posts
    279
    Rep Power
    37
    Hi.
    Any HTML tag sting you put into text() argument will be ignored I guess. Wanna try jQuery's html() ?
    Ref: .html() | jQuery API Documentation
  12. #7
  13. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2016
    Posts
    39
    Rep Power
    2
    Correct, jQuery .html() is what you need, or add linebreaks to your PHP output:

    PHP Code:
    echo $file."\n"


    That would sort it.

    Have you considered returning Json from PHP using json_encode() which you could then manage as an object in jQuery? This would give you more power client-side but would then add Javascript reliability to your formatting.
    The PHP method is easier.
    Thank You :: John Davies, || Learn how to install WordPress in 5 minutes
    Belira - Web Hosting, Domain Names, Reseller Hosting, VPS Hosting & Dedicated Servers
    Reliable & Professional WordPress Hosting, Joomla Hosting, e-Commerce Website Hosting
    cPanel, Domain Names, SSL Certificates, with A Close & Personal Support 24/7/365
  14. #8
  15. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Location
    On planet earth
    Posts
    131
    Rep Power
    14
    Originally Posted by hdewantara
    Hi.
    Any HTML tag sting you put into text() argument will be ignored I guess. Wanna try jQuery's html() ?
    Ref: .html() | jQuery API Documentation
    Tried that, still the same
  16. #9
  17. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Location
    On planet earth
    Posts
    131
    Rep Power
    14
    Originally Posted by Belira-John
    Correct, jQuery .html() is what you need, or add linebreaks to your PHP output:

    PHP Code:
    echo $file."\n"


    That would sort it.

    Have you considered returning Json from PHP using json_encode() which you could then manage as an object in jQuery? This would give you more power client-side but would then add Javascript reliability to your formatting.
    The PHP method is easier.
    All the json_encode has done, is put the results in double quotes
  18. #10
  19. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2016
    Posts
    39
    Rep Power
    2
    Originally Posted by cssbonding
    All the json_encode has done, is put the results in double quotes
    Yeah, passing the data as Json would allow you to manage each item back seperately, either placing inside an <ul> as a <li> but you'd need to change your jQuery for that.

    Does adding the \n linebreak work when using jQuery text()?

    Comments on this post

    • cssbonding agrees : thanks for trying to help
    Thank You :: John Davies, || Learn how to install WordPress in 5 minutes
    Belira - Web Hosting, Domain Names, Reseller Hosting, VPS Hosting & Dedicated Servers
    Reliable & Professional WordPress Hosting, Joomla Hosting, e-Commerce Website Hosting
    cPanel, Domain Names, SSL Certificates, with A Close & Personal Support 24/7/365
  20. #11
  21. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Location
    On planet earth
    Posts
    131
    Rep Power
    14
    Originally Posted by Belira-John
    Yeah, passing the data as Json would allow you to manage each item back seperately, either placing inside an <ul> as a <li> but you'd need to change your jQuery for that.

    Does adding the \n linebreak work when using jQuery text()?
    This is my new echo statement
    PHP Code:
    echo json_encode($file),"\n"
    And it doesn't do diddly squat of any difference
  22. #12
  23. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2016
    Posts
    39
    Rep Power
    2
    Originally Posted by cssbonding
    This is my new echo statement
    PHP Code:
    echo json_encode($file),"\n"
    And it doesn't do diddly squat of any difference
    Get rid of json_encode it was a completely different suggestion that would need changes to your JS. It was just a different solution to look in to.

    In fact, if you have it showing right in the PHP file then all you need to do with jQuery when you are ready is:

    Code:
    $('#pID').load('path/to/php/file.php');
    Ensure you change pID to the ID of your element you want the data loading in to.
    Thank You :: John Davies, || Learn how to install WordPress in 5 minutes
    Belira - Web Hosting, Domain Names, Reseller Hosting, VPS Hosting & Dedicated Servers
    Reliable & Professional WordPress Hosting, Joomla Hosting, e-Commerce Website Hosting
    cPanel, Domain Names, SSL Certificates, with A Close & Personal Support 24/7/365
  24. #13
  25. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Location
    On planet earth
    Posts
    131
    Rep Power
    14
    Still the same
  26. #14
  27. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2016
    Posts
    39
    Rep Power
    2
    Originally Posted by cssbonding
    Still the same
    Can you share a live link to your HTML/JS page, and PHP page?
    Thank You :: John Davies, || Learn how to install WordPress in 5 minutes
    Belira - Web Hosting, Domain Names, Reseller Hosting, VPS Hosting & Dedicated Servers
    Reliable & Professional WordPress Hosting, Joomla Hosting, e-Commerce Website Hosting
    cPanel, Domain Names, SSL Certificates, with A Close & Personal Support 24/7/365
  28. #15
  29. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2009
    Location
    Jakarta, Indonesia.
    Posts
    279
    Rep Power
    37
    I'd usually go with the "json_encode()" approach, suggested by Belira-John.
    But in your case, maybe it's much easier to format the HTML files string right away from PHP.

    This would be your fold.php:
    Code:
    echo $file.'<br>';
    and is accompanied by following jQuery ajax:
    Code:
    $.ajax({
                type: 'POST',
                url: 'fold.php',
                success: function(data) {
                    $("p").html(data);
                }
    });

    Comments on this post

    • cssbonding agrees : Thank you, thank you, thank you :)
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo