jQueryスクリプトをphpで裏で実行しようとしています。基本的には、jQuery を使用して div のコンテンツを取得し (動作)、ajax を使用してスクリプトを呼び出します (動作します) が、php を呼び出して変数を php に送信する ajax スクリプトが必要なので、コンテンツを保存できます。
コードは次のとおりです。
<script>
$( document ).ready(function() {
$( ".tweets" ).click(function() {
var htmlString = $( this ).html();
tweetUpdate(htmlString);
});
});
</script>
<script>
function tweetUpdate(htmlString)
{
$.ajax({
type: "POST",
url: 'saveTweets.php',
data: htmlString,
success: function (data) {
// this is executed when ajax call finished well
alert('content of the executed page: ' + data);
},
error: function (xhr, status, error) {
// executed if something went wrong during call
if (xhr.status > 0) alert('got error: ' + status); // status 0 - when load is interrupted
}
});
}
</script>
そしてsaveTweets.phpの私のコード
<?
// SUPPOSED TO receive html conents called htmlString taken from a div
// and then I will write this code to a file with php and save it.
echo $_POST[htmlString];
?>