我在每个更新时间都在文本区域有空格

时间:2011-04-24 08:27:27

标签: php html codeigniter textarea

<textarea name="email_message" id="email_message" cols="65" rows="15"><?php echo trim($entry['message']);?></textarea>

当我使用上面的html代码进行更新时,它会在消息中包含两次空白行。

例如:

Dear Sir
(here is one now now)
How are you?

当它更新到数据库时,它保存如下:

Dear Sir
(here is two lines)

How are you?

每增加一次保存就会增加空行。

PHP代码在这里:

function email_template_save()
    {
        $content = $this->input->post('email_message');
        $this->load->model('templates');
        $data['msg']=$this->templates->update_email_template($content,$this->session->userdata('username'));
        $data['main_content']='message';
        $this->load->view('template',$data);

    }


function update_email_template($content,$username)
    {
        $data=array(
            'message'=>$this->input->post('email_message'));
            $this->db->where('username', $username);
            $this->db->update('email_format', $data); 
            return 'Successfully Updated';


    }

3 个答案:

答案 0 :(得分:2)

我知道这一切,这是CI和Windows换行的最新版本的问题。

请参阅此答案:PHP textarea saving extra new lines to MySQL DB

这是我正在使用的变通方法,输入类的扩展名:

<?php defined('BASEPATH') OR exit('No direct script access.');

/**
 * CI 2.0 (Reactor version) did something funny to newlines.
 * Textareas would $_POST an extra newline, this is a patch for that "feature".
 * 
 * @package     Codeigniter
 * @subpackage  Input
 */
class MY_Input extends CI_Input {

    public function __construct()
    {
        if (PHP_EOL == "\r\n")
        {
            $this->_standardize_newlines = FALSE;
        }
        parent::__construct();
    }
}
/* end file */

这也与您的其他问题有关:explode error \r\n and \n in windows and linux server

请点击此处查看相关说明:When do I use the PHP constant "PHP_EOL"?

答案 1 :(得分:0)

是否有可能将空白表示为\r\n,当您打印它们时,\r\n分开?尝试运行<textarea name="email_message" id="email_message" cols="65" rows="15"><?php echo trim(str_replace("\r\n","\n",$entry['message']));?></textarea>

答案 2 :(得分:0)

你有可能两次逃避这些值,我最近遇到了同样的问题,检查你是否在获取帖子和保存数据时都逃避了这个值。