Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

submit data without reload

by bigup401 (Pilgrim)
on Jan 21, 2019 at 19:42 UTC ( [id://1228777]=perlquestion: print w/replies, xml ) Need Help??

bigup401 has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: submit data without reload
by poj (Abbot) on Jan 22, 2019 at 12:44 UTC

    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

      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
Re: submit data without reload
by marto (Cardinal) on Jan 21, 2019 at 20:29 UTC

    This isn't a JavaScript forum. Several answers to your previous posts tell you what research you need to do to learn how to achieve what you are trying to do.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1228777]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2024-04-25 21:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found