使用jQuery从Perl脚本获取响应提交表单

时间:2014-12-03 16:07:50

标签: jquery perl

我只需要一个关于如何将表单提交到Perl脚本并获得简单响应的简单示例,我认为此示例代码缺少某些内容,我无法弄明白。如果有人能够判断这是一个好的开始方式或问题所在,感谢您的帮助 这是我正在使用的:

html文件:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4   /strict.dtd">
<html>
<head>
<title>jQuery.post demo</title>
<script src="jquery-1.4.1.min.js" type="text/javascript"></script>
<script type="text/javascript">

// Attach a submit handler to the form
$( "#searchForm" ).submit(function( event ) {

// Stop form from submitting normally
event.preventDefault();

// Get some values from elements on the page:
var $form = $( this ),
namev     = $form.find( "input[name='name']" ).val(),
agev      = $form.find( "input[name='age']" ).val(),
url       = $form.attr( "action" );
alert(url);

// Send the data using post
var posting = $.post( url, { name: namev, age: agev } );

// Put the results in a div
posting.done(function( data ) {

//var content = $( data ).find( "#content" );

$( "#result" ).empty().append( data );

});

});

</script>

</head>
<body>

<p>jQuery.post</p>

<form action="form_1.pl" id="searchForm">
<input type="text" name="name" placeholder="Search...">
<input type="text" name="age">
<input type="submit" value="Search">
</form>

<!-- the result of the search will be rendered inside this div -->
<div id="result"></div>

</body>
</html>

这是Perl测试文件:

#!/usr/bin/perl

use strict;
use warnings;
use CGI;
use CGI::Carp qw(fatalsToBrowser);

my $c = new CGI;

print $c->header();

if ('POST' eq $c->request_method && $c->param('name')) {

# yes, parameter exists
# print $c->param('name');
my $name = $c->param('name');
my $age = $c->param('age');

print "<span style='color:red'>Welcome <b>$name</b>. So you're <b>$age</b> years old eh?</span>";

 }else {

  print "--";

 }

谢谢!

1 个答案:

答案 0 :(得分:1)

我用

包装了代码
$( document ).ready(function() { ... });

现在它的工作原理应该如此。

(安德烈在评论中自我回答)