简体   繁体   中英

use perl cgi values inside javascript ajax

I have a perl cgi script which accepts 2 params.

I have a jquery ajax block within to the same cgi script.

Now i need to use these param values of the cgi script inside the javascript block.

Is it possible to do so?

if (param)
{
    $insp_id = param("param1");
    $section_id = param("param2");
}


my $html = <<BLOCK;
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org    /TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title> Handling Images order for Inspection: $insp_id  and Section: $section_id     </title>
<link rel="stylesheet" type="text/css" href="new_style.css">
<script type="text/javascript" src="jquery-1.8.3.min.js"></script>
<script type="text/javascript" src="jquery-ui.js"></script>
<script type="text/javascript">
jQuery(document).ready(function(){
        jQuery('.reorder_link').on('click',function(){
                jQuery("ul.reorder-photos-list").sortable({ tolerance: 'pointer' });
                jQuery('.reorder_link').html('save reordering');
                jQuery('.reorder_link').attr("id","save_reorder");
                jQuery('#reorder-helper').slideDown('slow');
                jQuery('.image_link').attr("href","javascript:void(0);");
                jQuery('.image_link').css("cursor","move");
                jQuery("#save_reorder").click(function( e ){
                        if( !jQuery("#save_reorder i").length )
                        {
                                jQuery(this).html('').prepend('<img src="img/refresh-animated.gif"/>');
                            //$(this).removeClass('addmsg2');
                            //$(this).html('<img src="images/refresh-animated.gif"/>');
                            jQuery("ul.reorder-photos-list").sortable('destroy');
                            jQuery("#reorder-helper").html( "Reordering Photos - This could take a moment. Please don't navigate away from this page." ).removeClass('light_box').addClass('notice notice_error');

                            var h = [];
                            jQuery("ul.reorder-photos-list li").each(function() {  h.push(jQuery(this).attr('id').substr(9));  });
                            //var counter=0;
                            //for (counter=0; counter<h.length; counter++)
                            //   document.write(h[counter] + "<br>");
                            jQuery.ajax({
                                    type: "POST",
                                    url: "DoImageReorder.cgi",
                                    data: {ids: " " + h + "", insp_id:7004423 , section_id:1 },
                                    success: function(html)
                                    {
                                            window.location.reload();
                                            /*$("#reorder-helper").html( "Reorder Completed - Image reorder have been successfully completed. Please reload the page for testing the reorder." ).removeClass('light_box').addClass('notice notice_success');
                                            jQuery('.reorder_link').html('reorder photos');
                                            jQuery('.reorder_link').attr("id","");*/
                                    }

                            });
                            return false;
                    }
                    e.preventDefault();
            });
    });

 });
</script>
</head>
BLOCK

Basically i have to replace 7004423 with insp_id in the javascript ajax block. Please advise. Thanks.

On the server side you can encode all your variables into json string,

use JSON;
my $json_text = encode_json({
  insp_id    => scalar param("param1"),
  section_id => scalar param("param2"),
});

then add that string into jQuery document chain,

jQuery(document)
.data("JSON", $json_text) // all server side variables in one place
.ready(..)

and finally use variables later as,

var JSON = jQuery(document).data("JSON");
console.log(JSON.insp_id);

In your cgi do:

$find="7004423";
$html =~ s/$find/$insp_id/g;

This will replace all occurrences of 7004423 with the value of $insp_id.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM