开发者

Send code from page to php script

开发者 https://www.devze.com 2022-12-20 09:11 出处:网络
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:

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/

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号