Table of Contents
怎么用一个ajax把一个div里的数据提交给后台的php执行呢,就是保存?
Jun 06, 2016 pm 08:47 PM
php
怎么用一个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
// 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; }
Copy after login
补充一下一楼的,人家不一定要text啊
<code>var t = $('#content').text(); //或者 var t=$('#content').html(); $.post('up.php', {text:t}, function(d) { /*发送成功后执行*/ }); </code>
Copy after login
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

Hot Article
How Long Does It Take To Beat Split Fiction?
3 weeks ago
By DDD
Repo: How To Revive Teammates
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌

Hot tools Tags

Hot Article
How Long Does It Take To Beat Split Fiction?
3 weeks ago
By DDD
Repo: How To Revive Teammates
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

How To Set Up Visual Studio Code (VS Code) for PHP Development
