1

我昨天正在尝试通过在线资源学习 PHP。我有一个简单的程序(下面的代码)正在运行。我遇到的最后一个问题是当我单击按钮提交时,整个页面重新加载。

我想寻求您的帮助如何删除该事件。php脚本是否有可能通过不重新加载整个页面来执行?

我在网上读过几篇文章,上面说可以使用 ajax 来完成。不幸的是,我不知道该怎么做,因为我仍然是一个超级初学者。(哈哈)

<form method="POST">
Salary: <input id="salarytext" type="text" name="salary" onkeypress="return isNumberKey(event)"><br>
Membership Type:
<select name="membershiptype">
        <option value="employed">Employed</option>
        <option value="SE">Self Employed</option>
        <option value="VM">Voluntary Member</option>
        <option value="OFW">OFW</option>
</select>

<br/>

<input type="submit" />

</form>

<div class="SSSContributionComputation">
<?php

$a = (isset($_POST['salary'])) ? $_POST['salary'] : '';
$b = (isset($_POST['membershiptype'])) ? $_POST['membershiptype'] : '';


function employed($a, $b) {

if (empty ($a)) {echo "Type in your Salary to see the results.";}
elseif ($a <= 1249.99) {echo "Your Employer's (ER) Contribution is P80.70.<br>Your Contribution (EE) is P33.30.<br>Total amount credited to your SSS Account is P114.00";}
elseif ($a <= 1749.99) {echo "Your Employer's (ER) Contribution is P116.00.<br>Your Contribution (EE) is P50.00.<br>Total amount credited to your SSS Account is P166.00";}
elseif ($a <= 2249.99) {echo "Your Employer's (ER) Contribution is P151.30.<br>Your Contribution (EE) is P66.70.<br>Total amount credited to your SSS Account is P218.00";}
elseif ($a <= 2749.99) {echo "Your Employer's (ER) Contribution is P186.70.<br>Your Contribution (EE) is P83.30.<br>Total amount credited to your SSS Account is P270.00";}
elseif ($a <= 3249.99) {echo "Your Employer's (ER) Contribution is P222.00.<br>Your Contribution (EE) is P100.00.<br>Total amount credited to your SSS Account is P322.00";}
elseif ($a <= 3749.99) {echo "Your Employer's (ER) Contribution is P257.30.<br>Your Contribution (EE) is P116.70.<br>Total amount credited to your SSS Account is P374.00";}
elseif ($a <= 4249.99) {echo "Your Employer's (ER) Contribution is P292.70.<br>Your Contribution (EE) is P133.30.<br>Total amount credited to your SSS Account is P426.00";}
elseif ($a <= 4749.99) {echo "Your Employer's (ER) Contribution is P328.00.<br>Your Contribution (EE) is P150.00.<br>Total amount credited to your SSS Account is P478.00";}
elseif ($a <= 5249.99) {echo "Your Employer's (ER) Contribution is P363.30.<br>Your Contribution (EE) is P166.70.<br>Total amount credited to your SSS Account is P530.00";}
elseif ($a <= 5749.99) {echo "Your Employer's (ER) Contribution is P398.70.<br>Your Contribution (EE) is P183.30.<br>Total amount credited to your SSS Account is P582.00";}
elseif ($a <= 6249.99) {echo "Your Employer's (ER) Contribution is P434.00.<br>Your Contribution (EE) is P200.00.<br>Total amount credited to your SSS Account is P634.00";}
elseif ($a <= 6749.99) {echo "Your Employer's (ER) Contribution is P469.30.<br>Your Contribution (EE) is P216.70.<br>Total amount credited to your SSS Account is P686.00";}
elseif ($a <= 7249.99) {echo "Your Employer's (ER) Contribution is P504.70.<br>Your Contribution (EE) is P233.30.<br>Total amount credited to your SSS Account is P738.00";}
elseif ($a <= 7749.99) {echo "Your Employer's (ER) Contribution is P540.00.<br>Your Contribution (EE) is P250.00.<br>Total amount credited to your SSS Account is P790.00";}
elseif ($a <= 8249.99) {echo "Your Employer's (ER) Contribution is P575.30.<br>Your Contribution (EE) is P266.70.<br>Total amount credited to your SSS Account is P842.00";}
elseif ($a <= 8749.99) {echo "Your Employer's (ER) Contribution is P610.70.<br>Your Contribution (EE) is P283.30.<br>Total amount credited to your SSS Account is P894.00";}
elseif ($a <= 9249.99) {echo "Your Employer's (ER) Contribution is P646.00.<br>Your Contribution (EE) is P300.00.<br>Total amount credited to your SSS Account is P946.00";}
elseif ($a <= 9749.99) {echo "Your Employer's (ER) Contribution is P681.30.<br>Your Contribution (EE) is P316.70.<br>Total amount credited to your SSS Account is P998.00";}
elseif ($a <= 10249.99) {echo "Your Employer's (ER) Contribution is P716.70.<br>Your Contribution (EE) is P333.30.<br>Total amount credited to your SSS Account is P1,050.00";}
elseif ($a <= 10749.99) {echo "Your Employer's (ER) Contribution is P752.00.<br>Your Contribution (EE) is P350.00.<br>Total amount credited to your SSS Account is P1,102.00";}
elseif ($a <= 11249.99) {echo "Your Employer's (ER) Contribution is P787.30.<br>Your Contribution (EE) is P366.70.<br>Total amount credited to your SSS Account is P1,154.00";}
elseif ($a <= 11749.99) {echo "Your Employer's (ER) Contribution is P822.70.<br>Your Contribution (EE) is P383.30.<br>Total amount credited to your SSS Account is P1,206.00";}
elseif ($a <= 12249.99) {echo "Your Employer's (ER) Contribution is P858.00.<br>Your Contribution (EE) is P400.00.<br>Total amount credited to your SSS Account is P1,258.00";}
elseif ($a <= 12749.99) {echo "Your Employer's (ER) Contribution is P893.30.<br>Your Contribution (EE) is P416.70.<br>Total amount credited to your SSS Account is P1,310.00";}
elseif ($a <= 13249.99) {echo "Your Employer's (ER) Contribution is P928.70.<br>Your Contribution (EE) is P433.30.<br>Total amount credited to your SSS Account is P1,362.00";}
elseif ($a <= 13749.99) {echo "Your Employer's (ER) Contribution is P964.00.<br>Your Contribution (EE) is P450.00.<br>Total amount credited to your SSS Account is P1,414.00";}
elseif ($a <= 14249.99) {echo "Your Employer's (ER) Contribution is P999.30.<br>Your Contribution (EE) is P466.70.<br>Total amount credited to your SSS Account is P1,466.00";}
elseif ($a <= 14749.99) {echo "Your Employer's (ER) Contribution is P1,034.70.<br>Your Contribution (EE) is P483.30.<br>Total amount credited to your SSS Account is P1,518.00";}
elseif ($a <= 30000.00) {echo "Your Employer's (ER) Contribution is P1,090.00.<br>Your Contribution (EE) is P500.00.<br>Total amount credited to your SSS Account is P1,590.00";}
elseif ($a > 30000) {echo "You reached the maximum!<br>Your Employer's (ER) Contribution is P1,090.00.<br>Your Contribution (EE) is P500.00.<br>Total amount credited to your SSS Account is P1,590.00";}

}

