-3

I could see that this question was being asked a lot of times and I did extensive research on what methods could be used to transfer a couple of Javascript variables to a PHP script.

  1. Post data in a form as hidden values
  2. Post to URL, i.e. '/txt.php?h=' + var
  3. Use a cookie

I'm trying write a piece of code that will let you download what you've written in Wrrrite.com - I'm the developer of this website. (I also did a client-side approach of putting the variables into the URI and setting a header to download stuff)

None of this is working. Either there's a character that's causing an error, or there are html elements.

Is there something I can code to guarantee a 1:1 transfer of the work/variables?

Datatype: HTML + various different Characters "!§!' etc. Output: .txt File, perferably a 1:1 translation of what was being written on the form

9
  • 3
    What kind of data are you trying to transfer? If it's HTML, posting data in a form as hidden values should work. (So should an Ajax POST request.) Commented Feb 20, 2012 at 10:56
  • What does PHP has to do with this? Wrrrite appears to be written entirely in JavaScript and stores data using Local Storage (which makes "downloading" an impossibility, since the data is already local). Commented Feb 20, 2012 at 10:57
  • What code do you have? What "isn't working"? What errors do you get? Commented Feb 20, 2012 at 11:01
  • It is getting down voted because it can't be sensibly answered in its current form. What code have you got? Are you writing PHP? Are you writing JS? Both? How are you executing them? What errors do you get? What does "not working" mean? Commented Feb 20, 2012 at 11:30
  • @Quentin I'm writing both. I can execute them both, also. Not working means that random characters (since everything is allowed, including " ' [ ] ) ) will break the script. Commented Feb 20, 2012 at 14:14

3 Answers 3

1

You should maybe use a Base64 encoding of your data, before sending it. Have a look at MDN for the JavaScript part and here for the PHP decoding. This should prevent special characters from breaking your code.

6
  • 1
    Base64 inflates the data volume by 33% and shouldn't be necessary when POST is being used. Commented Feb 20, 2012 at 11:06
  • He states that his problems are special characters and html elements within the data. Both can be circumvented by Base64. Commented Feb 20, 2012 at 11:10
  • It shouldn't be necessary when the content is only text, which it appears to be from a glance at the site in the question. encodeUriComponent is a more sensible approach. Commented Feb 20, 2012 at 11:10
  • @Sirko sure they can, but I can't think of a situation where it's necessary when POST is used. Commented Feb 20, 2012 at 11:11
  • How would I dynamically update the value of what is being submitted, since the user will change the text constantly? Commented Feb 20, 2012 at 11:23
0

Hope it helps

//PHP

 if(isset($_REQUST['submit']))
{
  $download=$_REQUEST['download'];
// from database get value of all the downloadable items
// and check if the input is in that array then to the suitable thing. 
}
//HTML
<form method="post" action=''>
Type :<input type="text" id="txtField" name="txtField" />
<input type="hidden" name='download' />
<input type="submit" name="submit" onclick="return onSubmit() " />
</form>

//Javascript

<script type="text/javascript">
function onSubmit()
{
if(document.getElementById("txtField").value != '')
{
  document.getElementById("download").value=document.getElementById("txtField").value;
return true;
}
else
{
 alert("Please enter item to download");
return false;
}
}
0

Here's the Cookie Approach, which doesn't work http://pastebin.com/SKNtxLi5

That just slaps values together without any consideration for the data format used in cookies.

Quirks Mode has a decent guide to cookies with JS if you want to fix that.

However… the point of cookies is that the data persists. It isn't a sensible transform for one shot messages.

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.