Php 使用jqueryajax更新mysql

Php 使用jqueryajax更新mysql,php,jquery,mysql,ajax,Php,Jquery,Mysql,Ajax,我试图用jquery和ajax从textarea更新mysql数据库,但无法将文本区域的更新内容传递到php页面 我有一个页面,在页面上单击一个按钮,打开一个层并调用编辑框功能 然后,该层用数据库中的数据填充表单字段 textarea是一个nicedit框,在我单击save之前,所有的功能都很好,并且我对textarea所做的任何更改都不会被传递。只有原始内容 这似乎很明显,但我似乎无法理解这个问题 谢谢你的帮助 这是密码 html页面 <!DOCTYPE html PUBLIC "-//

我试图用jquery和ajax从textarea更新mysql数据库,但无法将文本区域的更新内容传递到php页面

我有一个页面,在页面上单击一个按钮,打开一个层并调用编辑框功能

然后,该层用数据库中的数据填充表单字段

textarea是一个nicedit框,在我单击save之前,所有的功能都很好,并且我对textarea所做的任何更改都不会被传递。只有原始内容

这似乎很明显,但我似乎无法理解这个问题

谢谢你的帮助

这是密码

html页面

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style type="text/css">
  #popupbox{ 
    padding:0;
    width: 99%;
    height: auto;
    margin: 0px auto;   
    position:absolute;
    background: #FBFBF0; 
    border: solid #000000 2px; 
    z-index: 9000; 
    font-family: arial; 
    visibility: hidden; 
   }
 </style>
 <script type="text/javascript" src="http://js.nicedit.com/nicEdit-latest.js"></script>
 <script>

 function get_edit_content(box_id,page_ref,template_ref)
   {
  $.get("get_content.php", { box_id: box_id, page_ref: page_ref, template_ref:template_ref } )
  .done(function(data) {
      document.getElementById("box_id").value = box_id; 
      document.getElementById("edit_content").value=data;
      var area1 = new nicEditor({fullPanel : true}).panelInstance("edit_content",{hasPanel : true});         
      });  
   }


   function edit_box(showhide,box_id,page_ref,template_ref){
     if(showhide == "show"){    
    get_edit_content(box_id,page_ref,template_ref);
            document.getElementById('popupbox').style.visibility="visible";
     }else if(showhide == "hide"){
            document.getElementById('popupbox').style.visibility="hidden";      
     }  
   }

  $(document).ready(function()
{    
      $('#sub').click(function (e) 
    { 
      e.preventDefault(); 
      $.ajax({type:'POST', url: 'update_textarea.php', data:$('#myFrm').serialize(), success:
      function(response) 
        {
         alert(response);
        }
     });

   });
});
</script>
</head>

<body>
 <div id="popupbox">
  <form id="myFrm">
    <input type="hidden" name="page_ref" value="<? echo $page_ref; ?>" />
    <input type="hidden" name="template_ref" value="<? echo $template_ref; ?>" />
    <input type="hidden" id="box_id" name="box_id"/>
    <textarea name="edit_content" id="edit_content"  style="width: 500px; height:500px;"></textarea>
    <button id="sub" >Save</button>
    <center><a href="javascript:edit_box('hide');">close</a></center> 
  </form>

 </div>
</body>
</html>
php页面

 <?
    include("connect.php"); 
$page_ref = $_POST['page_ref'];  
$template_ref = $_POST['template_ref'];
$box_id = $_POST['box_id'];
$edit_content = addslashes($_POST['edit_content']);  
if(mysql_query("UPDATE site_content SET content='$edit_content' WHERE page_ref='$page_ref' AND template_ref='$template_ref' AND box_id='$box_id' AND box_type='text'"))
    {
        echo "page_ref=".$page_ref." template_ref=".$template_ref." box_id=".$box_id." edit_content=".$edit_content;
    }
else
    {
        echo "error";
    }


   ?>

乍一看一切正常,但试试这个:

$query = "UPDATE site_content SET content='$edit_content' WHERE page_ref='$page_ref' AND
template_ref='$template_ref' AND box_id='$box_id' AND box_type='text'";
echo "Query = $query <br />";
mysql_query($query);

这将输出您试图运行的查询,可能有助于调试。请尝试此操作,并将回音结果发布到此处。

确定通过此小片段解决了问题

$(document).ready(function() {
 $('#sub').click(function() {
   var edit_content = $('#myFrm').find('.nicEdit-main').text(); 
   var box_id = $('#myFrm').find("#box_id").val();
   var page_ref = $('#myFrm').find("#page_ref").val();
   var template_ref = $('#myFrm').find("#template_ref").val();
   $.post("update_textarea.php", {box_id:box_id, page_ref:page_ref, template_ref:template_ref, edit_content:edit_content },
  function(status){
   alert(status);       
   });
 });
});

我将“保存”按钮放在表单外部以停止发布表单。

Hi我用您的代码更新了php页面,这是返回的输出Query=UPDATE site\u content SET content='newtext',其中page\u ref='170'和template\u ref='15'以及box\u id='10'和box\u type='text',但框的实际内容是newtext bnvvbsasi发现如果我从文本区域删除nicedit,那么任何更新的文本都会被PSSE,所以这与nicedit有关确实,我也尝试过一些东西,归结起来就是这样。摆脱nicedit我会说…我不能我需要一个免费的富文本编辑器,它具有所有的功能,如改变字体等,它符合法案。如果我将表单值传递到php页面,而不是使用ajax,这很好,只是在使用jquery ajax时,我遇到了问题,这就是为什么我在这里发布文章,看看是否有人可以帮助我您正在使用并且应该使用。您也很容易受到现代API的影响,因为它会使您更容易使用。