Everything working perfectly

Now on to more detailed enhancements

DELETE W/O THE ENHANCEMENTS WORKS - ENHANCEMENTS UNTESTED

"delete" code below is supposed to delete record and if successful grab the next or previous record to send back to populate the originating form. If neither exist send error flag back to caller which is supposed to intercept the "data" looking for flag.

If previous or next record exist, send that back along with flag added to hash before encoded by JSOAN.

Same with delete record failure

I want to intercept the "data" to find the flags, act on them and then remove those flags before sending data on to populate the form. Is there any problem with doing that?

var inquiryQuery = "http://www.xxxxxx.org/httpsdocs/cgi-bin/upda +te_tables.cgi?action=delete_Record&user_id=" + userid + '"'; var data; $.getJSON(inquiryQuery, function(data){ //Check data for status flag. If flag, remove and send data on t +o form loadJSONFormData(data); });

Code from perl module

sub delete_Record { my $userid = $query->param('user_id'); my $json = JSON->new; my $result; my $count; my %success; my $key; my $value; my ($sth, $stmt); warn("Delete record based on user_id = '$userid'"); #Delete the desired record $key = "DeleteRecord"; $stmt = "DELETE FROM users WHERE user_id = ?"; $sth = $dbh->prepare ($stmt) or die "Error Preparing:\n" . $stmt . + "\nDBI returned: \n", $dbh->errstr; $sth->execute ($userid) or die "Unable to execute query: " . $sth- +>errstr; $value = (SELECT ROW_ COUNT()); if ($value == 0) { %success{$key} = $value; #Add "0" FLAG Delete failed no other ac +tion follows $json = JSON->new; $json->canonical(1); $json = encode_json(\%success); print "Content-Type: application/json\n\n"; print $json; warn("Finished print $json"); exit(0); } elsif ($value >= 1) { %success{$key} = $value; #Update FLAG "1" Delete success #check previous record $stmt = "SELECT * FROM users WHERE user_id <= (SELECT MAX(user_i +d) FROM users WHERE user_id < ?) ORDER BY user_id DESC LIMIT 1"; $sth = $dbh->prepare ($stmt) or die "Error Preparing:\n" . $stmt + . "\nDBI returned: \n", $dbh->errstr; $sth->execute ($userid) or die "Unable to execute query: " . $st +h->errstr; $result = $sth->fetchrow_hashref(); $count = $sth->rows; warn("count 1st Pass = '$count'"); if ($count == 0) { #check next record $stmt = "SELECT * FROM users WHERE user_id >= (SELECT MIN(user +_id) FROM users WHERE user_id > ?) ORDER BY user_id ASC LIMIT 1"; $sth = $dbh->prepare ($stmt) or die "Error Preparing:\n" . $st +mt . "\nDBI returned: \n", $dbh->errstr; $sth->execute ($userid) or die "Unable to execute query: " . $ +sth->errstr; $result = $sth->fetchrow_hashref(); $count = $sth->rows; warn("count 2nd Pass = '$count'"); $key ="ReturnedRecord"; if ($count == 0) { warn("No other record must have deleted last one"); %success( $key => $count); #Add "0" FLAG Failed to return ne +w record after successful delete $json->canonical(1); $json = encode_json(\%success); print "Content-Type: application/json\n\n"; print $json; warn("Finished print 0 count $json"); exit(1); } elsif ($count == 1) { warn("count = '$count'"); $json->canonical(1); %success( $key => $count); #Add "1" FLAG return new record s +uccessful %result = (%result, %success); #add %success flags to $resul +t $json = encode_json(\%result); print "Content-Type: application/json\n\n"; print $json; warn("Finished print $json"); exit(0); } } elsif ($count == 1) { warn("count = '$count'"); $json->canonical(1); %result = (%result, %success); #add %success flags to $result + $json = encode_json(\%result); print "Content-Type: application/json\n\n"; print $json; warn("Finished print $json"); exit(0); } } }

In reply to Re: JSON Return Values by tultalk
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.