将html id(存储在js变量中)发送到另一个php文件

时间:2015-06-16 13:52:47

标签: javascript php sql ajax post

我正在尝试将数据从我的索引文件发送到save.php 这是我发送数据的代码

function ClickToSave() {
var tobesent = CKEDITOR.instances.textToBeSaved.getData();
var pagename = document.getElementById("pagename").innerHTML;
var pagepath = document.getElementById("pagepath").innerHTML;
$.ajax({
    url :"save.php", 
    type :"POST",
    data :{
        content : tobesent,
        pagepath : pagepath,
        pagename : pagename
        }
    }).done(function(){
        window.alert("changes saved");
    });
}
</script>

我也试过

function ClickToSave() {
        var pagename = document.getElementById("pagename").innerHTML;
        var pagepath = document.getElementById("pagepath").innerHTML;
        //var tobesent = CKEDITOR.instances.textToBeSaved.getData();
        var tobesent = document.getElementById("textToBeSaved").innerHTML;
        window.alert(tobesent);
        $.ajax({
            url : "save.php", 
            type : "POST",
            data :{
                content : tobesent,
                pagepath : pagepath,
                pagename : pagename
                }
            }).done(function(){
                window.alert("changes saved");
                check();
            });
        }

这是我的保存文件

<?php
  if (empty($_POST) === false){
$pagename = $_POST["pagename"];
$pagepath = $_POST["pagepath"];
$data = $_POST["content"];
$connect = new mysqli("localhost","root","","bakliwal");
$sql =  "UPDATE `bakliwal`.`pages` SET `pagecontent` = '$data' WHERE `pages`.`pagepath` = '$pagepath' AND `pages`.`pagename` = '$pagename'";
$result = $connect->query($sql);
if($result){
    $sql =  "UPDATE `bakliwal`.`pages` SET `editable`='0' WHERE  `pages`.`pagepath` = '$pagepath' AND `pages`.`pagename` = '$pagename'";
    $result = $connect->query($sql);
    }
    $sql =  "insert into `bakliwal`.`dummy` VALUES (NULL,'$data','$pagename','$pagepath')";
    $result = $connect->query($sql);

}
?&GT;

在第一种情况下,我收到了一个错误说法 &#34;未捕获的TypeError:无法读取属性&#39; getData&#39;未定义&#34; 在第二种情况下,我收到一个窗口警报更改已保存,但数据库中没有保存任何内容

我也使用了相同的代码(第一种情况下的代码)从另一个文件page.php发送数据到save.php,并且它在那里工作得很好

0 个答案:

没有答案