function sevmofw($a, $b) {

if (empty ($a)) {echo "Type in your Salary to see the results.";}
elseif ($a <= 1249.99) {echo "Your Total Contribution (EE) is P104.<br>This amount will be credited to your SSS Account.";}
elseif ($a <= 1749.99) {echo "Your Total Contribution (EE) is P156.<br>This amount will be credited to your SSS Account.";}
elseif ($a <= 2249.99) {echo "Your Total Contribution (EE) is P208.<br>This amount will be credited to your SSS Account.";}
elseif ($a <= 2749.99) {echo "Your Total Contribution (EE) is P260.<br>This amount will be credited to your SSS Account.";}
elseif ($a <= 3249.99) {echo "Your Total Contribution (EE) is P312.<br>This amount will be credited to your SSS Account.";}
elseif ($a <= 3749.99) {echo "Your Total Contribution (EE) is P364.<br>This amount will be credited to your SSS Account.";}
elseif ($a <= 4249.99) {echo "Your Total Contribution (EE) is P416.<br>This amount will be credited to your SSS Account.";}
elseif ($a <= 4749.99) {echo "Your Total Contribution (EE) is P468.<br>This amount will be credited to your SSS Account.";}
elseif ($a <= 5249.99) {echo "Your Total Contribution (EE) is P520.<br>This amount will be credited to your SSS Account.";}
elseif ($a <= 5749.99) {echo "Your Total Contribution (EE) is P572.<br>This amount will be credited to your SSS Account.";}
elseif ($a <= 6249.99) {echo "Your Total Contribution (EE) is P624.<br>This amount will be credited to your SSS Account.";}
elseif ($a <= 6749.99) {echo "Your Total Contribution (EE) is P676.<br>This amount will be credited to your SSS Account.";}
elseif ($a <= 7249.99) {echo "Your Total Contribution (EE) is P728.<br>This amount will be credited to your SSS Account.";}
elseif ($a <= 7749.99) {echo "Your Total Contribution (EE) is P780.<br>This amount will be credited to your SSS Account.";}
elseif ($a <= 8249.99) {echo "Your Total Contribution (EE) is P832.<br>This amount will be credited to your SSS Account.";}
elseif ($a <= 8749.99) {echo "Your Total Contribution (EE) is P884.<br>This amount will be credited to your SSS Account.";}
elseif ($a <= 9249.99) {echo "Your Total Contribution (EE) is P936.<br>This amount will be credited to your SSS Account.";}
elseif ($a <= 9749.99) {echo "Your Total Contribution (EE) is P988.<br>This amount will be credited to your SSS Account.";}
elseif ($a <= 10249.99) {echo "Your Total Contribution (EE) is P1,040.<br>This amount will be credited to your SSS Account.";}
elseif ($a <= 10749.99) {echo "Your Total Contribution (EE) is P1,092.<br>This amount will be credited to your SSS Account.";}
elseif ($a <= 11249.99) {echo "Your Total Contribution (EE) is P1,144.<br>This amount will be credited to your SSS Account.";}
elseif ($a <= 11749.99) {echo "Your Total Contribution (EE) is P1,196.<br>This amount will be credited to your SSS Account.";}
elseif ($a <= 12249.99) {echo "Your Total Contribution (EE) is P1,248.<br>This amount will be credited to your SSS Account.";}
elseif ($a <= 12749.99) {echo "Your Total Contribution (EE) is P1,300.<br>This amount will be credited to your SSS Account.";}
elseif ($a <= 13249.99) {echo "Your Total Contribution (EE) is P1,352.<br>This amount will be credited to your SSS Account.";}
elseif ($a <= 13749.99) {echo "Your Total Contribution (EE) is P1,404.<br>This amount will be credited to your SSS Account.";}
elseif ($a <= 14249.99) {echo "Your Total Contribution (EE) is P1,456.<br>This amount will be credited to your SSS Account.";}
elseif ($a <= 14749.99) {echo "Your Total Contribution (EE) is P1,508.<br>This amount will be credited to your SSS Account.";}
elseif ($a <= 30000.00) {echo "Your Total Contribution (EE) is P1,560.<br>This amount will be credited to your SSS Account.";}
elseif ($a > 30000) {echo "You reached the maximum!<br>Your Total Contribution (EE) is P1,560.<br>This amount will be credited to your SSS Account.";}


}


