有人能看看这些吗?对我来说,它不是一个html表单,一个处理它的php文件和一个javascript文件,来自浏览器的错误显示了JScript文件中的U+12AF幻影非法字符,已经使用了1.5个小时,我不知所措。
--submit.js--
$(document).ready(function() {
$(function() {
$('#paycheck').bind('submit', function() {
$.post('Paycheck.php', $("#paycheck").serialize(), function(data) {
$('#result').html(data);
});
});
});
});
--Paycheck.html--
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<pre><title>Weekly Gross Paycheck Calculator</title></pre>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="submit.js"></script>
</head>
<body>
<h2 style = "text-align:center">PayCheck</h2>
<form name="paycheck" id="paycheck" action="" method="POST">
<p>Number of Hours: <input type="text" name="numHours"/></p>
<p>Hourly Wage $<input type="text" name="hourlyWage"/></p>
<p><input type="reset" value="Clear Form" />
<input type="submit" name="submit" value="Send Form" /></p>
</form>
<!-- Form and javascript will output the results here -->
<div id="result"></div>
</body>
</html>
--Paycheck.php--
<?php
function validateInput($data, $fieldName){
global $errorCount;
if (empty($data)) {
echo "'"$fieldName'" is a required field.<br/>'n";
++$errorCount;
$retval = "";
} else { // Only clean up the input if it isn't empty
if (!is_numeric($data)){
echo "'"$fieldName'" must be numeric.<br/>'n";
++$errorCount;
$retval = "";
}else{
$retval = trim($data);
$retval = stripslashes($retval);
}
return($retval);
} // end validateInput()
}
$ShowForm = TRUE;
$errorCount = 0;
$numHours = "";
$hourlyWage = "";
$wage = "";
if (isset($_POST['Submit'])) {
$numHours = validateInput($_POST['numHours'],"Number of Hours");
$hourlyWage = validateInput($_POST['hourlyWage'],"Hourly Wage");
if ($errorCount==0)
$ShowForm = FALSE;
else
$ShowForm = TRUE;
}
echo $numHours ." " . $hourlyWage;
if ($ShowForm == TRUE) {
if ($errorCount > 0)// if there were errors
print "<p>Please re-enter the form information below.</p>'n";
} else {
//If hours are over 40 then use time and a half
if($numHours > 40) { $wage = ((($numHours - 40) * 1.5) * $hourlyWage) + ($hourlyWage * 40); }
//otherwise use normal multiplication.
else { $wage = $numHours * $hourlyWage; }
print "<p>Your weekly gross salary is $". $wage . ".</p>'n";
}
?>
几件事:
js中有一个额外的嵌套lambda函数,这不是必需的。
$(document).ready($(function() {
$('#paycheck').bind('submit', function() {
$.post('Paycheck.php', $("#paycheck").serialize(), function(data) {
$('#result').html(data);
});
});
});
您的表单上没有任何操作,请更改为action="Payment.php"
同样在php脚本中,第27行应该是if (isset($_POST['submit'])) {
(submit中的小写s)。
在这些变化之后,一切似乎都按预期进行。
您确定php脚本中的最后一行吗?我的意思是,美元符号不应该像中那样被转义吗
print "<p>Your weekly gross salary is '$". $wage . ".</p>'n";
评论是正确的btw:
1)
$(document).ready(function() {
// don't double wrap
$('#paycheck').bind('submit', function() {
$.post('Paycheck.php', $("#paycheck").serialize(), function(data) {
$('#result').html(data);
});
});
});
2)
<head>
<title>Weekly Gross Paycheck Calculator</title>
<!-- no pre tags -->
<script type=...