Php 无法显示$this->;验证->;错误字符串
您好这是我的onload函数:-Php 无法显示$this->;验证->;错误字符串,php,javascript,codeigniter,Php,Javascript,Codeigniter,您好这是我的onload函数:- $(function() { <?php if( isset ($this->validation->error_string) && $this->validation->error_string != '') {?> str = '<?php echo $this->validation->error_string;?>';
$(function() {
<?php if( isset ($this->validation->error_string) && $this->validation->error_string != '') {?>
str = '<?php echo $this->validation->error_string;?>';
alert(str);
<?php }?>
});
$(函数(){
str='';
警报(str);
});
我总是会遇到这样的错误:-
unterminated string literal
str = '<p>The Username field is required.</p>\n
未终止的字符串文字
str='用户名字段是必需的。\n
即使有验证错误,我总是在firebug中得到这个错误。然而,当我只是在body标签中重复它时,它工作得很好。我如何解决这个问题
提前感谢:)您需要在输出中对新行进行编码,以使其成为有效的JavaScript,例如使用如下方式:
$(function() {
<?php if( isset ($this->validation->error_string) && $this->validation->error_string != '') {?>
var str = '<?php echo json_encode($this->validation->error_string);?>';
alert(str);
<?php }?>
});
$(function() {
<?php if( isset ($this->validation->error_string) && $this->validation->error_string != '') {?>
alert('<?php echo json_encode($this->validation->error_string);?>');
<?php }?>
});
另一个注意事项是,由于您正在对此发出警报,您可能需要删除
包装器,因为它也将显示在警报()
中。您需要在输出中编码新行,以便它将是有效的JavaScript,例如,使用如下所示:
$(function() {
<?php if( isset ($this->validation->error_string) && $this->validation->error_string != '') {?>
var str = '<?php echo json_encode($this->validation->error_string);?>';
alert(str);
<?php }?>
});
$(function() {
<?php if( isset ($this->validation->error_string) && $this->validation->error_string != '') {?>
alert('<?php echo json_encode($this->validation->error_string);?>');
<?php }?>
});
另一个注意事项是,由于您正在对此发出警报,您可能需要删除
包装器,因为它也会显示在警报()
中