0

I'm sending comma separated values through a URL (key, value). I'm encoding them with Javascript's escape() and then replacing the commas within each value with %2c . The problem is at the PHP end the commas that are encoded are turned into "," BEFORE explode() takes place and then my string containing commas is broken up and it doesn't save right.

How can I stop PHP from converting my encoded bits back into unencoded bits?

My JS for each input is:

fieldData += $(this).attr("id")+","+escape($(this).html()).replace(/,/g,"%2c")+",";

My PHP is:

$fieldData = explode(",", $_POST['fieldData']);

Tried (along with other things):

$fieldData = explode(",", urlencode($_POST['fieldData']));
3
  • 1
    Please post what the un-modified $_POST['fieldData'] looks like. Commented Jan 30, 2013 at 16:43
  • url(EN)code should be your first hint. you're converting the commas back to %2c, which is NOT what you're exploding on... Commented Jan 30, 2013 at 16:53
  • @Sammitch It would look something like this "data=name,sammitch,age,20,bio,helpful%20guy%20,on%20stackoverflow" The problem is that comma in the last bit ^ Commented Jan 30, 2013 at 21:01

2 Answers 2

1

I would suggest using base64encode/decode for this.

The javascript would look something like this: http://jsfiddle.net/Y6yuN/

<script src='http://javascriptbase64.googlecode.com/svn/trunk/base64.js'></script>
fieldData += $(this).attr("id")+","+escape(Base64.encode($(this).html()))+",";

The escape is for the trailing =

So you would end up with comma delimited base64 encoded strings.

On the PHP side:

$fieldData = explode(",", $_POST['fieldData']);
foreach ($fieldData as $k => $v){
  $fieldData[$k] = base64_decode(urldecode($v));
}
Sign up to request clarification or add additional context in comments.

2 Comments

I've resolved that issue. But the one issue remains is that the longest value, the one with the comma, now returns empty after decoding! I did this: foreach ($fieldData as $k => $v){ echo $fieldData[$k]; $fieldData[$k] = base64_decode(urldecode($v)); echo $fieldData[$k]; } The second time it comes out blank! I believe it has to do with the = at the end. urldecode doesn't seem to be converting it back and so base64_decode is just returning false.
In the end I had to escape BEFORE encoding in the JS: Base64.encode(escape($(this).html())) Thank you for your help!
0

Your post is not really well explained, but I think you want to decode the data passed by JS. So, the code should be:

$fieldData = explode(",", urldecode($_POST['fieldData']));

Try to write it better if I am wrong!

2 Comments

This process wasn't the problem. Sorry if it isn't explained well - apparently the title was auto-corrected. Matthew Scragg has the right idea if you can see his answer
Actually apparently someone else edited my title which probably didn't help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.