views:

38

answers:

4

Hi. 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?

A: 

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.

Ignacio Vazquez-Abrams
A: 

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); 
    } 
martani_net
I think for this but the code is too long to send with GET
Ilian
you can use POST the same way `xmlhttp.open("POST", "http://yoursite.com/data.php?source=" + text);`
martani_net
A: 

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.

kemp
Very good idea :) thanks!
Ilian
A: 

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/

Mech Software