in reply to JSON Return Values

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); } } }

Replies are listed 'Best First'.
Re^2: JSON Return Values
by poj (Abbot) on Mar 23, 2018 at 19:38 UTC

    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); } }
    poj

      Pretty slick. Lots of shortcuts particularly the UNION.

      Thanks much