[英]How to send data from a div tag with ajax without the need for a form?
I have a div as follows: 我有一个div如下:
<div id="test-container">
<div id="test-one">
<p id="data">Example to send</p>
</div>
</div>
I need to send what's inside the <p>
"Example to send"
As I can send it without the need to use a form, only using ajax, jQuery and PHP 我需要发送
<p>
"Example to send"
因为我可以发送它而无需使用表单,仅使用ajax,jQuery和PHP
Thanks! 谢谢!
You can get the content of p using this: 您可以使用以下方法获取p的内容:
var content = $("#data").html();
Then you can send it using ajax this way: 然后,您可以通过以下方式使用ajax发送:
$.post("url_to_some_file.php", content, function(response){
//Here you can get the response from the server
});
you need an event to get trigger the ajax function first 您需要一个事件来首先触发ajax函数
$('#button').click(function(){
var data = $('.data').html()
$.ajax({
url: 'url_to_server',
data: data,
type: 'POST',
success: function(result){
alert(result)
}
})
})
var getdata = $("#data").html(); // get the text inside of the p tag
$.post("yourphpfile.php", {data: getdata}).done(function(r) {
alert("Data sent!"); // show alert box after the query sent
});
to get the data via PHP 通过PHP获取数据
<?php
$data = $_POST['data'];
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.