[英]Retrieving Javascript Variable to use in Perl
I have a html table with something like this: 我有这样的html表:
<table class="notices-table" width="100%" cellpadding="0" cellspacing="0" border="0">
<% foreach my $notice (@regular_posts) { %>
<form method = "post" id = "post-form-<%= $notice->notice_id()%>">
<tr class = "click-post" href = "<%= $notice->link() %>">
<td><b>Date Posted: </b> <%= '' . (split(/\./, $notice->created()))[0] %><br/>
<b>Title: </b><%= $notice->title() %><br/><br/>
<%= $notice->content() %>
</td>
<td><button class = "archive-button" id="archive-button">Archive</button></td>
</tr>
<input type="hidden" id="action" name="action" value="archive" />
</form>
<% } %>
</table>
and then I have a javascript code that triggers when the "archive" button is hit for each that atempts to retrieve the "notice_id": 然后我有一个javascript代码,当每个试图检索“ notice_id”的按钮都被按下“归档”按钮时,就会触发该代码:
$(document).ready(function() {
$('button.archive-button').bind('click', function() {
var id = $(this).attr('id');
var parts = id.split(/-/);
var notice_id = parts[2];
var post_form_id = '#post-form-' + notice_id;
$(post_form_id).submit();
var path_name = window.location.pathname;
$.ajax(path_name, {
type: 'POST'
data: {
'notice_id2' : notice_id
},
success: function(data) {
console.log('/takeNotice called successfully:');
console.log(data);
},
error: function(jqXHR, textStatus, err) {
console.error('/takeNotice call failed:');
console.error(err);
}
});
return false;
});
}); });
I want to pass the "notice_id" into a Perl script which will use the id to retrieve the correct post from the database so that it will archive it: 我想将“ notice_id”传递到Perl脚本中,该脚本将使用id从数据库中检索正确的帖子,以便对其进行存档:
my $cgi = CGI->new;
my $notice_id = $cgi->param("notice_id2");
my $form = $Request->Params();
my $notice;
eval {
$notice = Taskman::Notice->new(notice_id => $notice_id);
};
I'm pretty new to Perl and Javascript/Ajax so I probably have more problems in here. 我对Perl和Javascript / Ajax还是很陌生,所以在这里可能会有更多问题。 Any insight would be great.
任何见识都会很棒。 Thanks
谢谢
EDITED I've made some modifications to my code. 编辑我已经对我的代码做了一些修改。 Ajax seems to be working but I cant tell if I am still invoking it right or not.
Ajax似乎正在工作,但是我无法确定我是否仍在正确调用它。 The scripts are on the same page.
这些脚本在同一页面上。 I am still retrieving the wrong "notice_id"
我仍在检索错误的“ notice_id”
There's nothing fundamentally wrong with your approach, but you're not invoking $.ajax
correctly, which will prevent anything from working. 您的方法从根本上没有错,但是您没有正确调用
$.ajax
,这将阻止任何工作。
Specifically, you're not providing a URL for your Perl script. 具体来说,您没有为Perl脚本提供URL。 For the sake of example, I'll assume your Perl script lives at the path
/takeNotice
(clever, no?). 为了举例说明,我假设您的Perl脚本位于
/takeNotice
路径中(聪明,不是吗?)。 You would call it like this: 您可以这样称呼它:
$.ajax('/takeNotice', {
type: 'POST',
data: {
notice_id2 : notice_id
},
success: function(data) {
console.log('/takeNotice called successfully:');
console.log(data);
},
error: function(jqXHR, textStatus, err) {
console.error('/takeNotice call failed:');
console.error(err);
}
});
The success
and error
functions are not strictly necessary, but it's a good idea to include them, and certainly when you're learning, it really helps understand the flow of your front-end logic. success
和error
功能不是严格必需的,但是包括它们是一个好主意,当然,当您学习时,它确实有助于理解前端逻辑的流程。
Speaking of which, you should probably also add some error handling to this code: 说到这,您可能还应该在此代码中添加一些错误处理:
var parts = id.split(/-/);
var notice_id = parts[2];
var post_form_id = '#post-form-' + notice_id;
If the ID is not in the format you expect it to be, you will end up with a post_form_id
of "#post-form-undefined
. Some error handling and console logging would be useful here. 如果该ID的格式不符合您的预期格式,则您最终会得到
post_form_id
为"#post-form-undefined
。这里有些错误处理和控制台日志记录将非常有用。
Lastly, this line does nothing: 最后,此行不执行任何操作:
$(post_form_id '#action').val('archive');
If you're trying to set the value, you need a second parameter. 如果要设置该值,则需要第二个参数。 If you're trying to get the value, you should probably assign it to something.
如果您试图获取该值,则可能应该将其分配给某些东西。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.