使用php和mysql获取和插入ajax

使用php和mysql获取和插入ajax,php,mysql,ajax,Php,Mysql,Ajax,我有一个页面,上面有一个表单,当我点击页面上的一个按钮时,它通过javascript调用将3个变量发送到一个层(page\u ref、template\u ref和box\u id) 然后使用ajax,我从mysql数据库中获取一些带有变量的内容,并将其插入到层上的文本区域中 该层有一个保存按钮,当我单击该按钮时,我想将textarea的内容保存回db 我可以从数据库中填充文本区域,没有问题,但将其推回到数据库时遇到问题 我遇到的问题是将javascript变量发送到ajax更新页面,因为它们需

我有一个页面,上面有一个表单,当我点击页面上的一个按钮时,它通过javascript调用将3个变量发送到一个层(page\u ref、template\u ref和box\u id)

然后使用ajax,我从mysql数据库中获取一些带有变量的内容,并将其插入到层上的文本区域中

该层有一个保存按钮,当我单击该按钮时,我想将textarea的内容保存回db

我可以从数据库中填充文本区域,没有问题,但将其推回到数据库时遇到问题

我遇到的问题是将javascript变量发送到ajax更新页面,因为它们需要传递给php变量

这是我的

在主页上,我调用了一个javascript函数

 edit_box('show',i,'<? echo $page_ref; ?>','<? echo $template_ref; ?>');
编辑框('show',i','';
在这层我有这个

<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>
 //Browser Support Code
 function get_edit_content(box_id,page_ref,template_ref)
   {
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("edit_content").innerHTML=xmlhttp.responseText;
    var area1 = new nicEditor({fullPanel : true}).panelInstance("edit_content",{hasPanel : true});  
    }
  }

  var queryString = "?box_id=" + box_id + "&page_ref=" + page_ref + "&template_ref=" + template_ref;

  xmlhttp.open("GET","get_content.php" + queryString,true);
  xmlhttp.send();
 }


 $("#sub").click( function() 
  {
     $.post( $("#myForm").attr("action"), 
     $("#myForm :input").serializeArray(), 
     function(info)
       { 
        $("#result").html(info); 
       });
    clearInput();
   });

$("#myForm").submit( function() 
  {
    return false;   
  });

 function clearInput() 
   {
$("#myForm :input").each( function() 
      {
    $(this).val('');
  });
    }     


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";      
      } 
   }  
</script>

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

#弹匣
{ 
填充:0;
宽度:99%;
高度:自动;
保证金:0px自动;
位置:绝对位置;
背景#fbf0;
边框:实心#0000002px;
z指数:9000;
字体系列:arial;
可见性:隐藏;
}
//浏览器支持代码
函数获取编辑内容(框id、页面参考、模板参考)
{
if(window.XMLHttpRequest)
{//IE7+、Firefox、Chrome、Opera、Safari的代码
xmlhttp=新的XMLHttpRequest();
}
其他的
{//IE6、IE5的代码
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}
xmlhttp.onreadystatechange=函数()
{
if(xmlhttp.readyState==4&&xmlhttp.status==200)
{
document.getElementById(“编辑内容”).innerHTML=xmlhttp.responseText;
var area1=newniceditor({fullPanel:true}).panelInstance(“编辑内容,{hasPanel:true}”);
}
}
var queryString=“?box\u id=“+box\u id+”和page\u ref=“+page\u ref+”和template\u ref=“+template\u ref;
open(“GET”、“GET_content.php”+queryString,true);
xmlhttp.send();
}
$(“#子”)。单击(函数()
{
$.post($(“#myForm”).attr(“操作”),
$(“#myForm:input”).serializeArray(),
功能(信息)
{ 
$(“#结果”).html(信息);
});
clearInput();
});
$(“#myForm”).submit(函数()
{
返回false;
});
函数clearInput()
{
$(“#myForm:input”)。每个(函数()
{
$(this.val(“”);
});
}     
功能编辑框(显示隐藏、框id、页面参考、模板参考)
{
如果(显示隐藏=“显示”)
{ 
获取编辑内容(框id、页面参考、模板参考);
document.getElementById('popubox').style.visibility=“visible”;
}
else if(showhide==“hide”)
{
document.getElementById('PopuBox').style.visibility=“hidden”;
} 
}  
最后是update_textarea页面

<?
include("connect.php"); 
$page_ref = $_POST['page_ref'];  
$template_ref = $_POST['template_ref'];
$box_id = $_POST['box_id'];
$edit_content = $_POST['edit_content']; 
if(mysql_query("UPADTE 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 "Successfully Inserted";
 }
else
 {
  echo "Insertion Failed";
 }
?>

我意识到我需要在ajax调用之后设置隐藏输入的值

 document.getElementById("edit_content").innerHTML=xmlhttp.responseText; 
加入

 document.getElementById("box_id").value = box_id;  

etc

既然你在使用jQuery,为什么你要在AJAX调用中使用如此冗长的Javascript,而不是
$.get()
?我的jscript和jQuery不太好,我正在使用我在网上找到的代码,并试图对其进行更改以满足我的需要我不知道你在谈论哪一部分,有什么线索吗?你要问的文本区域是nicEdit小部件?其
onSave:
选项的文档是我不想使用nicEdit onSave。我想使用ajax将这3个变量和textarea的内容推送到db
 document.getElementById("box_id").value = box_id;