简体   繁体   中英

Dynamic PHP content inside ajax success function

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);
    }
});

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.

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);
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM