简体   繁体   中英

Encoded HTML string incorrectly decoded in CodeIgniter

I post data containing an html string as well as some other data from JavaScript to the PHP (Codeigniter) server w/ AJAX & JSON.

The HTML string contains an image tag w/ some style data. On the server, the style info is gone! The rest of the data I post is just fine.

What should I do to the HTML string to prevent this?

JavaScript side:

my_html='<p><img alt="" src="/ckfinder/userfiles/images/ferns.jpg" style="width: 200px; height: 133px;" /></p>';
my_count=1;

This is the data that gets posted in the AJAX call:

form_data='my_html='+encodeURIComponent(my_html)+'&my_count='+my_count;

On the server:

$html_str=$this->input->post('my_html');
$this->chromephp->log($html_str);

And this is what it prints:

<p><img alt="" src="/ckfinder/userfiles/images/ferns.jpg"  /></p>

EDIT - 1/24/18 After more investigation, it seems to be a decoding issue or the way I'm reading the value in Codeigniter (v 2). Nothing to do with the fact it's an image tag.

Here's what I've figured out -

Javascript:

var hstr ='<p style="color:red;">Hello</p>';

After encodeURIComponent(), encoded string submitted as form data:

%3Cp%20style%3D%22color%3Ared%3B%22%3EHello%3C%2Fp%3E

But, in the server:

The (URIencoded string being submitted as form data over AJAX: $html_str=$this->input->post('hstr'); $this->chromephp->log($html_str);

yields:

<p >Hello</p>

On the server if I urldecode() the encoded string directly,

$xx=urldecode('%3Cp%20style%3D%22color%3Ared%3B%22%3EHello%3C%2Fp%3E');
$this->chromephp->log($xx);

yields:

<p style="color:red;">Hello</p>

So is it that Codeigniter (v2) decodes it when I use input->post()? Is there a way to get the raw undecoded string?

Any suggestions?

Help! Please!

Mmiz

尝试

form_data='my_html='+encodeURI(my_html)+'&my_count='+my_count;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM