语法错误。无法在php文件中嵌入html代码和javascript操作

时间:2018-04-20 15:15:32

标签: javascript php laravel datatables

我正在尝试使用ajax加载dataTables。我目前正在使用laravel框架。我的代码就像:

    if($posts){
            foreach($posts as $r){
                $nestedData['name'] = $r->name;
                $nestedData['email'] = $r->email;
                $nestedData['created_at'] = date('d-m-Y H:i:s',strtotime($r->created_at));
                $nestedData['action'] = '
                <a href="'.url('/admin/user').'/'.$r->id.'/edit" class="btn btn-sm btn-info"><i class="fa fa-pencil" aria-hidden="true"></i></a>
                &nbsp;&nbsp;
                <form method="POST" action="'.url('/admin/user').'/'.$r->id.'" accept-charset="UTF-8" id="frm_'.$r->id.'" class="form form-delete-btn">
                <input name="_method" type="hidden" value="DELETE">
                <input name="_token" type="hidden" value="'.csrf_token().'">
                <button type="button" class="btn btn-sm btn-danger" data-toggle="modal" data-target="#confirm-delete'.$r->id.'"><i class="fa fa-trash" aria-hidden="true"></i></button>
                </form>


                <div class="modal fade confirm-delete" id="confirm-delete'.$r->id.'" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
                    <div class="modal-dialog modal-dialog-centered" role="document">
                        <div class="modal-content">
                            <div class="modal-header">
                            <h5 class="modal-title" id="exampleModalLongTitle"><strong>Confirm Delete</strong></h5>
                            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                            <span aria-hidden="true">&times;</span>
                            </button>
                            </div>
                            <div class="modal-body">
                            <p>You are about to delete one client <strong>"'.$r->name.'"</strong>, this procedure is irreversible.</p>
                            <p>Do you want to proceed?</p>
                            <p class="debug-url"></p>
                            </div>
                            <div class="modal-footer">
                            <button type="button" class="btn btn-warning" data-dismiss="modal">Cancel</button>
                            <a onclick="submit_form("frm_'.$r->id.'")" class="btn btn-primary btn-ok">Delete</a>
                            </div>
                        </div>
                    </div>
                </div>
                ';
    $data[] = $nestedData;
    }
}

但是,我在线上遇到错误

<a onclick="submit_form("frm_'.$r->id.'")" class="btn btn-primary btn-ok">Delete</a>

这是说:

  

未捕获的SyntaxError:意外的输入结束

submit_form(之后指出错误。

任何建议都会受到赞赏,因为我对编程和stackoverflow都很陌生。

1 个答案:

答案 0 :(得分:1)

<a onclick="submit_form("frm_'.$r->id.'")" class="btn btn-primary btn-ok">Delete</a>

当PHP服务器解释时会产生:

<a onclick="submit_form("frm_<ID>")" class="btn btn-primary btn-ok">Delete</a>

有嵌套引号导致Unexpected end of input

可能的解决方法是:

<a onclick="submit_form(\'frm_'.$r->id.'\')" class="btn btn-primary btn-ok">Delete</a>

将成为:

<a onclick="submit_form('frm_<ID>')" class="btn btn-primary btn-ok">Delete</a>