单击 jquery 按钮 + 不带表单发送数据 - 书签
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10406571/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
click jquery button + send data without form - bookmark
提问by chowwy
I'm working on a bookmarking function where the user clicks on a jQueryui button and certain information is sent to the database. But I'm not using a form, because there is no information for the user to enter.
我正在研究书签功能,用户单击 jQueryui 按钮并将某些信息发送到数据库。但我没有使用表单,因为没有供用户输入的信息。
I'm pulling the user's ID from the session data, and I'm sending a URI segment (portion of the URL)
我从会话数据中提取用户 ID,并发送一个 URI 段(URL 的一部分)
Using codeigniter/php.
使用 codeigniter/php。
I'm trying to figure out what to put in the data portion of the ajax/post function, since there's no form/no data entered, and what to do about the "submit" part of the controller.
我试图弄清楚在 ajax/post 函数的数据部分中放什么,因为没有表单/没有输入数据,以及如何处理控制器的“提交”部分。
Controller
控制器
function addBookmark(){
if ($this->input->post('submit')) {
$id = $this->session->userdata('id');
$bookmark = $this->uri->segment(3, 0);
$this->bookmarks_model->postBookmark($id, $bookmark);
}
}
Model
模型
function postBookmark() {
$data = array(
'user_id' => $user_id,
'bookmark_id' => $bookmark,
);
$this->db->insert('bookmarks', $data);
}
HTML
HTML
<button class="somebutton">Add bookmark</button>
jQuery
jQuery
$('.somebutton').click(function() {
$.ajax({
url: 'controller/addBookmark',
type: 'POST',
data: ???,
success: function (result) {
alert("Your bookmark has been saved");
}
});
});
采纳答案by Brombomb
Your problem is you are checking for a submit
key in the POST
args. You can either fake it by sending data: {submit:true}
or by by removing your if statement and just processing a POST request
您的问题是您正在检查args 中的submit
键POST
。您可以通过发送data: {submit:true}
或删除 if 语句并仅处理 POST 请求来伪造它
$('.somebutton').click(function() {
$.ajax({
url: 'controller/addBookmark',
type: 'POST',
data: {'submit':true}, // An object with the key 'submit' and value 'true;
success: function (result) {
alert("Your bookmark has been saved");
}
});
});
回答by Jasonw
from the documentation of jQuery.ajax()
来自jQuery.ajax()的文档
Data to be sent to the server. It is converted to a query string, if not already a string. It's appended to the url for GET-requests.
要发送到服务器的数据。如果不是字符串,则将其转换为查询字符串。它附加到 GET 请求的 url。
If you do not know what to put for the data, probably you should remove that option? In your controller function addBookmark(), you can reduce the code by remove the if check. Try this and see if it works for you.
如果您不知道为数据放置什么,您可能应该删除该选项?在您的控制器函数 addBookmark() 中,您可以通过删除 if 检查来减少代码。试试这个,看看它是否适合你。
回答by Control Freak
Instead of using .ajax()
use either .get()
or .post()
Using .get()
使用 .get()
$.get('controller/addBookmark',function(data){
alert('Your bookmark has been saved');
});
Using .post()
使用 .post()
$.post('controller/addBookmark', function(data) {
alert('Your bookmark has been saved, The contents of the page were:' + data);
});