Php 根据下拉列表中的选择执行函数
今天我正在尝试学习php,我正在进行实验。我在使用下拉列表if和else以及函数时遇到问题 看来它不起作用了。我不知道如何调试它。我想做的是,当用户选择“employed”时,它只会返回一个“DoThis!”文本。但是,如果用户选择3个选项中的任何一个(自营职业者、自愿会员和OFW),它将显示“DO THAT!” 它非常简单,但我无法让它工作,我6小时前才开始使用php 请帮忙Php 根据下拉列表中的选择执行函数,php,Php,今天我正在尝试学习php,我正在进行实验。我在使用下拉列表if和else以及函数时遇到问题 看来它不起作用了。我不知道如何调试它。我想做的是,当用户选择“employed”时,它只会返回一个“DoThis!”文本。但是,如果用户选择3个选项中的任何一个(自营职业者、自愿会员和OFW),它将显示“DO THAT!” 它非常简单,但我无法让它工作,我6小时前才开始使用php 请帮忙 <form method="POST"> Salary: <input id="salarytex
<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>
<?php
$a = (isset($_POST['salary'])) ? $_POST['salary'] : '';
$b = (isset($_POST['membershiptype'])) ? $_POST['membershiptype'] : '';
function employed () {
if (empty ($a)) {echo "";}
elseif ($a<10000) {$a * 2.0}
elseif ($a<20000) {$a * 2.3}
elseif ($a<30000) {$a * 2.7}
elseif ($a>30000) {$a * 3.0}
}
function sevmofw() {
if (empty ($a)) {echo "";}
elseif ($a<10000) { $a * 1.3}
elseif ($a<20000) { $a * 1.5}
elseif ($a<30000) { $a * 1.8}
elseif ($a>30000) { $a * 2.0}
}
if ( $_POST['membershiptype'] == 'employed' ){employed();
} elseif ( $_POST['membershiptype'] == 'SE' ){sevmofw();
} elseif ( $_POST['membershiptype'] == 'VM' ){sevmofw();
} elseif ( $_POST['membershiptype'] == 'OFW' ){sevmofw();
}
?>
工资:
会员类型:
雇佣
自营的
自愿成员
OFW
这是我要做的事情的流程图
<select name="membershiptype" method="POST">
<option value="Employed" name="employed">Employed</option>
<option value="SE" name="sevmofw">Self Employed</option>
<option value="VM" name="sevmofw">Voluntary Member</option>
<option value="OFW" name="sevmofw">OFW</option>
</select>
…我想加上这个答案,因为这个主题有更多的潜力,而且你正在学习PHP,所以所有的东西都加起来了 让我们从PHP标记开始。在.php文件中,只需在末尾添加
。为什么?看到这个答案了吗
然后,将所有希望通过select执行的函数放入脚本中:
<?php
/**
* functions.php
*/
function employed()
{
return 'Employed function called.';
}
function sevmofw()
{
return 'Sevmofw function called.';
}
以下是编辑问题后您的帮助请求的答案:
首先,我也喜欢流程图!这是一种规划项目或功能的干净方法
我知道你用了三元运算符。太好了:-)
好的,那么你已经对你想要实现的目标有了一个很好的想法(流程图)。我将试着用一个例子为您指出正确的方向
根据您的流程图,“开始”是指“表格”。此示例显示了php部分,并希望表单与所需数据一起提交:
什么错误?还可能希望在调用函数的点之前,将函数的定义进一步移到顶部。解析错误:语法错误,在C:\wamp\www\php\u sandbox\switch.php的第100行上意外出现$end,将closing}
放在echo“Do THIS!”之后代码>和回显“做那个!”代码>噢!我的错!对不起。无论如何,我通过添加“}”修复了它,但我得到了另一个错误。这一个:注意:第66行的未定义索引:C:\wamp\www\php\u sandbox\switch.php中的membershiptype注意:第68行的未定义索引:C:\wamp\www\php\u sandbox\switch.php中的membershiptype这些行是什么:if($\u POST['membershiptype']='employed'){employed();}elseif($\u POST['membershiptype']='SE')){sevmov();}我已经编辑了我的代码。这与我现在正在使用的代码完全相同。我更新了我的问题,并在函数中添加了一些代码。也许您可以提供帮助?您需要将$a变量传递给函数,如employed($a)。或者使用全局变量,但在这种情况下,这不是必需的。
if (isset($_POST['membershiptype'])) {
if ( $_POST['membershiptype'] == 'employed' ){
employed();
} elseif ( $_POST['membershiptype'] == 'SE' ){
sevmofv();
}
}
<?php
/**
* functions.php
*/
function employed()
{
return 'Employed function called.';
}
function sevmofw()
{
return 'Sevmofw function called.';
}
<?php include 'functions.php'; // include the functions ?>
<!DOCTYPE html>
<html>
<head>
<title>Form Function Test</title>
</head>
<body>
<?php
// Check if the form was submitted
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset('membershiptype'))
{
// If the requested function exists, call it
if (function_exists($_POST['membershiptype']))
{
$func = $_POST['membershiptype'];
echo $func();
}
}
?>
<form action="" method="POST">
<div>
<label>
Salary: <input id="salarytext" type="text" name="salary" onkeypress="return isNumberKey(event);">
</label>
</div>
<div>
<label>
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>
</label>
</div>
<div>
<input type="submit" name="submit_btn" value="Submit form">
</div>
</form>
</body>
</html>
<?php
// START - Check if all required data was submitted
if ( isset($_POST['salary'], $_POST['membershiptype']))
{
// Get the form data
$salary = $_POST['salary'];
$member_type = $_POST['membershiptype'];
// USER SELECTS EMPLOYED?
$user_employed = ($member_type == 'employed') ? true : false;
// Create the multiplication table
// for the salary calculations
$multiBy = array(
'10000' => 1.3,
'20000' => 1.5,
'30000' => 1.8,
'30001' => 2.0,
'employed' => array(
'10000' => 2,
'20000' => 2.3,
'30000' => 2.7,
'30001' => 3.0,
),
);
// Create the calculation function
function calcSalary($s = 0, $employed = false)
{
// Use global here so you can access the
// $multiBy array from inside this function
// without the need to pass it separately
// like $s and $employed
global $multiBy;
// Check if $s is specified
if (empty($s)) return false;
// Round the $s value to be able to use
// it as key for the $multiBy array
// PHP Documentation: http://php.net/manual/en/function.round.php
$value = round($s, -4);
// Set the multiplication values
$multi = $multiBy;
if ($employed) $multi = $multiBy['employed'];
if ($value > 30000) $value = 30001;
if ($value < 10000) $value = 10000;
// Calculate the salary and return the result
return $s * $multi[$value];
}
// GET SALARY INPUT
// Putting the result in a var so you can process however you like
$calculated_salary = calcSalary($salary, $user_employed);
// END - Print the calculated salary
echo $calculated_salary;
}
else
{
// I guess this is self-explanatory :P
echo 'Please enter all required data and try again.';
}