2017-09-27 79 views
0

我有一个div如下:如何使用ajax发送数据而不需要表单?

<div id="test-container"> 
    <div id="test-one"> 
     <p id="data">Example to send</p> 
    </div> 
</div> 

我需要发送什么是<p>"Example to send" 里面,我可以把它无需使用的一种形式,只使用AJAX,jQuery和PHP

谢谢!

+0

哪里的ajax和事件处理程序? – brk

回答

2

可以使用得到p的内容是:

$.post("url_to_some_file.php", content, function(response){ 
//Here you can get the response from the server 
}); 
1

你需要一个事件来获取触发Ajax功能首先

var content = $("#data").html(); 

然后你可以使用AJAX这种方式发送

$('#button').click(function(){ 
    var data = $('.data').html() 
    $.ajax({ 
     url: 'url_to_server', 
     data: data, 
     type: 'POST', 
     success: function(result){ 
     alert(result) 
    } 
    }) 
}) 
0
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 

}); 

得到t他通过PHP的数据

<?php 
$data = $_POST['data']; 


?> 
相关问题