2017-09-27 77 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']; 


?> 
相關問題