Appending results to checkboxes

I have this script that returns results from fold.php, the problem is that it doesn’t return the result into a) a new line and b) since I know little about ajax/javascript

$(document).ready(function(){
    $("button").click(function(){
        $.ajax({
            type: 'POST',
            url: 'fold.php',
            success: function(data) {
                $("p").text(data);
            }
        });

});

If data is a string with \n characters for line breaks you’ll need to convert them to <br>'s to show in a web page.
If data contains HTML then you’ll want to use

$("p").html(data)

rather than

$("p").text(data)
1 Like

Didn’t work

Since the <p> element produces a new line by default, I’m not quite sure what exactly “didn’t work”… if you want to add content based on multiple AJAX calls you might want to use .append() rather than .html() or .text() (as these replace any content already there), like

// adds a new <p> element for every response
$('#targetContainerId').append('<p>' + data + '</p>');

or

// adds a new line with the data to any (!) <p> element
$('p').append(function() {
    return ($(this).html() ? '<br>' : '') + data;
});
1 Like

You’re not trying hard enough.

Can you please show us the output from fold.php ?

Or better yet, please link us to fold.php so that we can come up with a solution that works for your situation?

This is the output of the fold.php

1354876944ABF.jpg_MG_0085.jpg

Thank you.

I started a local server on my computer using easy php for the sake of testing, created a fold.php file that has the following:

echo "1354876944ABF.jpg_MG_0085.jpg";

and a test file that contains your code:

<button>Press me</button>
<p></p>
<script src="https://code.jquery.com/jquery-2.2.3.js"></script>
<script>
$(document).ready(function(){
    ...
});
</script>

The only issue that I found with your code was found when loading the page, where the console of my web browser said “unexpected end of input”. That meant that one of the code sections hadn’t been properly closed off.

The click section was missing its }); to close things off. With that in there, the following code works perfectly fine.

$(document).ready(function(){
    $("button").click(function(){
        $.ajax({
            type: 'POST',
            url: 'fold.php',
            success: function(data) {
                $("p").text(data);
            }
        });
    });
});

The revised ajax code looks like this

$(document).ready(function(){
$.ajax({
type: ‘POST’,
url: ‘fold.php’,
success: function(data) {
$(“p”).text(data);
}
});

There is no button element as this was purely something for testing purposes

Assuming you only forgot to paste in the end of your code, it looks good to me.
Glad you got things to work.

It’s not working according to what I want it to do

I think you’re going to have to provide people with more to go on than that. Can you mock up an idea of how you want things to look and upload it here?

More than what? What exactly do you want? I thought that the only problem is that ajax/javascript nothing else

I must be missing something. People have spent time providing a solution to the problem as you described it. Now you’re saying it doesn’t work the way you want it to. So how do you want it to work? Your responses so far don’t contain an awful lot to go on.

No, there’s no info here to miss. This is why I replied with “You’re not trying hard enough”.

@cssbonding if you want help with code you need to provide a full working example and a description of what problem you’re trying to solve, no one here knows what you want to achieve.

2 Likes

What do you want it to contain that it hasn’t already contained? Do you want me to write up a whole 400 word thesis for you? If it doesn’t work, it doesn’t work, what more do you want me to explain?

What doesn’t work? No output? Incorrect output? Wrong formatting? Error messages? What?

How about reading the first post?

I will say this
The code is producing the result, but not in a way I want it to be produced

As I said in the first post, I wanted it to be on a new line or should I put more clearly, where the html element is using the <p> tag to put them on a new sentence/new line

1 Like

What do you want it to contain that it hasn’t already contained? Do you
want me to write up a whole 400 word thesis for you? If it doesn’t work,
it doesn’t work, what more do you want me to explain?

I have already given full working example, if you don’t somehow see that, well what can I say about you?

As I said in the first post

Quit with the attitude. To be clear, your first post made no sense. I tried to make sense of it and provide a solution to which you replied.

Doesn’t Work.

Your last response does shed a little more light though

$("p").text(data);

This code replaces the contents of any paragraph in the page with the response, perhaps you want append to add the content after the existing content as @m3g4p0p suggested.

$("p").append(data);

Or

$("p").append("<br><br>" + data);

My guess is that won’t work either though.

You’re welcome.

2 Likes