if ($b == 'employed') {
    employed($a, $b); //Pass the variables into the function from the outside.
}
elseif ($b == 'SE') {
    sevmofw($a, $b);
}
elseif ($b == 'VM') {
    sevmofw($a, $b);
}
elseif ($b == 'OFW') {
    sevmofw($a, $b);
}


?>
4

1 回答 1

1

我实际上建议您并不真的需要 PHP 来处理这个应用程序。

PHP 是一种在 Web 服务器上运行的脚本语言。只有当有人从网络服务器请求 php 文件时,PHP 才能执行代码。这可以同步完成(例如当您通过按提交提交表单时)或异步完成(例如当您使用 AJAX 模式时,它使用 javascript 从服务器请求页面)。

当您需要与服务器上的其他资源(如文件或数据库)进行交互时,PHP 非常有用。在上面的代码中,您只有一点逻辑,您希望根据这些逻辑在页面上显示某些内容。

您可以通过将所有逻辑移动到 javascript 中轻松完成此操作。JavaScript 在页面加载后在用户的网络浏览器中运行,因此它可以更改页面而无需将请求发送回服务器。

PHP 可以实现您的目标。但是如果您不想刷新页面,javascript 会更合适。

它看起来像这样(尽管代码也可以变得更短):

<script>
function processForm(){
    var membershiptype = document.getElementById('membershiptype').options[document.getElementById('membershiptype').selectedIndex].value;
    if(membershiptype == 'employed'){
        employed();
    }else{
        sevmofw();
    }
}
function employed() {
    //get the value from the form
    var sal = document.getElementById('salarytext').value;
    var message =  "Type in your Salary to see the results.";
if (sal <= 1249.99) {message = "Your Employer's (ER) Contribution is P80.70.<br>Your Contribution (EE) is P33.30.<br>Total amount credited to your SSS Account is P114.00";}
elseif (sal <= 1749.99) {message = "Your Employer's (ER) Contribution is P116.00.<br>Your Contribution (EE) is P50.00.<br>Total amount credited to your SSS Account is P166.00";}
elseif (sal <= 2249.99) {message = "Your Employer's (ER) Contribution is P151.30.<br>Your Contribution (EE) is P66.70.<br>Total amount credited to your SSS Account is P218.00";}
elseif (sal <= 2749.99) {message = "Your Employer's (ER) Contribution is P186.70.<br>Your Contribution (EE) is P83.30.<br>Total amount credited to your SSS Account is P270.00";}
elseif (sal <= 3249.99) {message = "Your Employer's (ER) Contribution is P222.00.<br>Your Contribution (EE) is P100.00.<br>Total amount credited to your SSS Account is P322.00";}
elseif (sal <= 3749.99) {message = "Your Employer's (ER) Contribution is P257.30.<br>Your Contribution (EE) is P116.70.<br>Total amount credited to your SSS Account is P374.00";}
elseif (sal <= 4249.99) {message = "Your Employer's (ER) Contribution is P292.70.<br>Your Contribution (EE) is P133.30.<br>Total amount credited to your SSS Account is P426.00";}
elseif (sal <= 4749.99) {message = "Your Employer's (ER) Contribution is P328.00.<br>Your Contribution (EE) is P150.00.<br>Total amount credited to your SSS Account is P478.00";}
elseif (sal <= 5249.99) {message = "Your Employer's (ER) Contribution is P363.30.<br>Your Contribution (EE) is P166.70.<br>Total amount credited to your SSS Account is P530.00";}
elseif (sal <= 5749.99) {message = "Your Employer's (ER) Contribution is P398.70.<br>Your Contribution (EE) is P183.30.<br>Total amount credited to your SSS Account is P582.00";}
elseif (sal <= 6249.99) {message = "Your Employer's (ER) Contribution is P434.00.<br>Your Contribution (EE) is P200.00.<br>Total amount credited to your SSS Account is P634.00";}
elseif (sal <= 6749.99) {message = "Your Employer's (ER) Contribution is P469.30.<br>Your Contribution (EE) is P216.70.<br>Total amount credited to your SSS Account is P686.00";}
elseif (sal <= 7249.99) {message = "Your Employer's (ER) Contribution is P504.70.<br>Your Contribution (EE) is P233.30.<br>Total amount credited to your SSS Account is P738.00";}
elseif (sal <= 7749.99) {message = "Your Employer's (ER) Contribution is P540.00.<br>Your Contribution (EE) is P250.00.<br>Total amount credited to your SSS Account is P790.00";}
elseif (sal <= 8249.99) {message = "Your Employer's (ER) Contribution is P575.30.<br>Your Contribution (EE) is P266.70.<br>Total amount credited to your SSS Account is P842.00";}
elseif (sal <= 8749.99) {message = "Your Employer's (ER) Contribution is P610.70.<br>Your Contribution (EE) is P283.30.<br>Total amount credited to your SSS Account is P894.00";}
elseif (sal <= 9249.99) {message = "Your Employer's (ER) Contribution is P646.00.<br>Your Contribution (EE) is P300.00.<br>Total amount credited to your SSS Account is P946.00";}
elseif (sal <= 9749.99) {message = "Your Employer's (ER) Contribution is P681.30.<br>Your Contribution (EE) is P316.70.<br>Total amount credited to your SSS Account is P998.00";}
elseif (sal <= 10249.99) {message = "Your Employer's (ER) Contribution is P716.70.<br>Your Contribution (EE) is P333.30.<br>Total amount credited to your SSS Account is P1,050.00";}
elseif (sal <= 10749.99) {message = "Your Employer's (ER) Contribution is P752.00.<br>Your Contribution (EE) is P350.00.<br>Total amount credited to your SSS Account is P1,102.00";}
elseif (sal <= 11249.99) {message = "Your Employer's (ER) Contribution is P787.30.<br>Your Contribution (EE) is P366.70.<br>Total amount credited to your SSS Account is P1,154.00";}
elseif (sal <= 11749.99) {message = "Your Employer's (ER) Contribution is P822.70.<br>Your Contribution (EE) is P383.30.<br>Total amount credited to your SSS Account is P1,206.00";}
elseif (sal <= 12249.99) {message = "Your Employer's (ER) Contribution is P858.00.<br>Your Contribution (EE) is P400.00.<br>Total amount credited to your SSS Account is P1,258.00";}
elseif (sal <= 12749.99) {message = "Your Employer's (ER) Contribution is P893.30.<br>Your Contribution (EE) is P416.70.<br>Total amount credited to your SSS Account is P1,310.00";}
elseif (sal <= 13249.99) {message = "Your Employer's (ER) Contribution is P928.70.<br>Your Contribution (EE) is P433.30.<br>Total amount credited to your SSS Account is P1,362.00";}
elseif (sal <= 13749.99) {message = "Your Employer's (ER) Contribution is P964.00.<br>Your Contribution (EE) is P450.00.<br>Total amount credited to your SSS Account is P1,414.00";}
elseif (sal <= 14249.99) {message = "Your Employer's (ER) Contribution is P999.30.<br>Your Contribution (EE) is P466.70.<br>Total amount credited to your SSS Account is P1,466.00";}
elseif (sal <= 14749.99) {message = "Your Employer's (ER) Contribution is P1,034.70.<br>Your Contribution (EE) is P483.30.<br>Total amount credited to your SSS Account is P1,518.00";}
elseif (sal <= 30000.00) {message = "Your Employer's (ER) Contribution is P1,090.00.<br>Your Contribution (EE) is P500.00.<br>Total amount credited to your SSS Account is P1,590.00";}
elseif (sal > 30000) {message = "You reached the maximum!<br>Your Employer's (ER) Contribution is P1,090.00.<br>Your Contribution (EE) is P500.00.<br>Total amount credited to your SSS Account is P1,590.00";}

document.getElementById('message').innerText = message;
}

