Tutorial :Send code from page to php script



Question:

I have a html page with javascript which change the content of page and I want to send the new content to php script? How can I make this?


Solution:1

Or you can just use a form:

<form action="php_page.php" method="POST">  <input type="hidden" id="container" name="container" value="">  

fill #container value with javascript and submit.


Solution:2

AJAX will let you send a request to a server in the middle of viewing the page. Just use jQuery or the like to get the page contents and POST it to the server.


Solution:3

Supposing you have a page on the server named data.php that expects the HTML content in the (GET) variable source for example http://yoursite.com/data.php?source=THE_HTML_HERE

you can use XHR like follows

function postIt() {           var text = document.body.innerHTML;          xmlhttp.open("GET", "http://yoursite.com/data.php?source=" + text);           xmlhttp.onreadystatechange = function() {               if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {                   //handle the response from the page here              }           }           xmlhttp.send(null);       }   


Solution:4

jQuery is a really great way to go since it handles cross browser compatibility.

$.post('ajax/test.html', function(data) {    $('.result').html(data);  });  

Is a sample post call. Make sure you keep in mind the difference between POST and GET. GET can be used to post data to your server as well to update or get new information but POST can be used to send larger amounts of data.

http://api.jquery.com/jQuery.post/


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »