in reply to submit data without reload

See here. You need to escape (\) the $ in the javascript and add jquery.js. Also you need to show the code for test.pl because more errors could be there.

#!/usr/bin/perl use strict; use warnings; print "Content-type: text/html\n\n"; print <<END_HTML; <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" /> <title>Untitled Document</title> <script src="jquery-3.1.1.min.js"></script> <script> \$(document).ready(function () { \$("#Submit").click(function(event) { Execute(); }); function Execute(){ \$.ajax({ type: 'POST', url: 'test1.pl', data: { 's': '' }, success: function(res) { alert(res.msg); }, error: function() { alert("failed"); } }); }; }); </script> </head> <body> <input type="button" id="Submit" name="Submit" value="Submit"/> </body> </html> END_HTML
#!/usr/bin/perl # test1.pl use strict; use JSON; use CGI; my $q = new CGI; my $json = encode_json( { msg => "Time now is ".localtime } ); print $q->header( -type => 'application/json' ),$json;
poj

Replies are listed 'Best First'.
Re^2: submit data without reload
by bigup401 (Pilgrim) on Jan 22, 2019 at 15:50 UTC

    thanks for the logic. it has worked for me. i really appreciate for your effort

    now the problem i have. resluts come in popup window. and want to print resluts on page with no popup. i tried to change it from

    #!/usr/bin/perl # test1.pl use strict; use JSON; use CGI; my $q = new CGI; my $json = encode_json( { msg => "Time now is ".localtime } ); print $q->header( -type => 'application/json' ),$json;
    to
    #!/usr/bin/perl use JSON; use CGI; my $q = new CGI; my $json = encode_json( { msg => "Time now is ".localtime } ); print "Content-type: text/html\n\n"; print $json;

    so am getting undefined as error

    because if use print $q->header( -type => 'application/json' ),$json; that means its going to bring a popup window. cgi header generates popup window

      The pop-up is the alert in the Execute() function

      success: function(res) { alert(res.msg); },

      Change to

      success: function(res) { \$('#result').text(res.msg); },

      and add space for the result to html

      <body> <p id="result" >Time now is :</p> <input type="button" id="Submit" name="Submit" value="Submit"/> </body>
      poj

        thanks poj