function sevmofw() {
    //get the value from the form
    var sal = document.getElementById('salarytext').value;
    var message =  "Type in your Salary to see the results.";
if (sal <= 1249.99) {message = "Your Total Contribution (EE) is P104.<br>This amount will be credited to your SSS Account.";}
elseif (sal <= 1749.99) {message = "Your Total Contribution (EE) is P156.<br>This amount will be credited to your SSS Account.";}
elseif (sal <= 2249.99) {message = "Your Total Contribution (EE) is P208.<br>This amount will be credited to your SSS Account.";}
elseif (sal <= 2749.99) {message = "Your Total Contribution (EE) is P260.<br>This amount will be credited to your SSS Account.";}
elseif (sal <= 3249.99) {message = "Your Total Contribution (EE) is P312.<br>This amount will be credited to your SSS Account.";}
elseif (sal <= 3749.99) {message = "Your Total Contribution (EE) is P364.<br>This amount will be credited to your SSS Account.";}
elseif (sal <= 4249.99) {message = "Your Total Contribution (EE) is P416.<br>This amount will be credited to your SSS Account.";}
elseif (sal <= 4749.99) {message = "Your Total Contribution (EE) is P468.<br>This amount will be credited to your SSS Account.";}
elseif (sal <= 5249.99) {message = "Your Total Contribution (EE) is P520.<br>This amount will be credited to your SSS Account.";}
elseif (sal <= 5749.99) {message = "Your Total Contribution (EE) is P572.<br>This amount will be credited to your SSS Account.";}
elseif (sal <= 6249.99) {message = "Your Total Contribution (EE) is P624.<br>This amount will be credited to your SSS Account.";}
elseif (sal <= 6749.99) {message = "Your Total Contribution (EE) is P676.<br>This amount will be credited to your SSS Account.";}
elseif (sal <= 7249.99) {message = "Your Total Contribution (EE) is P728.<br>This amount will be credited to your SSS Account.";}
elseif (sal <= 7749.99) {message = "Your Total Contribution (EE) is P780.<br>This amount will be credited to your SSS Account.";}
elseif (sal <= 8249.99) {message = "Your Total Contribution (EE) is P832.<br>This amount will be credited to your SSS Account.";}
elseif (sal <= 8749.99) {message = "Your Total Contribution (EE) is P884.<br>This amount will be credited to your SSS Account.";}
elseif (sal <= 9249.99) {message = "Your Total Contribution (EE) is P936.<br>This amount will be credited to your SSS Account.";}
elseif (sal <= 9749.99) {message = "Your Total Contribution (EE) is P988.<br>This amount will be credited to your SSS Account.";}
elseif (sal <= 10249.99) {message = "Your Total Contribution (EE) is P1,040.<br>This amount will be credited to your SSS Account.";}
elseif (sal <= 10749.99) {message = "Your Total Contribution (EE) is P1,092.<br>This amount will be credited to your SSS Account.";}
elseif (sal <= 11249.99) {message = "Your Total Contribution (EE) is P1,144.<br>This amount will be credited to your SSS Account.";}
elseif (sal <= 11749.99) {message = "Your Total Contribution (EE) is P1,196.<br>This amount will be credited to your SSS Account.";}
elseif (sal <= 12249.99) {message = "Your Total Contribution (EE) is P1,248.<br>This amount will be credited to your SSS Account.";}
elseif (sal <= 12749.99) {message = "Your Total Contribution (EE) is P1,300.<br>This amount will be credited to your SSS Account.";}
elseif (sal <= 13249.99) {message = "Your Total Contribution (EE) is P1,352.<br>This amount will be credited to your SSS Account.";}
elseif (sal <= 13749.99) {message = "Your Total Contribution (EE) is P1,404.<br>This amount will be credited to your SSS Account.";}
elseif (sal <= 14249.99) {message = "Your Total Contribution (EE) is P1,456.<br>This amount will be credited to your SSS Account.";}
elseif (sal <= 14749.99) {message = "Your Total Contribution (EE) is P1,508.<br>This amount will be credited to your SSS Account.";}
elseif (sal <= 30000.00) {message = "Your Total Contribution (EE) is P1,560.<br>This amount will be credited to your SSS Account.";}
elseif (sal > 30000) {message = "You reached the maximum!<br>Your Total Contribution (EE) is P1,560.<br>This amount will be credited to your SSS Account.";}

document.getElementById('message').innerText = message;
}
</script>

<form method="POST">
Salary: <input id="salarytext" type="text" name="salary" onkeypress="return isNumberKey(event)"><br>
Membership Type:
<select name="membershiptype">
        <option value="employed">Employed</option>
        <option value="SE">Self Employed</option>
        <option value="VM">Voluntary Member</option>
        <option value="OFW">OFW</option>
</select>

<br/>

<input type="button" value="submit" onClick="processForm();"/>

</form>

<div class="SSSContributionComputation" id="message"></div>
于 2013-07-08T03:35:17.263 回答