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
2010-02-11 17:43:26
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
2010-02-11 17:45:16
I think for this but the code is too long to send with GET
Ilian
2010-02-11 17:53:09
you can use POST the same way `xmlhttp.open("POST", "http://yoursite.com/data.php?source=" + text);`
martani_net
2010-02-12 11:20:04
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
2010-02-11 17:48:32
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.
Mech Software
2010-02-11 17:49:46