简体   繁体   English

Ajax成功函数中的动态PHP内容

[英]Dynamic PHP content inside ajax success function

I am trying to create dynamic php content on ajax success like the one below. 我正在尝试在ajax成功上创建动态php内容,如下所示。 I keep getting parse error. 我不断收到解析错误。 Not able to figure out the issue. 无法找出问题。 New to PHP n Ajax 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. 说明:您可以创建html并仅使用JS或JQuery在页面上显示它。 There is no requirement of PHP in that. 不需要PHP。

As an alternative you can do it with json_encode . 或者,您可以使用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);
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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