Send code from page to php script
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 ma开发者_如何学Pythonke this?
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.
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.
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);
}
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/
精彩评论