1
我只需要一個簡單的例子來說明如何將表單提交到Perl腳本並獲得簡單的響應,我認爲這個示例代碼缺少一些東西,而且我無法確定出來。如果有人能夠判斷這是否是一個很好的開始或問題出在哪裏,謝謝你的幫助。
這裏是我使用的是什麼:
HTML文件:使用jQuery提交表單從Perl腳本獲取響應
<!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 "--";
}
謝謝!
看起來是正確的,你可以看到什麼反應? apache是否設置爲執行CGI? – 2014-12-03 16:10:15
爲什麼這麼古老的jQuery版本? – Barmar 2014-12-03 16:17:53
「我認爲這個示例代碼缺少一些東西,我無法弄清楚」 - 這不是對你的問題非常有用的描述。你爲什麼覺得缺少一些東西?你看到了什麼意想不到的行爲? Web服務器錯誤日誌中是否有任何內容? – 2014-12-03 16:42:52