0

有人可以看看这些吗,它只是不适合我,它是一个 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" />&nbsp;&nbsp;
    <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";
}

?>
4

2 回答 2

1

一些东西:

您的 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'])) {(在提交时使用小写的 s)。

在这些更改之后,一切似乎都按预期工作。

于 2012-03-19T00:04:49.683 回答
0

你确定你的 php 脚本的最后一行吗?我的意思是,美元符号不应该像以下那样被转义:

print "<p>Your weekly gross salary is \$". $wage . ".</p>\n";

顺便说一句,评论是正确的:

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=...
于 2012-03-18T23:55:52.693 回答