Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jQuery/Ajax获取html并使用php保存_Php_Jquery_Function - Fatal编程技术网

jQuery/Ajax获取html并使用php保存

jQuery/Ajax获取html并使用php保存,php,jquery,function,Php,Jquery,Function,我正在尝试使用php在后台运行jQuery脚本。它基本上会用jQuery(works)获取div的内容,然后用ajax(works)调用脚本,但我需要调用php的ajax脚本将变量发送到php,这样我就可以保存内容了 代码如下: <script> $( document ).ready(function() { $( ".tweets" ).click(function() { var htmlString = $( this ).html(); tweetUpdate(htmlS

我正在尝试使用php在后台运行jQuery脚本。它基本上会用jQuery(works)获取div的内容,然后用ajax(works)调用脚本,但我需要调用php的ajax脚本将变量发送到php,这样我就可以保存内容了

代码如下:

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

$(文档).ready(函数(){
$(“.tweets”)。单击(函数(){
var htmlString=$(this.html();
推特更新(htmlString);
});
});
函数tweetUpdate(htmlString)
{
$.ajax({
类型:“POST”,
url:'saveTweets.php',
数据:htmlString,
成功:功能(数据){
//这是在ajax调用顺利完成时执行的
警报('已执行页面的内容:'+数据);
},
错误:函数(xhr、状态、错误){
//在通话过程中出现问题时执行
如果(xhr.status>0)警报('got error:'+status);//状态0-加载中断时
}
});
}
还有我的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];

?>

您必须为参数指定一个名称,以便PHP能够检索它。将
$.ajax
调用更改为:

data: { htmlString: htmlString },

然后在PHP中,您可以引用
$\u POST['htmlString']
来获取参数。

您必须为参数指定一个名称,以便PHP可以检索它。将
$.ajax
调用更改为:

data: { htmlString: htmlString },
然后在PHP中,可以引用
$\u POST['htmlString']
来获取参数。

更正函数

  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>';
echo';打印请求($);回声';
更正您的函数

  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>';
echo';打印请求($);回声';

您可以使用
$.post
方法发布到PHP页面,然后在回调函数中从该页面检索结果。

您可以使用
$.post
方法发布到PHP页面,然后在回调函数中从该页面检索结果。

使用json更适合发送数据:

echo $_POST['htmlString'];
现在使用php,您只需执行以下操作:


使用json更适合发送数据:

echo $_POST['htmlString'];
现在使用php,您只需执行以下操作:


令人惊叹的!成功了。谢谢你,巴尔马。如果可以,我将在10分钟内标记正确。:)令人惊叹的!成功了。谢谢你,巴尔马。如果可以,我将在10分钟内标记正确。:)