使PHP识别是否单击了右键

时间:2018-08-05 22:28:02

标签: php html

我正在制作一个带有个人资料,个人资料编辑等的简单网站。我正在努力使其成为可能,以便人们可以通过个人资料编辑来更改他们的偏好,例如显示年龄等。由于我想向所有可能发生的变化添加按钮,因此需要添加多个提交按钮。我的问题出在哪里。我尝试将其设置为if($submitshowage) (isset($submitshowage)),但是由于我的第二个按钮不知道该索引,因此出现了Notice: Undefined index: submitshowage in /storage/ssd4/637/3688637/public_html/profile/editprocess.php on line 16错误。

我的问题是如何使多个按钮正常工作,而不询问其他按钮仅给出的信息。

我的edit.php

<form action="editprocess.php" method="POST">
<input type="radio" name="showage" value="Yes" checked>
<br>
<input type="radio" name="showage" value="No">
<br><br>
<input type="submit" name="submitshowage" value="submit">
</form>

<form action="editprocess.php" method="POST">
<p>Show your age publicly?</p>
<input type="radio" name="showage" value="Yes" checked> Yes
<br>
<input type="radio" name="showage" value="No"> No
<br><br>
<input type="submit" name="submittest" value="submit">
</form>

我的editprocess.php

       <?php
//Show age proberty
$submitshowage = $_POST['submitshowage'];
if($submitshowage) {
 if ($_POST['showage'] == 'Yes'){
     $answer = 1;
 } else {
     $answer = 0;
 }
    //id from cookie
    $id = $_COOKIE['id'];
$sql = "UPDATE login SET showage='$answer' WHERE id='$id'";

    if ($conn->query($sql) === TRUE) {
    echo "Record updated successfully";
} else {
    echo "Error updating record: " . $conn->error;
}

    $conn->close();
} else { }
    ?>

所有可能变得更容易的事情(除非需要按钮),这无关紧要,因为这是我的第一个php项目,除了享受乐趣并了解它的作用之外,不打算用它做某事

1 个答案:

答案 0 :(得分:0)

更改此代码:

$submitshowage = $_POST['submitshowage'];
if($submitshowage) {

收件人:

if (isset($_POST['submitshowage']) {

,您可以将类似的代码用于其他提交按钮,例如

if (isset($_POST['submitshowemail']) {