Why not encode a structure than keeps the record data and the flags separate

my $json = encode_json({ record => $data, ReturnedRecord => $return_count, DeleteRecord => $delete_count });

then no need to delete anything

var url = ".../update_tables.cgi"; var param = { "action":"delete_Record", "user_id":userid, }; $.getJSON(url, param, function(data){ if (data.ReturnedRecord){ loadJSONFormData(data.record); } }
sub delete_Record { my $userid = $query->param('user_id'); warn("Delete record based on user_id = '$userid'"); # SQL my $stmt_del = 'DELETE FROM users WHERE user_id = ?'; my $stmt_sel = ' (SELECT * FROM users WHERE user_id < ? ORDER BY user_id DESC LIMIT 1) UNION (SELECT * FROM users WHERE user_id > ? ORDER BY user_id ASC LIMIT 1)'; my $data = {}; my $return_count = 0; my $exit = 0; my $delete_count = $dbh->do($stmt_del,undef,$userid); if ($delete_count eq '0E0') { # Nothing deleted $delete_count = 0; } else { # select prev or next record my $sth = $dbh->prepare($stmt_sel); $sth->execute($userid,$userid); if ($data = $sth->fetchrow_hashref()){ $return_count = 1; } else { $exit = 1; $data = {}; } } # output my $json = encode_json({ record => $data, ReturnedRecord => $return_count, DeleteRecord => $delete_count }); print "Content-Type: application/json\n\n"; print $json; warn("Finished print $json"); exit($exit); }
poj

In reply to Re^2: JSON Return Values by poj
in thread JSON Return Values by tultalk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.