doushou8299 2013-01-28 14:47
浏览 113
已采纳

用PHP代码插入html字符串

Well, I need to insert a HTML string with php code inside in a form.

I have one form to create new users. Inside of it, I need to insert a role for this user, so I have some roles in my database to apply to him.

I have a html select field to do that, and a button to add a new rol. When I click in the button, I want to add a new select field to my form. But inside this code I have PHP code to show all roles. I try this way using jQuery:

$(document).ready(function () {
    $('#add-role').click(function (event) {
        var newRoleField = "<label for=\"usuario_perm_id\">ROLE <span class=\"required-field\"> * </span>:</label>" +
            "<select id=\"usuario_perm_id\" name=\"usuario_perm_id\" tabindex=\"7\">" +
            "<option <?php echo ($this->usuario[\"rol_id\"] == \"none\" ? 'selected=\"selected\"' : null); ?> value=\"none\">-- Seleccione un rol --</option>" +
            "<?php foreach ($this->roles as $rol): ?>" +
            "<option value=\"<?php echo $rol[\"rol_id\"]; ?>\" <?php echo ($this->usuario[\"rol_id\"] == $rol[\"rol_id\"] ? 'selected=\"selected\"' : null); ?>><?php echo $rol[\"nombre\"]; ?></option>" +
            "<?php endforeach; ?>" +
            "</select><br />";

        $('#new-roles').append(newRoleField);

        event.stopPropagation();
        event.preventDefault;
    });
});

And I get a new select field but inside it, the text:

usuario["rol_id"] == "none" ? 'selected="selected"' : null); ?> value="none">-- Select role --

  • 写回答

3条回答 默认 最新

  • dongshu9458 2013-01-28 14:51
    关注

    You can't. JavaScript (and thus also jQuery) runs client side and PHP runs server side. The browser has no idea how to handle any PHP you might insert.

    A way to handle this, is to have jQuery query the server for the roles and have your server create a (PHP generated) list of roles. Check out AJAX.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料