Home > Backend Development > PHP Tutorial > 怎么用一个ajax把一个div里的数据提交给后台的php执行呢,就是保存?

怎么用一个ajax把一个div里的数据提交给后台的php执行呢,就是保存?

WBOY
Release: 2016-06-06 20:47:42
Original
1110 people have browsed it

怎么用一个ajax把一个div里的数据提交给后台的php执行呢,就是保存?

回复内容:

怎么用一个ajax把一个div里的数据提交给后台的php执行呢,就是保存?

写一个jQuery的示例吧,假设你要获取<div id="content">123</div>的内容并保存。

<code>var t = $('#content').text();
$.post('up.php', {text:t}, function(d) {
    /*发送成功后执行*/
});
</code>
Copy after login

up.php页面只要向正常的表单一样用$_POST['text']就可以获取到传递过去的参数了。

原生js中,需要拼接字符串 like this

<code class="lang-js">// createRequest()是自己写的兼容函数
request = new createRequest();
var data = 'content='+div.innerHTML+'&id='+num;
request.open("POST",url,true);
request.setRequestHeader("Content-Type","text/plain");
request.send(data);
request.onreadystatechange=callback;

// 这个函数特意从以前写过的程序里拿出来,应该是以前从红皮书里抄来的QAQ忘记了
function createRequest() {
  try {
    request = new XMLHttpRequest();
  } catch (tryMS) {
    try {
      request = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (otherMS) {
      try {
        request = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (failed) {
        request = null;
      }
    }
  } 
  return request;
}
</code>
Copy after login

补充一下一楼的,人家不一定要text啊

<code>var t = $('#content').text();
//或者 var t=$('#content').html();
$.post('up.php', {text:t}, function(d) {
    /*发送成功后执行*/
});
</code>
Copy after login
Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template