Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/462.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 无法显示$this->;验证->;错误字符串_Php_Javascript_Codeigniter - Fatal编程技术网

Php 无法显示$this->;验证->;错误字符串

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;?>';

您好这是我的onload函数:-

  $(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 }?>
});
另一个注意事项是,由于您正在对此发出警报,您可能需要删除

包装器,因为它也会显示在
警报()