参数应该用逗号分隔,但是从超全局数组中获取时并不需要参数。但是,您只需要在以下情况下调用该函数'yes'
:
<form action="choice.php" method="post">
<input type="submit" name="choice" value="yes" />
<input type="submit" name="choice" value="no" /> <!-- You'd better use radio buttons -->
</form>
<?php
function choice() {
if ($db->query("UPDATE ..... ;")) {
return true;
}
return false;
}
if (isset($_POST['choice']) && $_POST['choice'] == 'yes') {
choice();
}
else {
echo 'no';
}
?>
当然,您可以有多个 if,但我认为它对您的帮助不够:
if (isset($_POST['choice']) && $_POST['choice'] == 'yes') {
//something;
}
elseif (isset($_POST['choice']) && $_POST['choice'] == 'no') {
//something else;
}
elseif (isset($_POST['choice']) && $_POST['choice'] == 'maybe') {
//something else;
}
如果您希望函数使用来自用户的值更新 db,您可以使用以下内容:
function choice() {
$choices = array('yes', 'no', 'maybe', 'dunno'); //predefined choices
if (in_array($_POST['choice'], $choices)) { //checks if the user input is one of the predefined choices (yes, no, maybe or dunno)
if($db->query("UPDATE table1 SET userchoice = '{$_POST['choice']}' WHERE user_id = '{$_SESSION['id']}';")) {
return true;
}
}
return false;
}
if (isset($_POST['choice'])) choice(); //so here you don't need (not necessary, but you can) to check if the value is the one you want. If it's set, you call choice(), then the function checks if it's in the array of predefined choices, so if it is - it will update, if it's not it will return false