0

I am trying to create dynamic php content on ajax success like the one below. I keep getting parse error. Not able to figure out the issue. New to PHP n Ajax

$.ajax({
    type: "GET",
    url: "edit_listing.php",
    data: {value:val},
    dataType: "JSON",
    success: function(data) { 
        txt="<php echo '<div></div>';"+
            "echo '<p></p>'; ?>";
        $("#someid").append(txt);
    }
});

2 Answers 2

1

Try this:

txt = '<div></div> <p></p>';
$("#someid").append(txt);

Explanation: You can create html and show it on page by using JS or JQuery only. There is no requirement of PHP in that.

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

5 Comments

I want even to use some if conditions n more, this was just an example. I am building something which uses session variables for more dynamic content.
Return the value of session variable from ajax call and use in JS
I thinks thats better idea. Have done that before. Just wondering is there a way to use some thing like this code directly in ajax o/p "<?php if(isset($_SESSION['pictures']) && !empty($_SESSION['pictures'])) { echo '<div class="w3-content1" style="width:300px;height:250px;position:relative;overflow:hidden">'; for($i=0;$i<count($_SESSION['pictures']);$i++){ $value=$_SESSION['pictures'][$i]['picture_link']; if($i==0){ echo "<img src=$value class='mySlides' alt='Image' style='width:100%;height:100%;'>"; } }echo '</div>'; }?>
There's not. Stick to the way you've done it before.
Thanks you all. If thats best way. I will stick to that.
0

As an alternative you can do it with json_encode. For example:

// php code
<?php $txt = "<div>{$somevar}</div><p>{$anothervar}</p>"; ?>

$.ajax({
    type: "GET",
    url: "edit_listing.php",
    data: {value:val},
    dataType: "JSON",
    success: function(data) {
        // php code 
        txt=<?php echo json_encode($txt); >;
        $("#someid").append(txt);
    }
});

Comments

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.