dongmiyu8979 2013-09-03 10:46
浏览 139
已采纳

如何使用$ _POST来保存div是contentEditable =“true”?

I'm trying to store the editable content of a Div in an SQL database. However from what I can tell isset($_POST["des"]) never returns true.

(I haven't done much website coding so I am probably missing something quite fundamental/trivial)

overview_page.php:

<?php session_start();
include_once("php_includes/check_login_status.php");
?>

<?php
    if(isset($_POST["des"]))
    {
    echo "Inside IF statement";
    include_once("php_includes/db_conx.php");
    $des = mysqli_real_escape_string($db_conx, $_POST['des']);    
    $sql = "UPDATE users SET project_description='$des' WHERE   username='$log_username'";
    }
?>

<!DOCTYPE html>
<html>
<head>

    <script src="js/main.js"></script> <!--Points to external java script file-->
    <script src="js/ajax.js"></script> <!--Points to external java script file-->

    <script type="text/javascript">

    function save_description()
    {
        var des = _("project_description").value;
        var ajax = ajaxObj("POST", "overview_page.php"); 
        //ajax.onreadystatechange = function() sorry this one shouldn't be here
        ajax.send("des="+des); 
    }

    </script>
</head>    
<body>    

  <?php include_once("template_pagetop.php"); ?>  

  <div id="pageMiddle">
    <div id="left_window">

            <div id="project_description" name="project_description" contentEditable="true">
            Please enter project description...
            </div>
            <center>
            <button id="save" onclick="save_description()">Save</button>
            </center>
    </div>
  </div>      
</body>    
</html>

and external scripts:

ajax.js:

function ajaxObj( meth, url ) {
var x = new XMLHttpRequest();
x.open( meth, url, true );
x.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
return x;
}
function ajaxReturn(x){
if(x.readyState == 4 && x.status == 200){
   return true; 
}}

main.js:

function _(x)
{
return document.getElementById(x);
}
  • 写回答

2条回答 默认 最新

  • douchen2595 2013-09-03 10:59
    关注

    So I took your code over to jsFiddle and created a new fiddle and the first thing that came up in the console (F12 in your browser - at least in Chrome) was that the event wasn't actually firing.

    So I moved your save_description() method into an event bind (there are other approaches to this):

    document.getElementById('save').onclick = function () {
        var des = _("project_description").innerHTML;
        var ajax = ajaxObj("POST", "overview_page.php");
        //ajax.onreadystatechange = function() sorry this one shouldn't be here
        ajax.send("des=" + des);
    }
    

    Also note I changed the des = _("project_description") access to innerHTML.

    This then seems to at least make the request with a value. After that, check your server-side script.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?