0

I have a string, get by Javascript (the content of a div!);
I want to write this string to PHP, because i want a text file with this content!

How is the best way to do this?




Edit[1]: Method Post doesn't works, because i have '<' chars and it erase them (i don't know why)... Example: I have #include <stdio.h> and when i call echo $_REQUEST just show #include ...

2
  • Please, Be more specific Commented Aug 11, 2010 at 21:12
  • It's not showing the '<' chars because your browser thinks they're tags. Try using echo htmlspecialchars($_REQUEST); instead, or use header('Content-type: text/plain'); before you echo anything. Then you will see that the '<' chars are actually there. If you try writing to a file (without the htmlspecialchars), it will work. Commented Aug 12, 2010 at 2:50

3 Answers 3

1

For the Javascript part, I suggest using jQuery.ajax. It could be triggered, for example, when a button is clicked.

$.ajax({ 
  type: "POST",
  url: "process_ajax_div.php",
  data: { divHTML: $('#yourdiv').html() }, 
  success: function(){ alert('done!'); },
  error: function(){ alert('error!'); }
});

For the PHP part (file process_ajax_div.php) you can do something like this:

<?php
   $divHTML = $_POST['divHTML'];

   // now do whatever you want with $divHTML
?>
Sign up to request clarification or add additional context in comments.

Comments

0

POST the string to the server, the simplest way is to add the value to a form on the page and submit it, but you could submit the value via AJAX if you like... here is the simple example.

<div id="mydiv">Hello World</div>

<form method="post" action="myphppage.php" id="myform">
    <textarea name="mytext" id="mytext"></textarea><br>
    <input type="submit" value="Create File">
</form>

<script type="text/javascript">
    document.getElementById("mytext").value = document.getElementById("mydiv").innerHTML();
    document.getElementById("myform").submit();
</script>

Comments

0

You could pass the string text via url to the .php file that will process it.

There are many ways to accomplish that, one, very simple, could be:

var text = "some";
var url = "http://myserver/webapp/file.php?content=" + text;
location.href = url;

or with form or with Ajax and so on...

1 Comment

Don't forget to URL escape the data.

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.