in reply to Re: submit data without reload
in thread submit data without reload

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

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

    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

        other thing. i have been trying to get param from html. but its not working

        TEST.PL #!/usr/bin/perl -w 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="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery +.min.js"></script> <script> \$(document).ready(function () { \$("#Submit").click(function(event) { Execute(); }); function Execute(){ \$.ajax({ type: 'POST', url: 'te.pl', data: { 's': '' }, success: function(res) { \$('#result').text(res.msg); }, error: function() { alert("failed"); } }); }; }); </script> </head> <body> <form method="post"> Fullname: <input type="text" name="fname"><br> <p id="result" >Name Is:</p> <input type="button" id="Submit" name="Submit" value="Submit"/> </form> </body> </html> END_HTML
        TE.PL #!/usr/bin/perl -w use JSON; use CGI; my $q = new CGI; my $name = $q->param("fname"); my $json = encode_json( { msg => $name } ); print $q->header( -type => 'application/json' ),$json;