tags:

views:

307

answers:

4

I have a wysiwyg editor that is changing my ampersand characters from & to &

What happens when the data is submitted is that everything after the & is dropped and everything before is left intact. I asked this question a while back but couldn't seem to get an answer. We thought that maybe it was the editor but I verified that the ampersand is being converted after submission. Is there a php function that will escape these ascii characters?

A: 

are you able to print_r() the raw contents as they are submitted to verify that PHP is getting the whole string?

If your editor is chopping it off at the ampersand then it's a problem before PHP is involved.

Evernoob
Hi Everknob. Yes, exactly..
+1  A: 

It sounds like it's not being urlencoded properly. Are you building the request yourself (using AJAX for example)?

Depending on the AJAX library you're using (if any) you may need to manually escape your data:

Imagine your editor contains "Beans&sausages". You post this in the variable "text":

text=Beans&sausages

This looks to PHP like

text = Beans
sausages = null

In Javascript use encodeURIComponent() to fix this.

Greg
Hi Greg. I'm using AJAX to submit the data to PHP
Actually, I'm not using urlencode for this.
This is the actual variable encapsulated in your function.var encodeURIComponent(rt) = $("textarea#rt").val();
this sounds right. what you are describing is javascript not url-encoding your data. you should check the function sending the data and maybe exend it to make sure it does url-encode all data. this is almost 100% a client side problem
XiroX
@Jim: var rt = encodeURIComponent($("textarea#rt").val())
Greg
Thanks Greg. brb.. I'm going to try this. Thank you!!
Greg, that worked. Thank you! Let me ask you a question though. gnarf (below) suggested that a block of code automatically would do this type of encoding for me. Is there a better way to handle this or am I good with this solution?
If you're using jQuery then the AJAX module should handle the encoding for you ad @gnarf says - not sure what you're doing that it doesn't
Greg
Ok, listen.. thank a lot for all the help!
A: 

I think the problem here is somewhere else, since what the editor does is absolutely correct. I would suggest checking the value received by PHP (the one in the $_REQUEST array), if that has missing characters, check the POST or GET request that is sent on the wire (i.e. use firebug or similar). I would think that the value is mangled before it is sent.

soulmerge
`var_export($_POST);` and see what you're submitting to the server. soulmerge is probably right - you're issue is at the front-end, well before PHP gets involved. Custom AJAX solution, or are you using a library?
searlea
Have you verified it using `var_dump($_REQUEST)` and firebug? Please do that, you are trying to debug based on assumptions.
soulmerge
Ok, will do Soulmerge. brb
Please attach the posted value to your question. Use firebug to find that POST data: http://getfirebug.com/net.html
soulmerge
Hi Soulmerge. Greg got this solved. Thank you. What it needed was encodeURIComponent()
A: 

Perhaps somewhere the function htmlentities is or isn't being called on your text? You can use html_entity_decode to decode it back. Having read most of the responses and your question again though, I would look at the code submitting the AJAX, you most likely need to urlencode the string in javascript before passing it as post data. Most libraries will handle this for you. The function encodeURIComponent should be called on the data if you are assembling it manually.

var postData = encodeURIComponent(key) + '=' + encodeURIComponent(value).replace(/%20/g,'+');

I noticed in one of your comments you were using a jQuery function. jQuery has a helper function called $.param() that will encode a postable string for you, or even easier is just to pass an object to the data parameter of the ajax call (internally it will be passed through $.param()

$.ajax({
  method:'POST',
  url: '/somefile.php',
  data: {
    rt: $("textarea#rt").val(),
    Type: 1,
    //.... more data here
  },
  success: function(data) {
  }
});
gnarf
Hi Gnarf, I am already using a block of code like that one you posted. Are you saying that this block of code provides me with the encoding I need? If so, then why is the string being torn apart?
gnarf
Hi Gnarf, Greg above got this working using the same encodeURIComponent() function you suggested. Please have a look at where I placed that function and please tell me if I should use it in the block of code that you have posted above.
You shouldn't need to even know the function encodeURIComponent if you used the object parameter to data on a jquery ajax call. Please post the whole section of code you are using to create and perform your ajax call. Starting a new question about it is probably a better idea, this one already has nothing to do with its title anymore.
gnarf