0

ページに 2 つのボタンがあります (はいといいえ)。この決定に基づいてデータベースを更新する別のページ「choice.php」に関数があります。この関数は、入力として「はい」または「いいえ」を取ります。したがって、各ボタンで Choice.php ページを実行し、押されたボタンに応じて「はい」または「いいえ」を指定してから、現在のページを更新します。現在、私は試しました(「はい」の場合):

echo "<form></br>";
echo "<input type='button' value = 'yes' Method ='POST' ACTION = 'choice.php'>";
echo "</form>";

しかし、私はここからどこへ行くべきかわかりません。choice.php ページには次のものがあります。

function choice('yes'/'no');
4

3 に答える 3

1

パラメータはコンマで区切る必要がありますが、スーパーグローバル配列から取得する場合、実際にはパラメータは必要ありません。'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
于 2013-11-12T08:05:43.170 に答える
0

1 つの方法は、データベース操作が完了した後 に使用headerすることです。choice.php
header('Location: *where your form page is*');

別の方法は、javascript を使用して ajax 投稿を行うことです。

于 2013-11-12T08:03:20.390 に答える
-1

次のようなものがあるとします。

<form method='POST' action='choice.php'>
<tr>
 <td><input type="submit" name="answer" value="yes"></td>
 <td><input type="submit" name="answer" value="no"></td>
</tr>
</form>

PHPファイルでは、メソッドを使用しPOSTて、答えがあったかどうかを確認yesできます。no

if($_POST['answer'] == 'yes') {
 //do something
}
if($_POST['answer'] == 'no') {
 //do something else
}
于 2013-11-12T08:04:58.330 に答える