jquery - Retrieving Javascript Variable to use in Perl -
i have html table this:
<table class="notices-table" width="100%" cellpadding="0" cellspacing="0" border="0"> <% foreach $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 have javascript code triggers when "archive" button nail each atempts retrieve "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 phone call failed:'); console.error(err); } }); homecoming false; }); });
i want pass "notice_id" perl script utilize id retrieve right post database archive it:
my $cgi = cgi->new; $notice_id = $cgi->param("notice_id2"); $form = $request->params(); $notice; eval { $notice = taskman::notice->new(notice_id => $notice_id); }; i'm pretty new perl , javascript/ajax have more problems in here. insight great. thanks
edited i've made modifications code. ajax seems working cant tell if still invoking right or not. scripts on same page. still retrieving wrong "notice_id"
there's nil fundamentally wrong approach, you're not invoking $.ajax correctly, prevent working.
specifically, you're not providing url perl script. sake of example, i'll assume perl script lives @ path /takenotice (clever, no?). phone call 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 phone call failed:'); console.error(err); } }); the success , error functions not strictly necessary, it's thought include them, , when you're learning, helps understand flow of front-end logic.
speaking of which, should add together error handling code:
var parts = id.split(/-/); var notice_id = parts[2]; var post_form_id = '#post-form-' + notice_id; if id not in format expect be, end post_form_id of "#post-form-undefined. error handling , console logging useful here.
lastly, line nothing:
$(post_form_id '#action').val('archive'); if you're trying set value, need sec parameter. if you're trying value, should assign something.
javascript jquery html ajax perl
No comments:
Post a Comment