1

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];

?>
4

4 に答える 4

4

PHP が取得できるように、パラメータに名前を付ける必要があります。呼び出しを次のように変更$.ajaxします。

data: { htmlString: htmlString },

次に、PHP で参照$_POST['htmlString']してパラメーターを取得できます。

于 2013-11-03T10:10:36.993 に答える
1

関数を修正します。

  function tweetUpdate(htmlString)
    {
    $.ajax({
        type: "POST",
        url: 'saveTweets.php',
        data: "htmlString="+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
        }
    });
    }

次に、saveTweets.php ページで行の下に書き込むと、そのページで値が取得されます。

echo '<pre>';print_r($_REQUEST );echo '</pre>';
于 2013-11-03T10:12:21.613 に答える